/** Parse the document, calling back the handler */ void parse() throws IOException, SchemaParseException { char c; StringBuffer curWord = new StringBuffer(); int wordContext = WORD_NO_CONTEXT; int level = 0; while ((c = this.getNext()) != '\0') { switch (c) { case '<': // Check if we have <! or <-- char c1 = this.getNext(); if (c1 == '!') { // Check if the next word is reserved if (wordContext != WORD_NO_CONTEXT && wordContext != WORD_COMMENT) { System.err.println( "Error: found a '<!' sequence within another '<!' sequence"); // NOI18N throw new SchemaParseException( "Warning: found a '<!' sequence within another '<!' sequence"); // NOI18N } if (wordContext != WORD_COMMENT) wordContext = WORD_CHECK; } else if (c1 == '?') { wordContext = WORD_PI; } else { curWord.append(c); curWord.append(c1); } break; case '>': // Might be the end of a comment or <!element switch (wordContext) { case WORD_NO_CONTEXT: // System.err.println("Error: Found '>' without '<!'");// NOI18N throw new SchemaParseException("Error: Found '>' without '<!'"); // NOI18N case WORD_PI: String word = curWord.toString(); int len = word.length(); if (word.endsWith("?")) { // NOI18N // Ignore any PI curWord.delete(0, len); wordContext = WORD_NO_CONTEXT; } else curWord.append(c); break; case WORD_COMMENT: word = curWord.toString(); len = word.length(); if (word.endsWith("--")) { // NOI18N this.handler.endElement(); curWord.delete(0, len); wordContext = WORD_NO_CONTEXT; } else curWord.append(c); break; case WORD_ENTITY: wordContext = WORD_NO_CONTEXT; break; default: wordContext = this.processWord(curWord, wordContext); this.handler.endElement(); wordContext = WORD_NO_CONTEXT; } break; case '(': if (wordContext == WORD_ELEMENT || wordContext == WORD_ATTLIST) { wordContext = this.processWord(curWord, wordContext); this.handler.startGroupElements(); } else curWord.append(c); break; case ')': wordContext = this.processWord(curWord, wordContext); if (wordContext == WORD_ELEMENT || wordContext == WORD_ATTLIST) { int instance = this.getInstanceValue(this.peekNext()); // Get rid of the extra character if (instance != Common.TYPE_1) this.getNext(); this.handler.endGroupElements(instance); } else curWord.append(c); break; case '|': wordContext = this.processWord(curWord, wordContext); if (wordContext == WORD_ELEMENT || wordContext == WORD_ATTLIST) this.handler.character(c); else curWord.append(c); break; case '\n': case '\r': case '\t': case ' ': case ',': wordContext = this.processWord(curWord, wordContext); break; // default: curWord.append(c); } } if (wordContext != WORD_NO_CONTEXT) System.out.println("Warning: unexpected EOF"); // NOI18N }
/** Find out the type of the current word */ private int processWord(StringBuffer curWord, int wordContext) throws SchemaParseException { String word = curWord.toString(); int len = word.length(); if (len > 0) { // We have some word to play with switch (wordContext) { case WORD_CHECK: if (word.startsWith("--")) { // NOI18N if (len > 2) word = curWord.substring(2); else word = ""; // NOI18N this.handler.startElement(word, word, Common.COMMENT); wordContext = WORD_COMMENT; } else if (word.equals("ELEMENT")) // NOI18N wordContext = WORD_ELEMENT1; else if (word.equals("ATTLIST")) // NOI18N wordContext = WORD_ATTLIST1; else if (word.equals("ENTITY")) // NOI18N wordContext = WORD_ENTITY1; else { // System.err.println("Error: found an unknown '<!' sequence (" + word + ")"); // NOI18N throw new SchemaParseException( "Error: found an unknown '<!' sequence (" + word + ")"); // NOI18N } break; case WORD_COMMENT: this.handler.element(word, word, 0); break; case WORD_ELEMENT1: this.handler.startElement(word, word, Common.ELEMENT); wordContext = WORD_ELEMENT; break; case WORD_ATTLIST1: this.handler.startElement(word, word, Common.ATTLIST); wordContext = WORD_ATTLIST; break; case WORD_ENTITY1: wordContext = WORD_ENTITY; break; case WORD_ENTITY: break; case WORD_ELEMENT: case WORD_ATTLIST: // Find out the instance value (*, ? or +) int instance = this.getInstanceValue(word.charAt(len - 1)); // Get rid of the extra character if (instance != Common.TYPE_1) word = curWord.substring(0, len - 1); try { this.handler.element(word, word, instance); } catch (MissingEndOfEltException e) { if (wordContext == WORD_ATTLIST) { // // The TreeBuilder is done with the previous // attribute and would expect an end of ATTLIST // declaration. // We might have several attributes declared on the // same ATTLIST declaration. // Let's continue assuming so, the TreeBuilder // checks the attribute semantic and will throw // if this is not the case. // this.handler.startElement(e.propName, e.propName, Common.ATTLIST); this.handler.element(word, word, instance); } } break; default: } curWord.delete(0, len); } return wordContext; }