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;
|
|
17 import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
|
|
18 import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
|
|
19
|
|
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
|
|
33 public Iterable<String> getBoards() {
|
|
34 JungleTree tree = jungle.getTreeByName("boards");
|
|
35 TreeNode node = tree.getRootNode();
|
|
36 Children chs = node.getChildren();
|
|
37
|
|
38 IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() {
|
|
39 public String conv(TreeNode _b) {
|
|
40 ByteBuffer e = _b.getAttributes().get("name");
|
|
41 return new String(e.array());
|
|
42 }
|
|
43 };
|
|
44
|
|
45 return new IterableConverter<String, TreeNode>(chs, converter);
|
|
46 }
|
|
47
|
|
48 public void createBoards(final String _name) {
|
|
49 if (null == jungle.createNewTree(_name)) {
|
|
50 throw new IllegalStateException();
|
|
51 }
|
|
52 JungleTree tree = jungle.getTreeByName("boards");
|
|
53 JungleTreeEditor editor = tree.getTreeEditor();
|
|
54 DefaultNodePath root = new DefaultNodePath();
|
|
55 Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0);
|
|
56 if (either.isA()) {
|
|
57 throw new IllegalStateException();
|
|
58 }
|
|
59 editor = either.b();
|
|
60
|
|
61 either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes()));
|
|
62 if (either.isA()) {
|
|
63 throw new IllegalStateException();
|
|
64 }
|
|
65 editor = either.b();
|
|
66 Either<Error, JungleTreeEditor> result = editor.success();
|
|
67 if (result.isA()) {
|
|
68 throw new IllegalStateException();
|
|
69 }
|
|
70
|
|
71 tree = jungle.getTreeByName(_name);
|
|
72 editor = tree.getTreeEditor();
|
|
73 either = editor.putAttribute(root, "NodeName", ByteBuffer.wrap("root".getBytes()));
|
|
74 if (either.isA()) {
|
|
75 throw new IllegalStateException();
|
|
76 }
|
|
77 editor = either.b();
|
|
78 editor.success();
|
|
79 }
|
|
80
|
|
81 public void createBoardMessage(final String boardName, final String key,
|
|
82 final String attribute, final String pathStr) {
|
|
83 if (key.equals("") | attribute.equals(""))
|
|
84 return ;
|
|
85 NodePath path = createNodePath(pathStr);
|
|
86 JungleTree tree = jungle.getTreeByName(boardName);
|
|
87 if (tree == null) {
|
|
88 throw new IllegalStateException();
|
|
89 }
|
|
90
|
|
91 JungleTreeEditor editor;
|
|
92 do {
|
|
93 editor = tree.getTreeEditor();
|
|
94 Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));
|
|
95 if (either.isA()) {
|
|
96 throw new IllegalStateException();
|
|
97 }
|
|
98 editor = either.b();
|
|
99 } while (editor.success().isA());
|
|
100 }
|
|
101
|
|
102 @Override
|
|
103 public void createChild(String bname, String nodeName, String pathStr) {
|
|
104 NodePath path = createNodePath(pathStr);
|
|
105 JungleTree tree = jungle.getTreeByName(bname);
|
|
106 if (tree == null) {
|
|
107 throw new IllegalStateException();
|
|
108 }
|
|
109 Either<Error,TreeNode> getNodeEither = tree.getNodeOfPath(path);
|
|
110 if (getNodeEither.isA())
|
|
111 return ;
|
|
112 TreeNode currentNode = getNodeEither.b();
|
|
113 int childCount = currentNode.getChildren().size();
|
|
114
|
|
115 JungleTreeEditor editor;
|
|
116 do {
|
|
117 editor = tree.getTreeEditor();
|
|
118 Either<Error,JungleTreeEditor> either = editor.addNewChildAt(path, childCount);
|
|
119 if (either.isA()) {
|
|
120 throw new IllegalStateException();
|
|
121 }
|
|
122 editor = either.b();
|
|
123
|
|
124 either = editor.putAttribute(path.add(childCount),"NodeName",ByteBuffer.wrap(nodeName.getBytes()));
|
|
125 if (either.isA()) {
|
|
126 throw new IllegalStateException();
|
|
127 }
|
|
128 editor = either.b();
|
|
129
|
|
130 } while (editor.success().isA());
|
|
131 }
|
|
132
|
|
133 public void editMessage(String bname, String key, final String attribute,
|
|
134 final String pathStr) {
|
|
135 NodePath path = createNodePath(pathStr);
|
|
136
|
|
137 JungleTreeEditor editor = null;
|
|
138
|
|
139 do {
|
|
140 JungleTree tree = jungle.getTreeByName(bname);
|
|
141 editor = tree.getTreeEditor();
|
|
142
|
|
143 Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes()));
|
|
144
|
|
145 if (either.isA()) {
|
|
146 throw new IllegalStateException();
|
|
147 }
|
|
148 editor = either.b();
|
|
149 } while (editor.success().isA());
|
|
150 }
|
|
151
|
|
152 public String sanitize(String str) {
|
|
153 if (str == null) {
|
|
154 return str;
|
|
155 }
|
|
156 str = str.replaceAll("&", "&");
|
|
157 str = str.replaceAll("<", "<");
|
|
158 str = str.replaceAll(">", ">");
|
|
159 str = str.replaceAll("\"", """);
|
|
160 str = str.replaceAll("'", "'");
|
|
161 return str;
|
|
162 }
|
|
163
|
|
164 public GetAttributeImp getAttribute(String _bname, String nodePath) {
|
|
165
|
|
166 DefaultNodePath path = createNodePath(nodePath);
|
|
167 JungleTree tree = jungle.getTreeByName(_bname);
|
|
168 Either<Error, TreeNode> either = tree.getNodeOfPath(path);
|
|
169 if (either.isA())
|
|
170 return new GetAttributeImp(new DefaultTreeNode());
|
|
171 TreeNode currentNode = either.b();
|
|
172 return new GetAttributeImp(currentNode);
|
|
173 }
|
|
174
|
|
175 @Override
|
|
176 public Iterator<TreeNode> getChildren(String bname, String nodePath) {
|
|
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 NulIterator<TreeNode>();
|
|
182 TreeNode currentNode = either.b();
|
|
183 TreeNodeChildren children = currentNode.getChildren();
|
|
184 return children.iterator();
|
|
185 }
|
|
186
|
|
187 private DefaultNodePath createNodePath(String nodePath) {
|
|
188 DefaultNodePath path = new DefaultNodePath();
|
|
189 String[] nums = nodePath.split(",");
|
|
190 for (String num : nums) {
|
|
191 if (num.equals("-1"))
|
|
192 continue;
|
|
193 path = path.add(Integer.parseInt(num));
|
|
194 }
|
|
195 return path;
|
|
196 }
|
|
197 }
|