home *** CD-ROM | disk | FTP | other *** search
- package netscape.util;
-
- import java.io.FilterInputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- class TokenGenerator extends FilterInputStream {
- public static final byte NULL_TOKEN = 0;
- public static final byte STRING_TOKEN = 1;
- public static final byte ARRAY_BEGIN_TOKEN = 2;
- public static final byte ARRAY_END_TOKEN = 3;
- public static final byte VECTOR_BEGIN_TOKEN = 4;
- public static final byte VECTOR_END_TOKEN = 5;
- public static final byte HASHTABLE_BEGIN_TOKEN = 6;
- public static final byte HASHTABLE_KEY_VALUE_SEP_TOKEN = 7;
- public static final byte HASHTABLE_KEY_VALUE_END_TOKEN = 8;
- public static final byte HASHTABLE_END_TOKEN = 9;
- public static final byte GENERIC_SEP_TOKEN = 10;
- public static final byte NULL_VALUE_TOKEN = 11;
- public static final byte LAST_TOKEN_TYPE = 11;
- static byte[] tokenToAscii = new byte[12];
- static byte[] asciiToToken;
- static final int CHARACTER_COUNT_PER_ARRAY = 128;
- static final int CCPA_BIT_COUNT = 7;
- static final int CCPA_MASK = 127;
- static final int PARSING_NONE_STATE = 0;
- static final int PARSING_STRING_STATE = 1;
- static final int PARSING_QUOTED_STRING_STATE = 2;
- static final int PARSING_COMMENT_STATE = 3;
- static final int PARSING_C_STYLE_COMMENT_STATE = 4;
- static final int PARSING_C_PLUS_PLUS_STYLE_COMMENT_STATE = 5;
- private byte[][] input = new byte[1][];
- private int nextAvailableByteIndex;
- private int markedByteIndex;
- private int nextFreeByteSlotIndex;
- private byte[] bytesForCurrentToken;
- private int currentToken;
- private int lastToken;
- private int currentLineNumber;
- private boolean previousCharacterWasBackslash = false;
- private boolean starFound = false;
- private int parserState;
-
- public TokenGenerator(InputStream var1) {
- super(var1);
- this.input[0] = new byte[128];
- this.nextAvailableByteIndex = 0;
- this.nextFreeByteSlotIndex = 0;
- this.currentLineNumber = 0;
- this.parserState = 0;
- }
-
- private final void markCurrentCharacter() {
- this.markedByteIndex = this.nextAvailableByteIndex;
- }
-
- private final void markPreviousCharacter() {
- this.markedByteIndex = this.nextAvailableByteIndex - 1;
- }
-
- private final void growInputBuffer() {
- byte[][] var1 = new byte[this.input.length + 1][];
- System.arraycopy(this.input, 0, var1, 0, this.input.length);
- var1[this.input.length] = new byte[128];
- this.input = var1;
- }
-
- private final void readMoreCharacters() throws IOException {
- int var2 = this.nextFreeByteSlotIndex >> 7;
- if (var2 >= this.input.length) {
- this.growInputBuffer();
- }
-
- int var1 = ((FilterInputStream)this).read(this.input[var2], this.nextFreeByteSlotIndex & 127, 128 - (this.nextFreeByteSlotIndex & 127));
- if (var1 != -1) {
- this.nextFreeByteSlotIndex += var1;
- if (((FilterInputStream)this).available() > 0 && var1 < 128) {
- var2 = this.nextFreeByteSlotIndex >> 7;
- if (var2 >= this.input.length) {
- this.growInputBuffer();
- }
-
- var1 = ((FilterInputStream)this).read(this.input[var2], this.nextFreeByteSlotIndex & 127, 128 - (this.nextFreeByteSlotIndex & 127));
- if (var1 != -1) {
- this.nextFreeByteSlotIndex += var1;
- }
- }
-
- }
- }
-
- private final boolean hasMoreCharacters() throws IOException {
- if (this.nextAvailableByteIndex < this.nextFreeByteSlotIndex) {
- return true;
- } else {
- this.readMoreCharacters();
- return this.nextAvailableByteIndex < this.nextFreeByteSlotIndex;
- }
- }
-
- private final byte peekNextCharacter() throws IOException {
- byte var1 = 0;
- if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
- this.readMoreCharacters();
- }
-
- if (this.nextAvailableByteIndex < this.nextFreeByteSlotIndex) {
- var1 = this.input[this.nextAvailableByteIndex >> 7][this.nextAvailableByteIndex & 127];
- ++this.nextAvailableByteIndex;
- }
-
- return var1;
- }
-
- private final void rewindToMarkedCharacter() {
- this.nextAvailableByteIndex = this.markedByteIndex;
- }
-
- private final void deletePeekedCharacters() {
- for(this.markedByteIndex = -1; this.nextAvailableByteIndex >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
- byte[] var1 = this.input[0];
- int var2 = 0;
-
- for(int var3 = this.input.length - 1; var2 < var3; ++var2) {
- this.input[var2] = this.input[var2 + 1];
- }
-
- this.input[this.input.length - 1] = var1;
- this.nextAvailableByteIndex -= 128;
- }
-
- }
-
- private final void deletePeekedCharactersMinusOne() {
- for(this.markedByteIndex = -1; this.nextAvailableByteIndex - 1 >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
- byte[] var1 = this.input[0];
- int var2 = 0;
-
- for(int var3 = this.input.length - 1; var2 < var3; ++var2) {
- this.input[var2] = this.input[var2 + 1];
- }
-
- this.input[this.input.length - 1] = var1;
- this.nextAvailableByteIndex -= 128;
- }
-
- }
-
- private final byte[] getAndDeletePeekedCharacters() {
- int var1 = this.nextAvailableByteIndex - this.markedByteIndex;
- byte[] var2 = new byte[var1];
- int var3 = this.markedByteIndex;
-
- for(int var4 = this.markedByteIndex + var1; var3 < var4; ++var3) {
- var2[var3 - this.markedByteIndex] = this.input[var3 >> 7][var3 & 127];
- }
-
- this.deletePeekedCharacters();
- this.markedByteIndex = -1;
- return var2;
- }
-
- private final byte[] getAndDeletePeekedCharactersMinusOne() {
- int var1 = this.nextAvailableByteIndex - this.markedByteIndex - 1;
- byte[] var2 = new byte[var1];
- int var3 = this.markedByteIndex;
-
- for(int var4 = this.markedByteIndex + var1; var3 < var4; ++var3) {
- var2[var3 - this.markedByteIndex] = this.input[var3 >> 7][var3 & 127];
- }
-
- this.deletePeekedCharactersMinusOne();
- this.markedByteIndex = -1;
- return var2;
- }
-
- private final void parseOneToken() throws DeserializationException, IOException {
- while(this.currentToken == 0) {
- if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
- this.readMoreCharacters();
- if (this.nextAvailableByteIndex >= this.nextFreeByteSlotIndex) {
- break;
- }
- }
-
- if (this.markedByteIndex == -1) {
- this.markedByteIndex = this.nextAvailableByteIndex;
- }
-
- byte var1 = this.input[this.nextAvailableByteIndex >> 7][this.nextAvailableByteIndex & 127];
- ++this.nextAvailableByteIndex;
- if (var1 == 10) {
- ++this.currentLineNumber;
- }
-
- if (this.parserState == 2) {
- if (!this.previousCharacterWasBackslash && var1 == 34) {
- this.currentToken = 1;
- this.bytesForCurrentToken = this.getAndDeletePeekedCharacters();
- this.parserState = 0;
- this.markedByteIndex = this.nextAvailableByteIndex;
- this.previousCharacterWasBackslash = false;
- } else if (var1 == 92) {
- this.previousCharacterWasBackslash = true;
- } else {
- this.previousCharacterWasBackslash = false;
- }
- } else {
- byte var2;
- if (var1 >= 0 && var1 < 127) {
- var2 = asciiToToken[var1];
- } else {
- var2 = 0;
- }
-
- if (this.parserState == 1) {
- if (var1 == 34 || var2 != 1) {
- this.currentToken = 1;
- this.bytesForCurrentToken = this.getAndDeletePeekedCharactersMinusOne();
- this.parserState = 0;
- this.markedByteIndex = this.nextAvailableByteIndex - 1;
- this.rewindToMarkedCharacter();
- }
- } else if (this.parserState == 3) {
- if (var1 == 42) {
- this.parserState = 4;
- } else {
- if (var1 != 47) {
- throw new DeserializationException("Syntax error at line " + this.lineForLastToken(), this.lineForLastToken());
- }
-
- this.parserState = 5;
- }
- } else if (this.parserState == 4) {
- if (this.starFound && var1 == 47) {
- this.starFound = false;
- this.parserState = 0;
- } else if (var1 == 42) {
- this.starFound = true;
- } else {
- this.starFound = false;
- }
- } else if (this.parserState == 5) {
- if (var1 == 10) {
- this.parserState = 0;
- }
- } else if (var1 == 47) {
- this.parserState = 3;
- } else if (var2 != 0) {
- if (var2 == 1) {
- if (var1 == 34) {
- this.parserState = 2;
- } else {
- this.parserState = 1;
- }
-
- this.deletePeekedCharactersMinusOne();
- this.markedByteIndex = this.nextAvailableByteIndex - 1;
- } else {
- this.currentToken = var2;
- this.bytesForCurrentToken = null;
-
- for(this.markedByteIndex = -1; this.nextAvailableByteIndex >> 7 > 0; this.nextFreeByteSlotIndex -= 128) {
- byte[] var3 = this.input[0];
- int var4 = 0;
-
- for(int var5 = this.input.length - 1; var4 < var5; ++var4) {
- this.input[var4] = this.input[var4 + 1];
- }
-
- this.input[this.input.length - 1] = var3;
- this.nextAvailableByteIndex -= 128;
- }
-
- this.markedByteIndex = this.nextAvailableByteIndex;
- }
- }
- }
- }
-
- if (this.currentToken == 0 && !this.hasMoreCharacters()) {
- switch (this.parserState) {
- case 0:
- default:
- break;
- case 1:
- this.currentToken = 1;
- this.bytesForCurrentToken = this.getAndDeletePeekedCharacters();
- this.parserState = 0;
- this.previousCharacterWasBackslash = false;
- return;
- case 2:
- this.parserState = 0;
- this.previousCharacterWasBackslash = false;
- throw new DeserializationException("Unterminated string at line " + this.lineForLastToken(), this.lineForLastToken());
- case 3:
- this.parserState = 0;
- throw new DeserializationException("Syntax error at line " + this.lineForLastToken(), this.lineForLastToken());
- case 4:
- this.parserState = 0;
- this.starFound = false;
- throw new DeserializationException("Unterminated comment at line " + this.lineForLastToken(), this.lineForLastToken());
- case 5:
- this.parserState = 0;
- return;
- }
- }
-
- }
-
- public final boolean hasMoreTokens() throws DeserializationException, IOException {
- if (this.currentToken != 0) {
- return true;
- } else {
- this.parseOneToken();
- return this.currentToken != 0;
- }
- }
-
- public final int nextToken() throws DeserializationException, IOException {
- int var1 = 0;
- if (this.currentToken == 0) {
- this.parseOneToken();
- }
-
- if (this.currentToken != 0) {
- var1 = this.currentToken;
- this.lastToken = this.currentToken;
- this.currentToken = 0;
- }
-
- return var1;
- }
-
- public final int peekNextToken() throws DeserializationException, IOException {
- this.hasMoreTokens();
- this.lastToken = this.currentToken;
- return this.currentToken;
- }
-
- public final byte[] bytesForLastToken() {
- if (this.lastToken == 1) {
- return this.bytesForCurrentToken;
- } else {
- byte[] var1 = new byte[]{tokenToAscii[this.lastToken]};
- return var1;
- }
- }
-
- public byte byteForLastToken() {
- return tokenToAscii[this.lastToken];
- }
-
- public int lineForLastToken() {
- return this.currentLineNumber + 1;
- }
-
- static {
- tokenToAscii[0] = 0;
- tokenToAscii[1] = 0;
- tokenToAscii[2] = 91;
- tokenToAscii[3] = 93;
- tokenToAscii[4] = 40;
- tokenToAscii[5] = 41;
- tokenToAscii[6] = 123;
- tokenToAscii[7] = 61;
- tokenToAscii[8] = 59;
- tokenToAscii[9] = 125;
- tokenToAscii[10] = 44;
- tokenToAscii[11] = 64;
- asciiToToken = new byte[127];
-
- for(int var0 = 0; var0 <= 32; ++var0) {
- asciiToToken[var0] = 0;
- }
-
- for(int var1 = 33; var1 < 127; ++var1) {
- asciiToToken[var1] = 1;
- }
-
- asciiToToken[91] = 2;
- asciiToToken[93] = 3;
- asciiToToken[40] = 4;
- asciiToToken[41] = 5;
- asciiToToken[123] = 6;
- asciiToToken[61] = 7;
- asciiToToken[59] = 8;
- asciiToToken[125] = 9;
- asciiToToken[44] = 10;
- asciiToToken[64] = 11;
- asciiToToken[47] = 0;
- }
- }
-