view src/test/java/jp/ac/u_ryukyu/cr/ie/tatsuki/xmlReadTest/TestHandler.java @ 0:faedeec97605

read Jungle xml
author tatsuki
date Fri, 24 Oct 2014 07:29:40 +0900
parents
children
line wrap: on
line source

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

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.*;

class TestHandler extends DefaultHandler {
	private JungleTree tree;
	private JungleTreeEditor editor;
	private NodePath path;
	
	public TestHandler(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);
			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));
				Either<Error, JungleTreeEditor> newEither = editor.putAttribute(path,attributes.getLocalName(count) , ByteBuffer.wrap(attributes.getValue(count).getBytes()));
				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();
				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;
	}

}