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