view src/howtouse/CharReader.java @ 8:d8d0855bcdfd draft

add CharReader.java , WikiLinkParser.java
author one
date Wed, 22 Aug 2012 01:44:26 +0900
parents
children d655c8fef734
line wrap: on
line source

package howtouse;

import java.io.BufferedReader;
import java.io.IOException;

public class CharReader {

	final static char EOFchar = (char) 0;

	private BufferedReader reader;
	private String line;
	private int lineLength;
	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(BufferedReader r) {
		reader = r;
		lineLength = 0;
		index = 1;
	}

	CharReader() {
		lineLength = 0;
		index = 1;
	}
	


	
	char nextChar() {
		if (index < lineLength)
			return line.charAt(index++);

		if (index++ == lineLength)
			return '\n';

		try {
			if ((line = reader.readLine()) != null) {
				lineLength = line.length();
				index = 0;
				return nextChar();
			}
		} catch (IOException e) {
			System.out.println("Cannot read line");

		}
		return EOFchar;

	}

	String getToken() throws IOException {

		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);
				} else {
					buf.append(ch);
				}
				break;
			case 4:
				if (ch == RBRANK) {
					if (index == -1) {
						return buf.toString();
					} else{
						return buf.substring(0,index);
					}
				} else {
					throw new IOException("Faild read string");
				}
			}

		}

	}
}