12
|
1 package jp.ac.u_ryukyu.ie.cr.tatsuki.xml;
|
|
2
|
17
|
3 import java.io.BufferedWriter;
|
|
4 import java.io.File;
|
12
|
5 import java.io.FileInputStream;
|
|
6 import java.io.FileNotFoundException;
|
17
|
7 import java.io.FileWriter;
|
12
|
8 import java.io.IOException;
|
17
|
9 import java.io.PrintWriter;
|
12
|
10 import java.nio.ByteBuffer;
|
|
11 import java.util.Iterator;
|
|
12 import java.util.Random;
|
|
13
|
|
14 import javax.xml.parsers.ParserConfigurationException;
|
|
15 import javax.xml.parsers.SAXParser;
|
|
16 import javax.xml.parsers.SAXParserFactory;
|
|
17
|
|
18 import org.xml.sax.SAXException;
|
|
19
|
19
|
20 import jp.ac.u_ryukyu.cr.ie.tatsuki.xmlTestBbs.JuGrix;
|
12
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
|
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
|
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
|
|
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
|
28 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
29 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
|
30 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.InterfaceTraverser;
|
|
31 import ac.jp.u_ryukyu.cr.ie.tatsuki.xmlReader.ReadXmlHandler;
|
|
32
|
|
33 public class extendXmlTree {
|
|
34
|
17
|
35 public static void main(String args[]) throws FileNotFoundException, ParserConfigurationException, SAXException,
|
|
36 IOException {
|
19
|
37 System.out.println("extendXmlTree");
|
17
|
38 if (args.length != 2) {
|
|
39 System.out.println("Arguments are two necessary , first person count, second use Index");
|
|
40 return;
|
|
41 }
|
|
42
|
|
43 int maxPersonCount = Integer.parseInt(args[0]);
|
|
44
|
|
45 boolean useIndex = false;
|
|
46 if (args[1].equals("true"))
|
|
47 useIndex = true;
|
|
48
|
18
|
49 for (int personCount = 100; personCount <= maxPersonCount; personCount = personCount + 100) {
|
17
|
50 createJuGrix(useIndex, personCount);
|
|
51 }
|
12
|
52
|
|
53 }
|
|
54
|
17
|
55 public static JuGrix createJuGrix(boolean useIndex, int personCount) throws ParserConfigurationException,
|
|
56 SAXException, FileNotFoundException, IOException {
|
12
|
57 Jungle jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser()));
|
|
58 JungleTree readXmlTree = jungle.createNewTree("Person");
|
17
|
59 // JungleTree readXmlTree = readXml("Person", jungle);
|
12
|
60 JungleTree orgTree = readXml("Organization", jungle);
|
|
61 JungleTree roleTree = readXml("Role", jungle);
|
19
|
62 JungleTree rdeTree = readXml("RoleDescriptionElement",jungle);
|
12
|
63 JungleTreeEditor editor = readXmlTree.getTreeEditor();
|
|
64 NodePath path = new DefaultNodePath();
|
|
65 editor = editor.addNewChildAt(path, 0).b();
|
|
66 editor = editor.putAttribute(path.add(0), "element", ByteBuffer.wrap("Persons".getBytes())).b();
|
17
|
67
|
|
68 long start = System.currentTimeMillis();
|
|
69
|
12
|
70 for (int count = 0; count < personCount; ++count) {
|
|
71 editor = extendsTree(editor, orgTree, roleTree, count);
|
|
72 }
|
17
|
73
|
|
74 long stop = System.currentTimeMillis();
|
|
75 File file = new File("./time/createPersonTime");
|
|
76 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
|
|
77 pw.println(personCount + " " + (stop - start));
|
|
78 pw.close();
|
|
79 System.out.println("create person time = " + (stop - start));
|
|
80 System.out.println("person count = " + personCount);
|
12
|
81 editor.success();
|
19
|
82 JuGrix jugrix = new JuGrix(useIndex, readXmlTree, orgTree, roleTree,rdeTree);
|
12
|
83 return jugrix;
|
|
84 }
|
|
85
|
17
|
86 public static JungleTreeEditor extendsTree(JungleTreeEditor editor, JungleTree orgTree, JungleTree roleTree, int count) {
|
12
|
87 Random rdm = new Random();
|
|
88 int orgNum = rdm.nextInt(30);
|
|
89 String orgId = "o:" + String.valueOf(orgNum);
|
|
90 String orgRoleId = searchOrgRole(orgTree, orgId);
|
|
91 String roleId = searchRole(roleTree, orgRoleId);
|
|
92 orgNum = rdm.nextInt(30);
|
|
93
|
|
94 String orgId2 = "o:" + String.valueOf(orgNum);
|
|
95 String orgRoleId2 = searchOrgRole(orgTree, orgId2);
|
|
96 String roleId2 = searchRole(roleTree, orgRoleId2);
|
|
97
|
|
98 NodePath path = new DefaultNodePath().add(0);
|
|
99 editor = editor.addNewChildAt(path, count).b();
|
|
100 path = path.add(count);
|
|
101 editor = editor.putAttribute(path, "element", ByteBuffer.wrap("Person".getBytes())).b();
|
|
102 String personId = String.valueOf(count);
|
|
103 editor = editor.putAttribute(path, "Person-id", ByteBuffer.wrap(("p:" + personId).getBytes())).b();
|
|
104 editor = editor.putAttribute(path, "Person-type", ByteBuffer.wrap("Person".getBytes())).b();
|
|
105
|
|
106 editor = editor.addNewChildAt(path, 0).b();
|
|
107 editor = editor.putAttribute(path.add(0), "element", ByteBuffer.wrap(("accountId").getBytes())).b();
|
|
108 editor = editor.putAttribute(path.add(0), "text-accountId", ByteBuffer.wrap(("a:" + personId).getBytes())).b();
|
|
109
|
|
110 editor = editor.addNewChildAt(path, 1).b();
|
|
111 editor = editor.putAttribute(path.add(1), "element", ByteBuffer.wrap(("lastName").getBytes())).b();
|
|
112 editor = editor.putAttribute(path.add(1), "text-lastName", ByteBuffer.wrap(("金川").getBytes())).b();
|
|
113
|
|
114 editor = editor.addNewChildAt(path, 2).b();
|
|
115 editor = editor.putAttribute(path.add(2), "element", ByteBuffer.wrap(("name").getBytes())).b();
|
|
116 editor = editor.putAttribute(path.add(2), "text-name", ByteBuffer.wrap(("竜己").getBytes())).b();
|
|
117
|
|
118 editor = editor.addNewChildAt(path, 3).b();
|
|
119 editor = editor.putAttribute(path.add(3), "element", ByteBuffer.wrap(("nameReading").getBytes())).b();
|
|
120 editor = editor.putAttribute(path.add(3), "text-nameReading", ByteBuffer.wrap(("かながわたつき").getBytes())).b();
|
|
121
|
|
122 editor = editor.addNewChildAt(path, 4).b();
|
|
123 editor = editor.putAttribute(path.add(4), "element", ByteBuffer.wrap(("roleRefIds").getBytes())).b();
|
|
124
|
|
125 editor = editor.addNewChildAt(path.add(4), 0).b();
|
|
126 editor = editor.putAttribute(path.add(4).add(0), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
127 editor = editor.putAttribute(path.add(4).add(0), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes())).b();
|
|
128 editor = editor.addNewChildAt(path.add(4), 1).b();
|
|
129 editor = editor.putAttribute(path.add(4).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
130 editor = editor.putAttribute(path.add(4).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes())).b();
|
|
131
|
|
132 editor = editor.addNewChildAt(path, 5).b();
|
|
133 editor = editor.putAttribute(path.add(5), "element", ByteBuffer.wrap(("parentOrganizations").getBytes())).b();
|
|
134 editor = editor.putAttribute(path.add(5), "parentOrganizations-type",
|
|
135 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
136
|
|
137 NodePath parentOrgPath = path.add(5);
|
|
138 editor = editor.addNewChildAt(parentOrgPath, 0).b();
|
|
139 editor = editor.putAttribute(parentOrgPath.add(0), "element",
|
|
140 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
141 editor = editor.putAttribute(parentOrgPath.add(0), "OrganizationMappedByRole-type",
|
|
142 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
143
|
|
144 editor = editor.addNewChildAt(parentOrgPath.add(0), 0).b();
|
|
145 editor = editor.putAttribute(parentOrgPath.add(0).add(0), "element",
|
|
146 ByteBuffer.wrap(("organizationRefId").getBytes())).b();
|
|
147 editor = editor.putAttribute(parentOrgPath.add(0).add(0), "text-organizationRefId",
|
|
148 ByteBuffer.wrap((orgId).getBytes())).b();
|
|
149
|
|
150 editor = editor.addNewChildAt(parentOrgPath.add(0), 1).b();
|
|
151 editor = editor.putAttribute(parentOrgPath.add(0).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
152 editor = editor.putAttribute(parentOrgPath.add(0).add(1), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes()))
|
|
153 .b();
|
|
154
|
|
155 editor = editor.addNewChildAt(parentOrgPath, 1).b();
|
|
156 editor = editor.putAttribute(parentOrgPath.add(1), "element",
|
|
157 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
158 editor = editor.putAttribute(parentOrgPath.add(1), "OrganizationMappedByRole-type",
|
|
159 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
160
|
|
161 editor = editor.addNewChildAt(parentOrgPath.add(1), 0).b();
|
|
162 editor = editor.putAttribute(parentOrgPath.add(1).add(0), "element",
|
|
163 ByteBuffer.wrap(("organizationRefId").getBytes())).b();
|
|
164 editor = editor.putAttribute(parentOrgPath.add(1).add(0), "text-organizationRefId",
|
|
165 ByteBuffer.wrap((orgId2).getBytes())).b();
|
|
166
|
|
167 editor = editor.addNewChildAt(parentOrgPath.add(1), 1).b();
|
|
168 editor = editor.putAttribute(parentOrgPath.add(1).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
169 editor = editor.putAttribute(parentOrgPath.add(1).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes()))
|
|
170 .b();
|
|
171
|
|
172 editor = editor.addNewChildAt(path, 6).b();
|
|
173 editor = editor.putAttribute(path.add(6), "element", ByteBuffer.wrap(("priorities").getBytes())).b();
|
|
174 editor = editor.putAttribute(path.add(6), "priorities-type",
|
|
175 ByteBuffer.wrap(("OrganizationMappedByRole").getBytes())).b();
|
|
176
|
|
177 //
|
|
178 editor = editor.addNewChildAt(path.add(6), 0).b();
|
|
179 editor = editor.putAttribute(path.add(6).add(0), "element", ByteBuffer.wrap(("PriorityMappedByRole").getBytes()))
|
|
180 .b();
|
|
181 editor = editor.putAttribute(path.add(6).add(0), "PriorityMappedByRole-type",
|
|
182 ByteBuffer.wrap(("PriorityMappedByRole").getBytes())).b();
|
|
183
|
|
184 editor = editor.addNewChildAt(path.add(6).add(0), 0).b();
|
|
185 editor = editor.putAttribute(path.add(6).add(0).add(0), "element", ByteBuffer.wrap(("priority").getBytes())).b();
|
|
186 editor = editor.putAttribute(path.add(6).add(0).add(0), "text-priority", ByteBuffer.wrap(("0").getBytes())).b();
|
|
187 editor = editor.addNewChildAt(path.add(6).add(0), 1).b();
|
|
188 editor = editor.putAttribute(path.add(6).add(0).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
189 editor = editor.putAttribute(path.add(6).add(0).add(1), "text-roleRefId", ByteBuffer.wrap((roleId).getBytes())).b();
|
|
190
|
|
191 editor = editor.addNewChildAt(path.add(6), 1).b();
|
|
192 editor = editor.putAttribute(path.add(6).add(1), "element", ByteBuffer.wrap(("PriorityMappedByRole").getBytes()))
|
|
193 .b();
|
|
194 editor = editor.putAttribute(path.add(6).add(1), "PriorityMappedByRole-type",
|
|
195 ByteBuffer.wrap(("PriorityMappedByRole").getBytes())).b();
|
|
196
|
|
197 editor = editor.addNewChildAt(path.add(6).add(1), 0).b();
|
|
198 editor = editor.putAttribute(path.add(6).add(1).add(0), "element", ByteBuffer.wrap(("priority").getBytes())).b();
|
|
199 editor = editor.putAttribute(path.add(6).add(1).add(0), "text-priority", ByteBuffer.wrap(("1").getBytes())).b();
|
|
200 editor = editor.addNewChildAt(path.add(6).add(1), 1).b();
|
|
201 editor = editor.putAttribute(path.add(6).add(1).add(1), "element", ByteBuffer.wrap(("roleRefId").getBytes())).b();
|
|
202 editor = editor.putAttribute(path.add(6).add(1).add(1), "text-roleRefId", ByteBuffer.wrap((roleId2).getBytes()))
|
|
203 .b();
|
|
204 return editor;
|
|
205
|
|
206 }
|
|
207
|
|
208 private static String searchRole(JungleTree roleTree, String orgRoleId) {
|
|
209 InterfaceTraverser traverser = roleTree.getTraverser(false);
|
|
210 Iterator<TreeNode> roleIdIterator = traverser.find((TreeNode node) -> {
|
|
211 String element = node.getAttributes().getString("element");
|
|
212 if (element == null)
|
|
213 return false;
|
|
214 if (!element.equals("Role"))
|
|
215 return false;
|
|
216 String compareRoleId = node.getAttributes().getString("Role-id");
|
|
217 if (compareRoleId == null)
|
|
218 return false;
|
|
219 if (compareRoleId.equals(orgRoleId))
|
|
220 return true;
|
|
221 return false;
|
|
222 }, "Role-id", orgRoleId);
|
|
223
|
|
224 if (!roleIdIterator.hasNext())
|
|
225 return orgRoleId;
|
|
226
|
|
227 TreeNode orgIdNode = roleIdIterator.next();
|
|
228 TreeNode roleRefIdNode = orgIdNode.getChildren().at(0).b().getChildren().at(0).b();
|
|
229 if (roleRefIdNode == null)
|
|
230 return orgRoleId;
|
|
231 String roleId = roleRefIdNode.getAttributes().getString("text-childrenRoleRefId");
|
|
232
|
|
233 return roleId;
|
|
234 }
|
|
235
|
|
236 private static String searchOrgRole(JungleTree orgTree, String orgId) {
|
|
237 InterfaceTraverser traverser = orgTree.getTraverser(false);
|
|
238 Iterator<TreeNode> orgIdIterator = traverser.find((TreeNode node) -> {
|
|
239 String element = node.getAttributes().getString("element");
|
|
240 if (element == null)
|
|
241 return false;
|
|
242 if (!element.equals("Organization"))
|
|
243 return false;
|
|
244 String compareRoleId = node.getAttributes().getString("Organization-id");
|
|
245 if (compareRoleId == null)
|
|
246 return false;
|
|
247 if (compareRoleId.equals(orgId))
|
|
248 return true;
|
|
249 return false;
|
|
250 }, "Organization-id", orgId);
|
|
251
|
|
252 if (!orgIdIterator.hasNext())
|
|
253 return "";
|
|
254
|
|
255 TreeNode orgIdNode = orgIdIterator.next();
|
|
256 TreeNode roleRefIdNode = orgIdNode.getChildren().at(2).b().getChildren().at(0).b();
|
|
257 String roleId = roleRefIdNode.getAttributes().getString("text-roleRefId");
|
|
258 return roleId;
|
|
259 }
|
|
260
|
|
261 private static JungleTree readXml(String xml, Jungle jungle) throws ParserConfigurationException, SAXException,
|
|
262 IOException, FileNotFoundException {
|
|
263 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
|
|
264 SAXParser saxParser = saxParserFactory.newSAXParser();
|
|
265 JungleTree tree = jungle.createNewTree(xml);
|
|
266 ReadXmlHandler readXmlHandler = new ReadXmlHandler(tree);
|
17
|
267 String currentPath = System.getProperty("user.dir");
|
|
268 saxParser.parse(new FileInputStream(currentPath + "/xml/" + xml + ".xml"), readXmlHandler);
|
12
|
269 return readXmlHandler.getTree();
|
|
270 }
|
|
271
|
|
272 }
|