39
|
1 package jungle.test.bbs;
|
|
2
|
|
3 import java.nio.ByteBuffer;
|
15
|
4
|
31
|
5 import alice.jungle.core.NetworkDefaultJungle;
|
20
|
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
|
15
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
|
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
|
39
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
15
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
|
39
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
15
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
|
39
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
15
|
19
|
|
20 public class JungleManager {
|
45
|
21 private static JungleManager jm = new JungleManager();
|
43
|
22 private Jungle jungle;
|
|
23
|
45
|
24 private JungleManager() {
|
15
|
25
|
|
26 }
|
|
27
|
45
|
28 public static void setJungle(Jungle _j) {
|
|
29 jm.jungle = _j;
|
15
|
30 }
|
|
31
|
43
|
32 public static Jungle getJungle() {
|
|
33 return jm.jungle;
|
|
34 }
|
|
35
|
|
36 public static JungleTree createNewTree(String name) {
|
|
37 return jm.jungle.createNewTree(name);
|
15
|
38 }
|
|
39
|
|
40
|
43
|
41 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) {
|
39
|
42 JungleTreeEditor editor = _editor;
|
|
43 Either<Error, JungleTreeEditor> either = null;
|
|
44 for (TreeOperation op : _log) {
|
|
45 either = _edit(editor, op);
|
|
46 if(either.isA()) {
|
|
47 return either;
|
|
48 }
|
|
49 editor = either.b();
|
|
50 }
|
|
51 return either;
|
|
52 }
|
|
53
|
43
|
54 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
|
39
|
55 TreeOperation op) {
|
|
56 NodePath path = op.getNodePath();
|
|
57 NodeOperation nodeOp = op.getNodeOperation();
|
|
58 Command c = nodeOp.getCommand();
|
|
59 String key = "";
|
|
60 switch (c) {
|
|
61 case PUT_ATTRIBUTE:
|
|
62 key = nodeOp.getKey();
|
|
63 ByteBuffer value = nodeOp.getValue();
|
|
64 return editor.putAttribute(path, key, value);
|
|
65 case DELETE_ATTRIBUTE:
|
|
66 key = nodeOp.getKey();
|
|
67 return editor.deleteAttribute(path, key);
|
|
68 case APPEND_CHILD:
|
|
69 return editor.addNewChildAt(path, 0);
|
|
70 case DELETE_CHILD:
|
|
71 return editor.deleteChildAt(path, 0);
|
|
72 }
|
|
73 return null;
|
|
74 }
|
15
|
75 }
|