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