Mercurial > hg > Members > tatsuki > bbs
view src/main/java/jp/ac/u_ryukyu/ie/cr/bbs/browsing/JungleBrowsingBulletinBoard.java @ 6:36f0f18ce6a8
change layout import method
author | tatsuki |
---|---|
date | Tue, 11 Oct 2016 07:15:09 +0900 |
parents | 2b3542c5be34 |
children | 6c584862fe93 |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.bbs.browsing; import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.NulIterator; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNodeChildren; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; import plparser.Property; import plparser.jungle.PropertyJungleTreeFactoryImpl; import plparser.jungle.PropertyListJungleNodeFactory; import plparser.jungle.PropertyListParserCreateTree; import java.nio.ByteBuffer; import java.util.Iterator; public class JungleBrowsingBulletinBoard implements BrowsingBulletinBoard { private final Jungle jungle; public JungleBrowsingBulletinBoard() { jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor( new DefaultTraverser())); jungle.createNewTree("boards"); } public JungleBrowsingBulletinBoard(Jungle jungle) { this.jungle = jungle; } public Iterable<String> getBoards() { JungleTree tree = jungle.getTreeByName("boards"); TreeNode node = tree.getRootNode(); Children chs = node.getChildren(); IterableConverter.Converter<String, TreeNode> converter = new IterableConverter.Converter<String, TreeNode>() { public String conv(TreeNode _b) { ByteBuffer e = _b.getAttributes().get("name"); return new String(e.array()); } }; return new IterableConverter<String, TreeNode>(chs, converter); } public void createBoards(final String _name) { createBoards(_name, new DefaultTreeNode()); } public void createBoards(final String _name, TreeNode rootNode) { if (null == jungle.createNewTree(_name, rootNode)) { throw new IllegalStateException(); } JungleTree tree = jungle.getTreeByName("boards"); JungleTreeEditor editor = tree.getTreeEditor(); DefaultNodePath root = new DefaultNodePath(); Either<Error, JungleTreeEditor> either = editor.addNewChildAt(root, 0); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.putAttribute(root.add(0), "name", ByteBuffer.wrap(_name.getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); Either<Error, JungleTreeEditor> result = editor.success(); if (result.isA()) { throw new IllegalStateException(); } tree = jungle.getTreeByName(_name); editor = tree.getTreeEditor(); either = editor.putAttribute(root, "NodeName", ByteBuffer.wrap("root".getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); editor.success(); } public void createBoardMessage(final String boardName, final String key, final String attribute, final String pathStr) { if (key.equals("") | attribute.equals("")) return ; NodePath path = createNodePath(pathStr); JungleTree tree = jungle.getTreeByName(boardName); if (tree == null) { throw new IllegalStateException(); } JungleTreeEditor editor; do { editor = tree.getTreeEditor(); Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); } while (editor.success().isA()); } @Override public void createChild(String bname, String nodeName, String pathStr) { NodePath path = createNodePath(pathStr); JungleTree tree = jungle.getTreeByName(bname); if (tree == null) { throw new IllegalStateException(); } Either<Error,TreeNode> getNodeEither = tree.getNodeOfPath(path); if (getNodeEither.isA()) return ; TreeNode currentNode = getNodeEither.b(); int childCount = currentNode.getChildren().size(); JungleTreeEditor editor; do { editor = tree.getTreeEditor(); Either<Error,JungleTreeEditor> either = editor.addNewChildAt(path, childCount); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.putAttribute(path.add(childCount),"NodeName",ByteBuffer.wrap(nodeName.getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); } while (editor.success().isA()); } public void editMessage(String bname, String key, final String attribute, final String pathStr) { NodePath path = createNodePath(pathStr); JungleTreeEditor editor = null; do { JungleTree tree = jungle.getTreeByName(bname); editor = tree.getTreeEditor(); Either<Error, JungleTreeEditor> either = editor.putAttribute(path, key, ByteBuffer.wrap(attribute.getBytes())); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); } while (editor.success().isA()); } public String sanitize(String str) { if (str == null) { return str; } str = str.replaceAll("&", "&"); str = str.replaceAll("<", "<"); str = str.replaceAll(">", ">"); str = str.replaceAll("\"", """); str = str.replaceAll("'", "'"); return str; } public GetAttributeImp getAttribute(String _bname, String nodePath) { DefaultNodePath path = createNodePath(nodePath); JungleTree tree = jungle.getTreeByName(_bname); Either<Error, TreeNode> either = tree.getNodeOfPath(path); if (either.isA()) return new GetAttributeImp(new DefaultTreeNode()); TreeNode currentNode = either.b(); return new GetAttributeImp(currentNode); } @Override public Iterator<TreeNode> getChildren(String bname, String nodePath) { DefaultNodePath path = createNodePath(nodePath); JungleTree tree = jungle.getTreeByName(bname); Either<Error, TreeNode> either = tree.getNodeOfPath(path); if (either.isA()) return new NulIterator<TreeNode>(); TreeNode currentNode = either.b(); TreeNodeChildren children = currentNode.getChildren(); return children.iterator(); } @Override public void importLayout(String boardName, String path) { PropertyListParserCreateTree<Property> jp; PropertyListJungleNodeFactory jlf = new PropertyJungleTreeFactoryImpl(); jp = new PropertyListParserCreateTree<Property>(jlf); TreeNode root = jp.parseFile(path); createBoards(boardName, root); } @Override public Iterator<TreeNode> findNode(String bname, String key, String value) { JungleTree tree = jungle.getTreeByName(bname); InterfaceTraverser traverser = tree.getTraverser(true); return traverser.find((TreeNode node) -> { return true; },key,value); } @Override public String getNodePath(String bname, TreeNode node) { JungleTree tree = jungle.getTreeByName(bname); NodePath path = tree.getNodePath(node); return path.toString(); } @Override public boolean childMove(String bname, String pathString, String childNumString, String move) { int childNum = Integer.parseInt(childNumString); JungleTree tree = jungle.getTreeByName(bname); DefaultNodePath path = createNodePath(pathString); JungleTreeEditor editor = tree.getTreeEditor(); editor = editor.moveChild(path,childNum,move).b(); return editor.success().isB(); } private DefaultNodePath createNodePath(String nodePath) { DefaultNodePath path = new DefaultNodePath(); String[] nums = nodePath.split(","); for (String num : nums) { if (num.equals("-1")) continue; path = path.add(Integer.parseInt(num)); } return path; } }