view src/main/java/ac/jp/u_ryukyu/cr/ie/tatsuki/xmlReader/ReadXmlPutAttributeCreateIndex.java @ 26:ef3fae2e7f0c

add ReadXmlNodeCreateIndexJandler and ReadXmlPutAttributeCreate query use Index but 13/15 error
author one
date Tue, 28 Oct 2014 06:34:47 +0900
parents
children 095813055f19
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.*;

import fj.test.reflect.Name;

public class ReadXmlPutAttributeCreateIndex extends DefaultHandler {
  private JungleTree tree;
  private JungleTreeEditor editor;
  private NodePath path;
  
  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);
      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 = 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()) {
        Pair<Integer, NodePath> nodePair = path.last();
        NodePath onePath = nodePair.right();
        //System.out.println(str);
        Either<Error, JungleTreeEditor> newEither = editor.putAttribute(onePath, "text", ByteBuffer.wrap(str.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;
  }

}