/** * 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; }
private static void inputSourceToSAX( InputSource inputSource, XMLReceiver xmlReceiver, XMLUtils.ParserConfiguration parserConfiguration, boolean handleLexical) { // Insert XInclude processor if needed final TransformerURIResolver resolver; if (parserConfiguration.handleXInclude) { parserConfiguration = new XMLUtils.ParserConfiguration( parserConfiguration.validating, false, parserConfiguration.externalEntities, parserConfiguration.uriReferences); resolver = new TransformerURIResolver(XMLUtils.ParserConfiguration.PLAIN); xmlReceiver = new XIncludeReceiver(null, xmlReceiver, parserConfiguration.uriReferences, resolver); } else { resolver = null; } try { final XMLReader xmlReader = newSAXParser(parserConfiguration).getXMLReader(); xmlReader.setContentHandler(xmlReceiver); if (handleLexical) xmlReader.setProperty(XMLConstants.SAX_LEXICAL_HANDLER, xmlReceiver); xmlReader.setEntityResolver(ENTITY_RESOLVER); xmlReader.setErrorHandler(ERROR_HANDLER); xmlReader.parse(inputSource); } catch (SAXParseException e) { throw new ValidationException(e.getMessage(), new LocationData(e)); } catch (Exception e) { throw new OXFException(e); } finally { if (resolver != null) resolver.destroy(); } }