//(c):Benja Fallenstein package gzz.slices; import java.io.*; import java.util.*; public class YAMLVersionReader { protected PushbackInputStream in; protected int line = 1; protected int realIndent; protected int expectedIndent = -1; public YAMLVersionReader(InputStream in) throws IOException { this.in = new PushbackInputStream(in); readSeparator(); readIndent(); } public void check(boolean what) throws IOException { if(!what) throw new IOException("Parse exception in line "+line); } public void startBlock() { expectedIndent++; } public boolean inBlock() throws IOException { check(realIndent <= expectedIndent); //pa("inBlock indent: "+realIndent); if(realIndent == expectedIndent) return true; else { expectedIndent--; return false; } } /** Read a colon character and the end of the line. * Throw an exception if something else is read. */ public void readColon() throws IOException { check(in.read() == ':'); readNewline(); } /** Read a comma and a space following it. * Throw an exception if something else is read. */ public void readComma() throws IOException { check(in.read() == ',' && in.read() == ' '); } /** Throw an exception if we're not at the end of the file. */ public void readEndOfFile() throws IOException { check(in.read() < 0); } /** Read a sequence of letters ([a-zA-Z]). * Any character other than a letter ends the sequence. * (After reading such a character, it is pushed back * into the input stream.) */ public String readTag() throws IOException { StringBuffer buf = new StringBuffer(); int b = in.read(); while((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) { buf.append((char)b); b = in.read(); } if(b >= 0) in.unread(b); return buf.toString(); } /** Read a double-quote-delimited string. * Cannot handle escaping of double quotes... */ public String readString() throws IOException { StringBuffer buf = new StringBuffer(); check(in.read() == '"'); int b = in.read(); while(b != '"') { buf.append((char)b); b = in.read(); } return buf.toString(); } /** Read an integer represented as a decimal number. * Signs are not handled. Any character that is not * in the range [0-9] terminates the int and is * pushed back into the input stream. */ public int readInt() throws IOException { int result = 0; int b = in.read(); while(b >= '0' && b <= '9') { result *= 10; result += (b - '0'); b = in.read(); } if(b >= 0) in.unread(b); return result; } public void startList() throws IOException { check(in.read() == '-' && in.read() == ' ' && in.read() == '['); } public void endList() throws IOException { check(in.read() == ']'); readNewline(); } public void readNewline() throws IOException { check(in.read() == '\n'); readIndent(); line++; } public void readIndent() throws IOException { realIndent = 0; int b = in.read(); if(b < 0) { realIndent = -1; return; } while(b == ' ') { check(in.read() == ' '); realIndent++; b = in.read(); } //pa("readIndent: "+realIndent); if(b >= 0) in.unread(b); else throw new IOException("Unexpected end of file"); } public void readSeparator() throws IOException { for(int i=0; i<3; i++) check(in.read() == '-'); check(in.read() == '\n'); } }