private void checkQuote (String name, char value) { if (value != '"' && value != '\'') throw new XMLParseException ("The quote for "+name+" must be '\"' or '\\'': ["+value+"]"); }
protected void checkSpace (String name, String value) { if (value == null) throw new XMLParseException ("space before " + name + " field can't be null"); if (value.length () == 0) throw new XMLParseException ("space before " + name + " field can't be empty"); if (value.trim ().length () != 0) throw new XMLParseException ("space before " + name + " field must contain only whitespace: ["+value+"]"); }
protected void checkEquals (String name, String value) { if (value == null) throw new XMLParseException ("equal sign after " + name + " field can't be null"); if (value.length () == 0) throw new XMLParseException ("equal sign after " + name + " field can't be empty"); if (!"=".equals (value.trim ())) throw new XMLParseException ("equal sign after " + name + " field must only contain whitespace and a single '=': ["+value+"]"); }
public Comment setText (String text) { if (text == null || text.length () == 0) text = " "; if (text.contains ("--")) throw new XMLParseException ("The text of a comment must not contain '--': ["+text+"]"); setValue ("<!--" + text + "-->"); this.text = text; return this; }
protected void nextChars (String expected, int startPos, String errorMessage) { int len = expected.length () - (pos - startPos); if (pos + len > source.length ()) throw new XMLParseException (errorMessage, source, startPos); String s = source.substring (startPos, startPos + expected.length ()); if (!expected.equals (s)) throw new XMLParseException (errorMessage, source, startPos); pos += len; }
public XMLDeclaration setPostSpace (String postSpace) { if (postSpace == null) postSpace = ""; else if (postSpace.trim ().length () != 0) throw new XMLParseException ("Space after the last field must not contain anything but whitespace"); this.postSpace = postSpace; updateText (); return this; }
private void expect (XMLSource source, int pos, String expected) { int end = Math.min (pos + expected.length (), source.length ()); String s = source.substring (pos, end); if (!expected.equals (s)) throw new XMLParseException ("Expected '"+expected+"' but found '"+s+"'", source, pos); }
public static char checkQuoteChar (String value, char quoteChar) { if (quoteChar == '\0') { if (!value.contains ("\"")) quoteChar = '"'; else if (!value.contains ("'")) quoteChar = '\''; else quoteChar = '"'; } else if (quoteChar != '"' && quoteChar != '\'') { char[] buffer = new char[1]; buffer[0] = (char)quoteChar; String s = new String (buffer); throw new XMLParseException ("Illegal quote charater: "+TextUtils.escapeJavaString (s)+" ("+(int)quoteChar+")"); } return quoteChar; }
protected char nextChar (String errorMessage) { if (pos >= source.length ()) throw new XMLParseException (errorMessage, source, pos); return source.charAt (pos ++); }
public Element checkMandatoryAttribute (String name) { Attribute a = getAttribute (name); if (a == null) throw new XMLParseException ("Element "+getName ()+" has no attribute "+name); return this; }
/** Read the name of an element. * * <p>The resulting token will contain the '<' plus any whitespace between * it and the name plus the name itself but no whitespace after the name. */ protected void parseBeginElement (Token token) { token.setType (Type.BEGIN_ELEMENT); inStartElement = true; skipWhiteSpace (); int nameStartOffset = pos; parseName ("start tag"); if (pos == nameStartOffset) throw new XMLParseException ("Missing element name", token); if (pos >= source.length ()) throw new XMLParseException ("Missing '>' of start tag", source, pos); char c = source.charAt (pos); if (!charValidator.isWhitespace (c) && c != '/' && c != '>') throw new XMLParseException ("Expected whitespace, '>' or '/>' after element name", source, pos); }
/** * Verify an entity. If no entityResolver is installed, this does nothing. */ protected void verifyEntity (int start, int end) { if (entityResolver == null) return; String entity = source.substring (start, end); try { entityResolver.validateEntity (entity); } catch (IllegalArgumentException e) { throw new XMLParseException (e.getMessage (), e).setSource (source, start); } }
/** * Check that the next character is {@code expected} and skip it */ protected void expect (char expected) { if (pos >= source.length () || source.charAt (pos) != expected) { throw new XMLParseException ("Expected '"+expected+"'"+lookAheadForErrorMessage ("but found", pos, 20), source, pos); } pos ++; }
/** * Fetch the next token and make sure it's {@code expected}. If not, create an * {@link XMLParseException} using the {@code errorMessage} */ protected Token expect (XMLTokenizer tokenizer, Token startToken, Type expected, String errorMessage) { Token token = tokenizer.next (); //System.out.println (token); if (token == null || token.getType () != expected) { if (token == null) token = startToken; throw new XMLParseException (errorMessage + tokenizer.lookAheadForErrorMessage ("but found", token.getStartOffset (), 20) + " (" + token + ")", token); } return token; }
/** * If the next token is whitespace, skip it. * * @param tokenizer * @param startToken This might be whitespace * @param docType * @return The current or the next token. */ protected Token skipOptionalWhitespace (XMLTokenizer tokenizer, Token startToken, DocType docType) { if (startToken == null) throw new XMLParseException ("Unexpected EOF after '<!DOCTYPE'", tokenizer.getSource (), tokenizer.getSource ().length ()); Token token = startToken; if (token.getType () == Type.DTD_WHITESPACE) { docType.add (toNode (token)); token = tokenizer.next (); if (token == null) throw new XMLParseException ("Unexpected EOF after '<!DOCTYPE'", startToken); } return token; }
/** Parse "<!--" or "<![CDATA[" */ protected void parseExcalamation (Token token) { char c = source.charAt (pos); if (c == '-') { pos ++; parseComment (token); } else if (c == '[') { pos ++; parseCData (token); } else if (c == 'D') { pos ++; parseDocType (token); } else throw new XMLParseException ("Expected '<!--' or '<![CDATA['", source, pos-2); }
protected void parseDocTypeText (Token token) { token.setType (Type.TEXT); pos --; while (pos < source.length () && getCharValidator ().isNameChar (source.charAt (pos))) pos ++; String s = source.substring (token.getStartOffset (), pos); if (s.length () == 0) throw new XMLParseException ("Expected some text"+lookAheadForErrorMessage ("but found", token.getStartOffset (), 20), token); // TODO How about "<!DOCTYPE SYSTEM ..."? if ("SYSTEM".equals (s)) token.setType (Type.DOCTYPE_SYSTEM); else if ("PUBLIC".equals (s)) token.setType (Type.DOCTYPE_PUBLIC); else if ("NDATA".equals (s)) token.setType (Type.DOCTYPE_NDATA); }
/** Read an XML name */ protected void parseName (String objectName) { int startPos = pos; if (pos < source.length () && charValidator.isNameStartChar (source.charAt (pos))) { pos ++; while (pos < source.length () && charValidator.isNameChar (source.charAt (pos))) pos ++; } if (pos == startPos) { throw new XMLParseException ("Expected valid XML name for "+objectName+lookAheadForErrorMessage ("but found", startPos, 20), source, startPos); } }
protected Token parseAttListNameTokens (XMLTokenizer tokenizer, Token token, DocTypeAttributeList attList) { while ((token = tokenizer.next ()) != null) { attList.addNode (toNode (token)); if (token.getType () == Type.DTD_WHITESPACE || token.getType () == Type.TEXT || token.getType () == Type.DOCTYPE_ALTERNATIVE ) continue; if (token.getType () == Type.DOCTYPE_END_GROUP) break; throw new XMLParseException ("Expected whitespace, '|' or a name token", token); } return token; }
protected void parseEntity (Token token) { token.setType (Type.ENTITY); char c; if (pos < source.length ()) { c = source.charAt (pos); if (c == '#') pos ++; } while (pos < source.length ()) { c = source.charAt (pos); if (c == ';') break; if (!charValidator.isNameChar (c)) throw new XMLParseException ("Illegal character in entity: ["+c+"] ("+Integer.toHexString (c)+")", source, pos); pos ++; } expect (';'); verifyEntity (token.getStartOffset (), pos); }