view src/howtouse/CharReader.java @ 12:bf7863a55cd6 draft

add wikigraph.Neo4jTest.java
author one
date Thu, 23 Aug 2012 03:40:48 +0900
parents d655c8fef734
children e30f2714686b
line wrap: on
line source

package howtouse;


public class CharReader {

	final static char EOFchar = (char) 0;

	private String text;
	private int textLength;
	private int index;

	private final char LBRANK = '[';
	private final char RBRANK = ']';
	private final char VERBAR = '|';

	private final static int LETTER = 1;
	private final static int[] charKindT = new int[0x10000];
	static {
		for (int i = 0; i < 'A'; i++)
			charKindT[i] = 0;
		for (int i = 'A'; i <= 'Z'; i++)
			charKindT[i] = LETTER;

	}

	CharReader() {
	}
	
	public void setText(String str) {
		text = str;
		textLength = text.length();
		index = 0;
	}

	
	char nextChar() {
		if (index < textLength)
			return text.charAt(index++);

		return EOFchar;

	}

	String getToken() {

		int nextState = 1;

		StringBuffer buf = new StringBuffer(256);
		char ch;
		int index = -1;
		while (true) {
			ch = nextChar();	
			if (ch == EOFchar) return null;
			switch (nextState) {
			case 1:
				if (ch == LBRANK)
					nextState = 2;
				break;
			case 2:
				if (ch == LBRANK)
					nextState = 3;
				else
					nextState = 1;
				break;
			case 3:
				if (ch == RBRANK) {
					nextState = 4;
				} else if (ch == VERBAR) {
					index = buf.length();
					buf.append(ch);
//					buf.delete(0,buf.length());
				} else {
					buf.append(ch);
				}
				break;
			case 4:
				if (ch == RBRANK) {
					if (index == -1) {
						return buf.toString();
					} else{
						return buf.substring(0,index);
					}
				} else {
					return null;
				}
			}

		}

	}
}