view src/main/java/ac/jp/u_ryukyu/cr/ie/tatsuki/xmlReader/ReadXmlHandler.java @ 28:ed831b2fc156

temporarily stored
author one
date Fri, 07 Nov 2014 02:20:07 +0900
parents 6f9439ca3eb5
children 037731e99d6e
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 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.getTreeEditor();
		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()) {
			//error
		}
		this.editor = either.b();
		
		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()) {
        Pair<Integer, NodePath> nodePair = path.last();
        NodePath onePath = nodePair.right();
        //System.out.println(str);
        Either<Error, JungleTreeEditor> newEither = editor.putAttribute(onePath, "text-" + elementName, 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;
	}

}