Example usage for org.xml.sax XMLReader setContentHandler

List of usage examples for org.xml.sax XMLReader setContentHandler

Introduction

In this page you can find the example usage for org.xml.sax XMLReader setContentHandler.

Prototype

public void setContentHandler(ContentHandler handler);

Source Link

Document

Allow an application to register a content event handler.

Usage

From source file:xbird.xquery.dm.instance.DocumentTableModel.java

private static final XMLReader getXMLReader(final DocumentTableBuilder handler, final boolean parseAsHtml,
        final boolean resolveEntity) {
    final XMLReader myReader;
    try {//from  w  ww .  j a  v a  2  s.c  o  m
        if (parseAsHtml) {
            Class clazz = ClassResolver.get(HTML_PARSER_CLASS);
            assert (clazz != null);
            myReader = ObjectUtils.<XMLReader>instantiate(clazz);
        } else {
            final SAXParserFactory factory;
            if (hasSunXerces) {
                factory = ObjectUtils
                        .instantiate("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", null);
            } else {
                factory = SAXParserFactory.newInstance();
            }
            factory.setNamespaceAware(true);
            SAXParser parser = factory.newSAXParser();
            myReader = parser.getXMLReader();
        }
    } catch (Exception e) {
        throw new XQRTException("Creating SAX XMLReader failed", e);
    }
    // setup handlers (requires saxHandler)
    myReader.setContentHandler(handler);
    try {
        myReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        myReader.setFeature("http://xml.org/sax/features/validation", true); // Validate the document and report validity errors.
        if (!parseAsHtml) {
            myReader.setFeature("http://apache.org/xml/features/validation/dynamic", true); // The parser will validate the document only if a grammar is specified.   
            myReader.setFeature("http://apache.org/xml/features/validation/schema", true); // Turn on XML Schema validation by inserting an XML Schema validator into the pipeline.   
        }
    } catch (Exception e) {
        throw new XQRTException("Configuaring SAX XMLReader failed.", e);
    }
    // setup entity resolver
    if (resolveEntity) {
        org.apache.xml.resolver.CatalogManager catalog = org.apache.xml.resolver.CatalogManager
                .getStaticManager();
        catalog.setIgnoreMissingProperties(true);
        catalog.setPreferPublic(true);
        catalog.setUseStaticCatalog(false);
        EntityResolver resolver = new org.apache.xml.resolver.tools.CatalogResolver(catalog);
        myReader.setEntityResolver(resolver);
    }
    return myReader;
}