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); }
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; }
/** * 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); } }
public String getPrefixWhiteSpace () { int pos = getStartOffset (); int N = getEndOffset (); while (pos < N) { char c = source.charAt (pos); if (!Character.isWhitespace (c)) break; pos ++; } return pos == 0 ? "" : source.substring (getStartOffset (), pos); } }
protected String lookAheadForErrorMessage (String conditionalPrefix, int pos, int len) { String found = ""; if (pos < source.length ()) { int len2 = source.length () - pos; len = Math.min (len, len2); String s = source.substring (pos, pos+len); if (len != len2) s += "..."; if (conditionalPrefix == null) found = TextUtils.escapeJavaString (s); else found = " " + conditionalPrefix + " " + TextUtils.escapeJavaString (s); } return found; }
/** Return the string of text which this token represents in the XMLSource * * @return the text or <code>null</code> if there is no source */ public String getText () { return getSource() == null ? null : getSource().substring (getStartOffset(), getEndOffset()); }
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); }
s = source.substring (errorOffset, pos2);
protected void parseDocTypeSubElement (XMLTokenizer tokenizer, Token startToken, DocType docType) { Token token = startToken; token = expect (tokenizer, token, Type.DTD_WHITESPACE, "Expected whitespace after '<!ELEMENT'"); token = expect (tokenizer, token, Type.TEXT, "Expected element name"); String name = token.getText (); token = expect (tokenizer, token, Type.DTD_WHITESPACE, "Expected whitespace after element name"); Token beforeContent = token; while ((token = tokenizer.next ()) != null) { //System.out.println ("parseDocTypeSubElement "+token); if (token.getType() == Type.DOCTYPE_END) break; // TODO Check EMPTY, ANY, #PCDATA, (|), ?, *, + } if (token == null) throw new XMLParseException ("Unexpected EOF while parsing element content", tokenizer.getSource (), tokenizer.getOffset ()); String content = tokenizer.getSource ().substring (beforeContent.getEndOffset (), token.getStartOffset ()); startToken.setEndOffset (token.getEndOffset ()); DocTypeElement element = new DocTypeElement (startToken, name, content); docType.add (element); }