Example #1
0
  /**
   * 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;
  }