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