Mercurial > hg > Members > tatsuki > JungleXMLReader
view src/main/java/jp/ac/u_ryukyu/cr/ie/tatsuki/bbs/JungleBulletinBoard.java @ 39:22edfb7f47ad
bbs show all node attribute
author | one |
---|---|
date | Tue, 11 Nov 2014 19:01:36 +0900 |
parents | b8d86bc46b51 |
children | 037731e99d6e c036d36c33af |
line wrap: on
line source
package jp.ac.u_ryukyu.cr.ie.tatsuki.bbs; import java.io.FileInputStream; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicInteger; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import ac.jp.u_ryukyu.cr.ie.tatsuki.xmlReader.ReadXmlHandler; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.bbs.BoardMessage; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultOperationLog; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultEvaluator; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.Traversal; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; import junit.framework.Assert; public class JungleBulletinBoard implements XmlBulletinBoard { private final Jungle jungle; public JungleBulletinBoard() { jungle = new DefaultJungle(null, "hoge", new DefaultTreeEditor(new DefaultTraverser())); jungle.createNewTree("boards"); } 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"); System.out.println(new String(e.array())); return new String(e.array()); } }; return new IterableConverter<String, TreeNode>(chs, converter); } public void createBoards(final String _name) { if (null == jungle.createNewTree(_name)) { 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 = either.b().success(); if (either.isA()) { throw new IllegalStateException(); } } public void createFolder(final String _board, final String _author, final String _message, final String _editKey, String _nodeNum) { JungleTree tree = jungle.getTreeByName(_board); if (tree == null) { throw new IllegalStateException(); } DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); } } catch (Exception _e) { } Either<Error, JungleTreeEditor> either; final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); do { TreeNode node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); // TraversableNodeWrapper<Node> traversable = new // TraversableNodeWrapper<Node>(node); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal> ret = traverser.traverse(node, evaluator); if (ret.isA()) { Assert.fail(); } Traversal traversal = ret.b(); TreeNode target = traversal.destination(); int size = target.getChildren().size(); JungleTreeEditor editor = tree.getTreeEditor(); either = editor.addNewChildAt(path, size); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; path = path.add(size); either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void createBoardMessage(final String _board, final String _author, final String _message, final String _editKey) { JungleTree tree = jungle.getTreeByName(_board); if (tree == null) { throw new IllegalStateException(); } Either<Error, JungleTreeEditor> either; final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); do { TreeNode node = tree.getRootNode(); int size = node.getChildren().size(); DefaultNodePath path = new DefaultNodePath(); JungleTreeEditor editor = tree.getTreeEditor(); either = editor.addNewChildAt(path, size); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; path = path.add(size); either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editMessage(String _board, String _nodeNum,final String _author, final String _message, final String _editKey) { final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; do { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); System.out.println(new String(node.getAttributes().get("mes").array())); logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp", tBuffer).b(); System.out.println(new String(node.getAttributes().get("mes").array())); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void createAttribute(String _board, String _uuid, final String _author, final String _message, final String _editKey) { final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _uuid.substring(count, count + 1) != null; count++) { if (!_uuid.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_uuid.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { String str; public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); str = "0"; int count = 0; for (; logNode.getAttributes().get("mes" + String.valueOf(count)) != null; count++) { } str = String.valueOf(count); logNode = logNode.getAttributes().put("mes" + str,ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp" + str, tBuffer).b(); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editAttribute(String boardName, String _path, final String id, final String _message) { final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(boardName); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _path.substring(count, count + 1) != null; count++) { if (!_path.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_path.substring(count, count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); // EnableNodeWrapper<T> node = _e.getWrap(); logNode = logNode.getAttributes().put("mes" + id,ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp" + id, tBuffer).b(); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void deleteNode(String _board, String _path, String _id) { int id = Integer.parseInt(_id); final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _path.substring(count, count + 1) != null; count++) { if (!_path.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_path.substring(count,count + 1))); } } catch (Exception _e) { } JungleTreeEditor editor = tree.getTreeEditor(); either = editor.deleteChildAt(path, id); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void deleteAttribute(String _board, String _path, final String id) { final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; DefaultNodePath path = new DefaultNodePath(); do { try { for (int count = 0; _path.substring(count, count + 1) != null; count++) { if (!_path.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_path.substring(count, count + 1))); } } catch (Exception _e) { System.out.println("屑"); } JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); logNode = logNode.getAttributes().delete("mes" + id).b(); logNode = logNode.getAttributes().delete("timestamp" + id).b(); int count = Integer.parseInt(id); for (; logNode.getAttributes().get("mes" + String.valueOf(count + 1)) != null;) { logNode = logNode.getAttributes().put("mes" + count,node.getAttributes().get("mes"+ String.valueOf(count + 1))).b(); logNode = logNode.getAttributes().put("timestamp" + count, tBuffer).b(); count++; } if (count != Integer.parseInt(id)) { logNode = logNode.getAttributes().delete("timestamp" + count).b(); logNode = logNode.getAttributes().delete("mes" + count).b(); } return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public void editMatrixMessage(String _board, String _uuid, final String _author, final String _message, final String _editKey) { final long timestamp = System.currentTimeMillis(); final ByteBuffer tBuffer = ByteBuffer.allocate(16); tBuffer.putLong(timestamp); JungleTree tree = jungle.getTreeByName(_board); Either<Error, JungleTreeEditor> either = null; do { DefaultNodePath path = new DefaultNodePath(); path = path.add(Integer.parseInt(_uuid)); JungleTreeEditor editor = tree.getTreeEditor(); NodeEditor e = new NodeEditor() { public Either<Error,LoggingNode> edit(TreeNode node) { LoggingNode logNode = wrap(node , new DefaultOperationLog()); logNode = logNode.getAttributes().put("author", ByteBuffer.wrap(_author.getBytes())).b(); logNode = logNode.getAttributes().put("mes", ByteBuffer.wrap(_message.getBytes())).b(); logNode = logNode.getAttributes().put("key", ByteBuffer.wrap(_editKey.getBytes())).b(); logNode = logNode.getAttributes().put("timestamp", tBuffer).b(); return DefaultEither.newB(logNode); } @Override public LoggingNode wrap(TreeNode node, OperationLog op) { return new LoggingNode(node, op); } }; either = editor.edit(path, e); if (either.isA()) { throw new IllegalStateException(); } editor = either.b(); either = editor.success(); } while (either.isA()); } public Iterable<BoardMessage> getMessages(String _boardName) { JungleTree tree = jungle.getTreeByName(_boardName); TreeNode node = tree.getRootNode(); Children chs = node.getChildren(); final AtomicInteger counter = new AtomicInteger(0); IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() { public BoardMessage conv(TreeNode _b) { String uuid = Integer.toString(counter.get()); String author = new String(_b.getAttributes().get("author").array()); String message = new String(_b.getAttributes().get("mes").array()); counter.incrementAndGet(); return new BoardMessageImpl(author, message, uuid); } }; return new IterableConverter<BoardMessage, TreeNode>(chs, converter); } public Iterable<BoardMessage> getFolder(String _boardName, String _nodeNum) { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count,count + 1))); } } catch (Exception _e) { } JungleTree tree = jungle.getTreeByName(_boardName); TreeNode node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal> ret = traverser.traverse(node,evaluator); if (ret.isA()) { Assert.fail(); } Traversal traversal = ret.b(); TreeNode target = traversal.destination(); Children chs = target.getChildren(); final AtomicInteger counter = new AtomicInteger(0); IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() { public BoardMessage conv(TreeNode _b) { String uuid = Integer.toString(counter.get()); String message = new String(_b.getAttributes().get("element").array()); counter.incrementAndGet(); return new BoardMessageImpl(null, message, uuid); } }; return new IterableConverter<BoardMessage, TreeNode>(chs, converter); } public GetAttributeImp getAttribute(String _boardName, String _nodeNum) { DefaultNodePath path = new DefaultNodePath(); try { for (int count = 0; _nodeNum.substring(count, count + 1) != null; count++) { if (!_nodeNum.substring(count, count + 1).equals("/")) path = path.add(Integer.parseInt(_nodeNum.substring(count, count + 1))); } } catch (Exception _e) { } JungleTree tree = jungle.getTreeByName(_boardName); TreeNode node = tree.getRootNode(); DefaultTraverser traverser = new DefaultTraverser(); DefaultEvaluator evaluator = new DefaultEvaluator(path); Either<Error, Traversal> ret = traverser.traverse(node,evaluator); if (ret.isA()) { Assert.fail(); } Traversal traversal = ret.b(); TreeNode target = traversal.destination(); return new GetAttributeImp(target); } private static class BoardMessageImpl implements BoardMessage { private final String author; private final String message; private final String uuid; public BoardMessageImpl(String _author, String _message, String _uuid) { author = _author; message = _message; uuid = _uuid; } public String getAuthor() { return author; } public String getMessage() { return message; } public String getUUID() { return uuid; } } public void readXml(String xmlPath){ try { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); createBoards(xmlPath); JungleTree tree = jungle.getTreeByName(xmlPath); ReadXmlHandler readXmlHandler = new ReadXmlHandler(tree); saxParser.parse(new FileInputStream("xml/" + xmlPath), readXmlHandler); } catch (Exception e) { e.printStackTrace(); } } 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; } @Override public void createBoards(String _name, String _author, String _initMessage, String _editKey) { } }