Mercurial > hg > Members > shoshi > jungle > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/xml/reader/ReadXmlHandler.java @ 329:2a0cb1f0ba4e
rename Error package
author | kono |
---|---|
date | Sat, 08 Jul 2017 21:05:55 +0900 |
parents | c62462c28807 |
children |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.jungle.xml.reader; import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.jungleError.Error; import jp.ac.u_ryukyu.ie.cr.jungle.util.Pair; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import java.nio.ByteBuffer; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReadXmlHandler extends DefaultHandler { private JungleTree tree; private JungleTreeEditor editor; private NodePath path; private String elementName; public ReadXmlHandler(JungleTree tree) { this.tree = tree; this.editor = tree.getJungleTreeEditor(); this.path = new DefaultNodePath().add(-1); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) { Pair<Integer, NodePath> nodePair = path.last(); path = nodePair.right(); int num = nodePair.left() + 1; Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, num); if (either.isA()) { System.out.println("error"); } this.editor = either.b(); path = path.add(num); if (!qName.equals("")) { elementName = qName; Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path, "element", ByteBuffer.wrap(qName.getBytes())); if (newEither.isA()) { System.out.println("error"); } this.editor = newEither.b(); } if (attributes.getLength() != 0) { for (int count = 0; attributes.getLength() > count; count++) { String key = attributes.getLocalName(count); String value = attributes.getValue(count); ByteBuffer bValue = ByteBuffer.wrap(value.getBytes()); Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path, key, bValue); if (newEither.isA()) { System.out.println("error"); } this.editor = newEither.b(); } } path = path.add(-1); } @Override public void characters(char[] ch, int start, int length) { String str = new String(ch, start, length); Pattern pattern = Pattern.compile("\n"); Matcher macher = pattern.matcher(str); if (!macher.find()) { String[] splitStrs = str.split(" "); for (String splitStr : splitStrs) { Pair<Integer, NodePath> nodePair = path.last(); NodePath onePath = nodePair.right(); Either<Error, JungleTreeEditor> newEither = editor.putAttribute(onePath,elementName, ByteBuffer.wrap(splitStr.getBytes())); if (newEither.isA()) { System.out.println("error"); } this.editor = newEither.b(); } } } @Override public void endElement(String namespaceURI, String localName, String qName) { path = path.tail(); } @Override public void endDocument() { Either<Error, JungleTreeEditor> either = editor.success(); if (either.isA()) { System.out.println("error"); } } public JungleTree getTree() { return tree; } }