view src/main/java/ac/jp/u_ryukyu/cr/ie/tatsuki/xmlReader/ReadXmlPutAttributeCreateIndex.java @ 31:7f70341a78bc

all function use index
author one
date Sat, 08 Nov 2014 06:31:14 +0900
parents ed831b2fc156
children 59a7d2cffc86
line wrap: on
line source

package ac.jp.u_ryukyu.cr.ie.tatsuki.xmlReader;

import java.nio.ByteBuffer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
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.Pair;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class ReadXmlPutAttributeCreateIndex extends DefaultHandler {
  private JungleTree tree;
  private JungleTreeEditor editor;
  private NodePath path;
  private String elementName;

  public ReadXmlPutAttributeCreateIndex(JungleTree tree) {
    this.tree = tree;
    this.editor = tree.getIndexTreeEditor();
    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;
    path = path.add(num);
    // System.out.println(path.toString());

    if (uri != "") {
      // System.out.println("namespaceURI= " + uri);
    }

    if (localName != "") {
      // System.out.println("localName= " + localName);
    }

    if (qName != "") {
      // System.out.println("qName= " + qName);
      elementName = qName;
      Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path, "element",
          ByteBuffer.wrap(qName.getBytes()));
      if (newEither.isA()) {
        // error
      }
      this.editor = newEither.b();
    }

    if (attributes.getLength() != 0) {
      for (int count = 0; attributes.getLength() > count; count++) {
        // System.out.println(attributes.getLocalName(count) + " = " +
        // attributes.getValue(count));
        String key = elementName + "-" + 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()) {
          // 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()) {
        if (str.equals("r:63 r:3 r:9"))
          System.out.println("aaa");
        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, "text-" + elementName, ByteBuffer.wrap(splitStr.getBytes()));
        if (newEither.isA()) {
          //error
        }
        this.editor = newEither.b();
        }
    }
  }

  @Override
  public void endElement(String namespaceURI, String localName, String qName) {
    path = path.tail();
    if (namespaceURI != "") {
      // System.out.println("namespaceURI= " + namespaceURI);
    }
    if (localName != "") {
      // System.out.println("localName= " + localName);
    }

    if (qName != "") {
      // System.out.println("qName= " + qName);
    }
  }

  @Override
  public void startDocument() {
    // System.out.println("start");
  }

  @Override
  public void endDocument() {
    Either<Error, JungleTreeEditor> either = editor.success();
    if (either.isA()) {
      // error
    }
    // System.out.println("end");
  }

  public JungleTree getTree() {
    return tree;
  }

}