4
|
1 package jp.ac.u_ryukyu.ie.cr.bbs.browsing;
|
|
2
|
|
3
|
|
4 import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle;
|
|
5 import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
|
|
6 import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree;
|
|
7 import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
|
|
8 import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
|
|
9 import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
|
|
10 import jp.ac.u_ryukyu.ie.cr.jungle.store.NulIterator;
|
|
11 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
|
|
12 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor;
|
|
13 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
|
|
14 import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren;
|
|
15 import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode;
|
|
16 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser;
|
5
|
17 import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser;
|
4
|
18 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
19 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
5
|
20 import plparser.Property;
|
|
21 import plparser.jungle.PropertyJungleTreeFactoryImpl;
|
|
22 import plparser.jungle.PropertyListJungleNodeFactory;
|
|
23 import plparser.jungle.PropertyListParserCreateTree;
|
4
|
24
|
|
25 import java.nio.ByteBuffer;
|
|
26 import java.util.Iterator;
|
|
27
|
|
28 public class JungleBrowsingBulletinBoard implements
|
|
29 BrowsingBulletinBoard {
|
|
30 private final Jungle jungle;
|
|
31
|
|
32 public JungleBrowsingBulletinBoard() {
|
|
33 jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(
|
|
34 new DefaultTraverser()));
|
|
35 jungle.createNewTree("boards");
|
|
36 }
|
|
37
|
|
38 public Iterable<String> getBoards() {
|
|
39 JungleTree tree = jungle.getTreeByName("boards");
|
|
40 TreeNode node = tree.getRootNode();
|
|
41 Children chs = node.getChildren();
|
|
42
|
|
43 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
|
|
44 public String conv(TreeNode _b) {
|
|
45 ByteBuffer e = _b.getAttributes().get("name");
|
|
46 return new String(e.array());
|
|
47 }
|
|
48 };
|
|
49
|
|
50 return new IterableConverter<String, TreeNode>(chs, converter);
|
|
51 }
|
|
52
|
|
53 public void createBoards(final String _name) {
|
5
|
54 createBoards(_name, new DefaultTreeNode());
|
|
55 }
|
|
56 public void createBoards(final String _name, TreeNode rootNode) {
|
|
57 if (null == jungle.createNewTree(_name, rootNode)) {
|
4
|
58 throw new IllegalStateException();
|
|
59 }
|
|
60 JungleTree tree = jungle.getTreeByName("boards");
|
|
61 JungleTreeEditor editor = tree.getTreeEditor();
|
|
62 DefaultNodePath root = new DefaultNodePath();
|
|
63 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
|
|
64 if (either.isA()) {
|
|
65 throw new IllegalStateException();
|
|
66 }
|
|
67 editor = either.b();
|
|
68
|
|
69 either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes()));
|
|
70 if (either.isA()) {
|
|
71 throw new IllegalStateException();
|
|
72 }
|
|
73 editor = either.b();
|
|
74 Either<Error, JungleTreeEditor> result = editor.success();
|
|
75 if (result.isA()) {
|
|
76 throw new IllegalStateException();
|
|
77 }
|
|
78
|
|
79 tree = jungle.getTreeByName(_name);
|
|
80 editor = tree.getTreeEditor();
|
|
81 either = editor.putAttribute(root, "NodeName", ByteBuffer.wrap("root".getBytes()));
|
|
82 if (either.isA()) {
|
|
83 throw new IllegalStateException();
|
|
84 }
|
|
85 editor = either.b();
|
|
86 editor.success();
|
|
87 }
|
|
88
|
|
89 public void createBoardMessage(final String boardName, final String key,
|
|
90 final String attribute, final String pathStr) {
|
|
91 if (key.equals("") | attribute.equals(""))
|
|
92 return ;
|
|
93 NodePath path = createNodePath(pathStr);
|
|
94 JungleTree tree = jungle.getTreeByName(boardName);
|
|
95 if (tree == null) {
|
|
96 throw new IllegalStateException();
|
|
97 }
|
|
98
|
|
99 JungleTreeEditor editor;
|
|
100 do {
|
|
101 editor = tree.getTreeEditor();
|
|
102 Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));
|
|
103 if (either.isA()) {
|
|
104 throw new IllegalStateException();
|
|
105 }
|
|
106 editor = either.b();
|
|
107 } while (editor.success().isA());
|
|
108 }
|
|
109
|
|
110 @Override
|
|
111 public void createChild(String bname, String nodeName, String pathStr) {
|
|
112 NodePath path = createNodePath(pathStr);
|
|
113 JungleTree tree = jungle.getTreeByName(bname);
|
|
114 if (tree == null) {
|
|
115 throw new IllegalStateException();
|
|
116 }
|
|
117 Either<Error,TreeNode> getNodeEither = tree.getNodeOfPath(path);
|
|
118 if (getNodeEither.isA())
|
|
119 return ;
|
|
120 TreeNode currentNode = getNodeEither.b();
|
|
121 int childCount = currentNode.getChildren().size();
|
|
122
|
|
123 JungleTreeEditor editor;
|
|
124 do {
|
|
125 editor = tree.getTreeEditor();
|
|
126 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(path, childCount);
|
|
127 if (either.isA()) {
|
|
128 throw new IllegalStateException();
|
|
129 }
|
|
130 editor = either.b();
|
|
131
|
|
132 either = editor.putAttribute(path.add(childCount),"NodeName",ByteBuffer.wrap(nodeName.getBytes()));
|
|
133 if (either.isA()) {
|
|
134 throw new IllegalStateException();
|
|
135 }
|
|
136 editor = either.b();
|
|
137
|
|
138 } while (editor.success().isA());
|
|
139 }
|
|
140
|
|
141 public void editMessage(String bname, String key, final String attribute,
|
|
142 final String pathStr) {
|
|
143 NodePath path = createNodePath(pathStr);
|
|
144
|
|
145 JungleTreeEditor editor = null;
|
|
146
|
|
147 do {
|
|
148 JungleTree tree = jungle.getTreeByName(bname);
|
|
149 editor = tree.getTreeEditor();
|
|
150
|
|
151 Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));
|
|
152
|
|
153 if (either.isA()) {
|
|
154 throw new IllegalStateException();
|
|
155 }
|
|
156 editor = either.b();
|
|
157 } while (editor.success().isA());
|
|
158 }
|
|
159
|
|
160 public String sanitize(String str) {
|
|
161 if (str == null) {
|
|
162 return str;
|
|
163 }
|
|
164 str = str.replaceAll("&", "&");
|
|
165 str = str.replaceAll("<", "<");
|
|
166 str = str.replaceAll(">", ">");
|
|
167 str = str.replaceAll("\"", """);
|
|
168 str = str.replaceAll("'", "'");
|
|
169 return str;
|
|
170 }
|
|
171
|
|
172 public GetAttributeImp getAttribute(String _bname, String nodePath) {
|
|
173
|
|
174 DefaultNodePath path = createNodePath(nodePath);
|
|
175 JungleTree tree = jungle.getTreeByName(_bname);
|
|
176 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
177 if (either.isA())
|
|
178 return new GetAttributeImp(new DefaultTreeNode());
|
|
179 TreeNode currentNode = either.b();
|
|
180 return new GetAttributeImp(currentNode);
|
|
181 }
|
|
182
|
|
183 @Override
|
|
184 public Iterator<TreeNode> getChildren(String bname, String nodePath) {
|
|
185 DefaultNodePath path = createNodePath(nodePath);
|
|
186 JungleTree tree = jungle.getTreeByName(bname);
|
|
187 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
188 if (either.isA())
|
|
189 return new NulIterator<TreeNode>();
|
|
190 TreeNode currentNode = either.b();
|
|
191 TreeNodeChildren children = currentNode.getChildren();
|
|
192 return children.iterator();
|
|
193 }
|
|
194
|
5
|
195 @Override
|
|
196 public void importLayout(String boardName, String path) {
|
|
197 PropertyListParserCreateTree<Property> jp;
|
|
198 PropertyListJungleNodeFactory jlf = new PropertyJungleTreeFactoryImpl();
|
|
199 jp = new PropertyListParserCreateTree<Property>(jlf);
|
|
200 TreeNode root = jp.parseFile(path);
|
|
201 createBoards(boardName, root);
|
|
202 }
|
|
203
|
|
204 @Override
|
|
205 public Iterator<TreeNode> findNode(String bname, String key, String value) {
|
|
206 JungleTree tree = jungle.getTreeByName(bname);
|
|
207 InterfaceTraverser traverser = tree.getTraverser(true);
|
|
208 return traverser.find((TreeNode node) -> {
|
|
209 return true;
|
|
210 },key,value);
|
|
211 }
|
|
212
|
|
213 @Override
|
|
214 public String getNodePath(String bname, TreeNode node) {
|
|
215 JungleTree tree = jungle.getTreeByName(bname);
|
|
216 NodePath path = tree.getNodePath(node);
|
|
217 return path.toString();
|
|
218 }
|
|
219
|
|
220 @Override
|
|
221 public boolean childMove(String bname, String pathString, String childNumString, String move) {
|
|
222 int childNum = Integer.parseInt(childNumString);
|
|
223 JungleTree tree = jungle.getTreeByName(bname);
|
|
224 DefaultNodePath path = createNodePath(pathString);
|
|
225 TreeNode node = tree.getNodeOfPath(path).b();
|
|
226 int childrenCount = node.getChildren().size();
|
|
227 TreeNode child = node.getChildren().at(childNum).b();
|
|
228 TreeNodeChildren grandsons = child.getChildren();
|
|
229 JungleTreeEditor editor = tree.getTreeEditor();
|
|
230 editor = editor.deleteChildAt(path,childNum).b();
|
|
231 if (move.equals("up") && childNum != 0)
|
|
232 childNum--;
|
|
233 else if (move.equals("down") && childNum != childrenCount)
|
|
234 childNum++;
|
|
235 editor = editor.addNewChildAt(path,childNum).b();
|
|
236
|
|
237 Iterator<String> keys = child.getAttributes().getKeys();
|
|
238 NodePath newChildPath = path.add(childNum);
|
|
239 while (keys.hasNext()) {
|
|
240 String key = keys.next();
|
|
241 ByteBuffer value = child.getAttributes().get(key);
|
|
242 editor = editor.putAttribute(newChildPath,key,value).b();
|
|
243 }
|
|
244
|
|
245 for (TreeNode grandson : grandsons) {
|
|
246
|
|
247 }
|
|
248 return editor.success().isB();
|
|
249 }
|
|
250
|
4
|
251 private DefaultNodePath createNodePath(String nodePath) {
|
|
252 DefaultNodePath path = new DefaultNodePath();
|
|
253 String[] nums = nodePath.split(",");
|
|
254 for (String num : nums) {
|
|
255 if (num.equals("-1"))
|
|
256 continue;
|
|
257 path = path.add(Integer.parseInt(num));
|
|
258 }
|
|
259 return path;
|
|
260 }
|
|
261 }
|