/** * This will create a SAX XMLReader capable of parsing a DTD and configure it so that the DTD * parsing events are routed to the handlers registered onto this SAXOutputter. * * @return <code>XMLReader</code> a SAX2 parser. * @throws JDOMException if no parser can be created. */ private XMLReader createDTDParser() throws JDOMException { XMLReader parser = null; // Get a parser instance try { parser = createParser(); } catch (Exception ex1) { throw new JDOMException("Error in SAX parser allocation", ex1); } // Register handlers if (this.getDTDHandler() != null) { parser.setDTDHandler(this.getDTDHandler()); } if (this.getEntityResolver() != null) { parser.setEntityResolver(this.getEntityResolver()); } if (this.getLexicalHandler() != null) { try { parser.setProperty(SAX_PROPERTY_LEXICAL_HANDLER, this.getLexicalHandler()); } catch (SAXException ex1) { try { parser.setProperty(SAX_PROPERTY_LEXICAL_HANDLER_ALT, this.getLexicalHandler()); } catch (SAXException ex2) { // Forget it! } } } if (this.getDeclHandler() != null) { try { parser.setProperty(SAX_PROPERTY_DECLARATION_HANDLER, this.getDeclHandler()); } catch (SAXException ex1) { try { parser.setProperty(SAX_PROPERTY_DECLARATION_HANDLER_ALT, this.getDeclHandler()); } catch (SAXException ex2) { // Forget it! } } } // Absorb errors as much as possible, per Laurent parser.setErrorHandler(new DefaultHandler()); return parser; }
/** * Converts an HTML document to XML. * * @param io io reference * @param opts html options * @return parser * @throws IOException I/O exception */ private static IO toXML(final IO io, final HtmlOptions opts) throws IOException { // reader could not be initialized; fall back to XML if (READER == null) return io; try { // tries to extract the encoding from the input final TextInput ti = new TextInput(io); String enc = ti.encoding(); final byte[] content = ti.content(); // looks for a charset definition final byte[] encoding = token("charset="); int cs = indexOf(content, encoding); if (cs > 0) { // extracts the encoding string cs += encoding.length; int ce = cs; final int cl = content.length; while (++ce < cl && content[ce] > 0x28) ; enc = string(substring(content, cs, ce)); } // define input final InputSource is = new InputSource(new ArrayInput(content)); is.setEncoding(supported(enc) ? normEncoding(enc) : UTF8); // define output final StringWriter sw = new StringWriter(); final XMLReader reader = (XMLReader) Reflect.get(READER); final Object writer = Reflect.get(WRITER, sw); // set TagSoup options if (opts.get(HtmlOptions.HTML)) { reader.setFeature("http://xml.org/sax/features/namespaces", false); opt("method", "html"); opt("omit-xml-declaration", "yes"); } if (opts.get(HtmlOptions.NONS)) reader.setFeature("http://xml.org/sax/features/namespaces", false); if (opts.get(HtmlOptions.OMITXML)) opt("omit-xml-declaration", "yes"); if (opts.get(HtmlOptions.NOBOGONS)) reader.setFeature(FEATURES + "ignore-bogons", true); if (opts.get(HtmlOptions.NODEFAULTS)) reader.setFeature(FEATURES + "default-attributes", false); if (opts.get(HtmlOptions.NOCOLONS)) reader.setFeature(FEATURES + "translate-colons", true); if (opts.get(HtmlOptions.NORESTART)) reader.setFeature(FEATURES + "restart-elements", false); if (opts.get(HtmlOptions.IGNORABLE)) reader.setFeature(FEATURES + "ignorable-whitespace", true); if (opts.get(HtmlOptions.EMPTYBOGONS)) reader.setFeature(FEATURES + "bogons-empty", true); if (opts.get(HtmlOptions.ANY)) reader.setFeature(FEATURES + "bogons-empty", false); if (opts.get(HtmlOptions.NOROOTBOGONS)) reader.setFeature(FEATURES + "root-bogons", false); if (opts.get(HtmlOptions.NOCDATA)) reader.setFeature(FEATURES + "cdata-elements", false); if (opts.get(HtmlOptions.LEXICAL)) reader.setProperty("http://xml.org/sax/properties/lexical-handler", writer); if (opts.contains(HtmlOptions.METHOD)) opt("method", opts.get(HtmlOptions.METHOD)); if (opts.contains(HtmlOptions.DOCTYPESYS)) opt("doctype-system", opts.get(HtmlOptions.DOCTYPESYS)); if (opts.contains(HtmlOptions.DOCTYPEPUB)) opt("doctype-public", opts.get(HtmlOptions.DOCTYPEPUB)); if (opts.contains(HtmlOptions.ENCODING)) is.setEncoding(opts.get(HtmlOptions.ENCODING)); // end TagSoup options reader.setContentHandler((ContentHandler) writer); reader.parse(is); return new IOContent(token(sw.toString()), io.name()); } catch (final SAXException ex) { Util.errln(ex); return io; } }