home *** CD-ROM | disk | FTP | other *** search
- package netscape.application;
-
- import java.io.FilterInputStream;
- import java.io.IOException;
- import java.io.InputStream;
-
- class HTMLTokenGenerator extends FilterInputStream {
- public static final byte NULL_TOKEN = 0;
- public static final byte STRING_TOKEN = 1;
- public static final byte MARKER_BEGIN_TOKEN = 2;
- public static final byte MARKER_END_TOKEN = 3;
- public static final byte COMMENT_TOKEN = 4;
- static final byte LAST_TOKEN_TYPE = 4;
- 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_MARKER_STATE = 2;
- static final int PARSING_COMMENT_STATE = 3;
- static final int PARSING_MARKER_OR_COMMENT_STATE = 4;
- static final int PARSING_END_COMMENT_ONE_STATE = 5;
- static final int PARSING_END_COMMENT_TWO_STATE = 6;
- private byte[][] input = new byte[1][];
- private int nextAvailableByteIndex;
- private int markedByteIndex;
- private int nextFreeByteSlotIndex;
- private int currentLineNumber;
- private int parserState;
- private int currentToken;
- private String currentTokenString;
- private String currentTokenAttributes;
-
- public HTMLTokenGenerator(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 (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 boolean isSpaceOrCR(byte var1) {
- return var1 == 32 || var1 == 9 || var1 == 10 || var1 == 13;
- }
-
- private String attributes(byte[] var1) throws HTMLParsingException {
- if (var1.length == 0 || var1[0] != 60 || var1[var1.length - 1] != 62) {
- this.syntaxError("Malformed marker");
- }
-
- int var2 = 1;
-
- int var3;
- for(var3 = var1.length; var2 < var3 && this.isSpaceOrCR(var1[var2]); ++var2) {
- }
-
- while(var2 < var3 && !this.isSpaceOrCR(var1[var2])) {
- ++var2;
- }
-
- while(var2 < var3 && this.isSpaceOrCR(var1[var2])) {
- ++var2;
- }
-
- return var3 - 1 - var2 > 0 ? new String(var1, 0, var2, var3 - 1 - var2) : "";
- }
-
- private String marker(byte[] var1) throws HTMLParsingException {
- if (var1.length == 0 || var1[0] != 60 || var1[var1.length - 1] != 62) {
- this.syntaxError("Malformed marker");
- }
-
- int var2 = 1;
- int var4 = 1;
-
- int var3;
- for(var3 = var1.length; var2 < var3 && this.isSpaceOrCR(var1[var2]); ++var4) {
- ++var2;
- }
-
- if (var1[var2] == 47) {
- ++var2;
- ++var4;
- }
-
- while(var2 < var3 - 1 && !this.isSpaceOrCR(var1[var2])) {
- ++var2;
- }
-
- return (new String(var1, 0, var4, var2 - var4)).toUpperCase();
- }
-
- private boolean isMarkerBegin(byte[] var1) throws HTMLParsingException {
- if (var1.length == 0 || var1[0] != 60 || var1[var1.length - 1] != 62) {
- this.syntaxError("Malformed marker");
- }
-
- int var2 = 1;
-
- for(int var3 = var1.length; var2 < var3 && this.isSpaceOrCR(var1[var2]); ++var2) {
- }
-
- return var1[var2] != 47;
- }
-
- private final void parseOneToken() throws HTMLParsingException, IOException {
- while(true) {
- if (this.currentToken == 0) {
- if (this.hasMoreCharacters()) {
- byte var1 = this.peekNextCharacter();
- if (var1 == 10) {
- ++this.currentLineNumber;
- }
-
- switch (this.parserState) {
- case 0:
- if (var1 == 60) {
- this.parserState = 4;
- } else {
- this.parserState = 1;
- }
-
- this.markPreviousCharacter();
- continue;
- case 1:
- if (var1 == 60) {
- this.currentToken = 1;
- this.currentTokenAttributes = null;
- this.currentTokenString = new String(this.getAndDeletePeekedCharactersMinusOne(), 0);
- this.markPreviousCharacter();
- this.parserState = 4;
- }
- continue;
- case 2:
- if (var1 != 62) {
- continue;
- }
-
- byte[] var2 = this.getAndDeletePeekedCharacters();
- if (this.isMarkerBegin(var2)) {
- this.currentToken = 2;
- } else {
- this.currentToken = 3;
- }
-
- this.currentTokenAttributes = this.attributes(var2);
- this.currentTokenString = this.marker(var2);
- this.parserState = 0;
- continue;
- case 3:
- if (var1 == 45) {
- this.parserState = 5;
- continue;
- }
-
- if (var1 == 62) {
- this.currentToken = 4;
- this.currentTokenString = new String(this.getAndDeletePeekedCharacters(), 0);
- this.currentTokenAttributes = null;
- this.parserState = 0;
- }
- continue;
- case 4:
- if (var1 == 33) {
- this.parserState = 3;
- continue;
- }
-
- this.parserState = 2;
- continue;
- case 5:
- if (var1 == 45) {
- this.parserState = 6;
- continue;
- }
-
- if (var1 == 62) {
- this.currentToken = 4;
- this.currentTokenString = new String(this.getAndDeletePeekedCharacters(), 0);
- this.currentTokenAttributes = null;
- this.parserState = 0;
- continue;
- }
-
- this.parserState = 3;
- continue;
- case 6:
- if (var1 != 10 && var1 != 13) {
- if (var1 == 62) {
- this.currentToken = 4;
- this.currentTokenString = new String(this.getAndDeletePeekedCharacters(), 0);
- this.currentTokenAttributes = null;
- this.parserState = 0;
- } else {
- this.parserState = 3;
- }
- }
- default:
- continue;
- }
- }
-
- if (this.parserState != 1 && this.markedByteIndex != -1) {
- this.rewindToMarkedCharacter();
- }
- }
-
- if (this.currentToken == 0 && !this.hasMoreCharacters()) {
- switch (this.parserState) {
- case 0:
- break;
- case 1:
- this.currentToken = 1;
- this.currentTokenString = new String(this.getAndDeletePeekedCharacters(), 0);
- this.currentTokenAttributes = null;
- this.parserState = 0;
- return;
- case 2:
- case 4:
- this.parserState = 0;
- this.syntaxError("Unterminated marker");
- return;
- case 3:
- default:
- this.parserState = 0;
- this.syntaxError("Unterminated comment. Comment should end with -->");
- return;
- }
- }
-
- return;
- }
- }
-
- public final boolean hasMoreTokens() throws HTMLParsingException, IOException {
- if (this.currentToken != 0) {
- return true;
- } else {
- this.parseOneToken();
- return this.currentToken != 0;
- }
- }
-
- public final int nextToken() throws HTMLParsingException, IOException {
- int var1 = 0;
- if (this.currentToken == 0) {
- this.parseOneToken();
- }
-
- if (this.currentToken != 0) {
- var1 = this.currentToken;
- this.currentToken = 0;
- }
-
- return var1;
- }
-
- final int peekNextToken() throws HTMLParsingException, IOException {
- if (this.currentToken == 0) {
- this.parseOneToken();
- }
-
- return this.currentToken;
- }
-
- public final String stringForLastToken() {
- return this.currentTokenString;
- }
-
- public final String attributesForLastToken() {
- return this.currentTokenAttributes;
- }
-
- final int lineForLastToken() {
- return this.currentLineNumber + 1;
- }
-
- final void syntaxError(String var1) throws HTMLParsingException {
- throw new HTMLParsingException(var1, this.lineForLastToken());
- }
- }
-