/** * Checks a set of characters, throws IOException if invalid */ public static void checkCharacters(char[] cbuf, int off, int len) throws IOException { int end = off + len; while(off<end) checkCharacter(cbuf[off++]); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { checkCharacters(cbuf, off, len); out.write(cbuf, off, len); }
return new UrlValidator(out); case XHTML: return new XhtmlValidator(out); case XHTML_ATTRIBUTE: return new XhtmlAttributeValidator(out);
/** * Encodes a single character and returns its String representation * or null if no modification is necessary. * * @see XhtmlValidator#checkCharacter(char) * * @throws IOException if any text character cannot be converted to XHTML */ private static String getEscapedCharacter(char c) throws IOException { // 0.0037849858401 ms per call with switch // 0.00360391125522 ms per call with if, no room to improve here switch(c) { case '<': return "<"; case '>': return ">"; case '&': return "&"; default: XhtmlValidator.checkCharacter(c); return null; } }
@Override public XhtmlValidator append(CharSequence csq, int start, int end) throws IOException { checkCharacters(csq, start, end); out.append(csq, start, end); return this; }
/** * Checks a set of characters, throws IOException if invalid */ public static void checkCharacters(CharSequence str, int off, int end) throws IOException { while(off<end) checkCharacter(str.charAt(off++)); }
@Override public void write(String str, int off, int len) throws IOException { if(str==null) throw new IllegalArgumentException("str is null"); checkCharacters(str, off, off + len); out.write(str, off, len); }
@Override public void write(int c) throws IOException { checkCharacter((char)c); out.write(c); }
@Override public XhtmlValidator append(CharSequence csq) throws IOException { checkCharacters(csq, 0, csq.length()); out.append(csq); return this; }
@Override public XhtmlValidator append(char c) throws IOException { checkCharacter(c); out.append(c); return this; } }