private void configureParser(final XMLReader parser, final SAXHandler contentHandler) throws JDOMException {

    // Setup SAX handlers.

    parser.setContentHandler(contentHandler);

    if (saxEntityResolver != null) {
      parser.setEntityResolver(saxEntityResolver);
    }

    parser.setDTDHandler(contentHandler);

    parser.setErrorHandler(new BuilderErrorHandler());

    boolean success = false;

    try {
      parser.setProperty(SAX_PROPERTY_LEXICAL_HANDLER,
                         contentHandler);
      success = true;
    }
    catch (final SAXNotSupportedException | SAXNotRecognizedException e) {
      // No lexical reporting available
    }

    // Some parsers use alternate property for lexical handling (grr...)
    if (!success) {
      try {
        parser.setProperty(SAX_PROPERTY_LEXICAL_HANDLER_ALT,
                           contentHandler);
      }
      catch (final SAXNotSupportedException | SAXNotRecognizedException e) {
        // No lexical reporting available
      }
    }

    // Set any user-specified properties on the parser.
    for (final Map.Entry<String, Object> me : properties.entrySet()) {
      internalSetProperty(parser, me.getKey(), me.getValue(), me.getKey());
    }

    // Set any user-specified features on the parser (always includes entity expansion true/false).
    for (Map.Entry<String, Boolean> me : features.entrySet()) {
      internalSetFeature(parser, me.getKey(), me.getValue(), me.getKey());
    }

    // Try setting the DeclHandler if entity expansion is off
    if (!getExpandEntities()) {
      try {
        parser.setProperty(SAX_PROPERTY_DECLARATION_HANDLER, contentHandler);
      }
      catch (final SAXNotSupportedException | SAXNotRecognizedException e) {
        // No lexical reporting available
      }
    }

    try {
      parser.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    }
    catch (Exception ignore) {
    }
  }
private void configureParser(final XMLReader parser, final SAXHandler contentHandler)