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