Example usage for org.xml.sax XMLReader setDTDHandler

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

Introduction

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

Prototype

public void setDTDHandler(DTDHandler handler);

Source Link

Document

Allow an application to register a DTD event handler.

Usage

From source file:org.jbuiltDemo.managed.view.Xhtml2Jbuilt.java

public Xhtml2Jbuilt() throws Exception {
    factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);/*from   w ww .j  a v a 2 s  .co m*/
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setValidating(false);

    parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setDTDHandler(this);
    reader.setContentHandler(this);
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", this);
    reader.setErrorHandler(this);
    reader.setEntityResolver(this);
}

From source file:org.pentaho.reporting.libraries.xmlns.parser.AbstractXmlResourceFactory.java

/**
 * Creates a resource by interpreting the data given in the resource-data object. If additional datastreams need to be
 * parsed, the provided resource manager should be used. This method parses the given resource-data as XML stream.
 *
 * @param manager the resource manager used for all resource loading.
 * @param data    the resource-data from where the binary data is read.
 * @param context the resource context used to resolve relative resource paths.
 * @return the parsed result, never null.
 * @throws ResourceCreationException if the resource could not be parsed due to syntaxctial or logical errors in the
 *                                   data.
 * @throws ResourceLoadingException  if the resource could not be accessed from the physical storage.
 *///from   w  w  w. j  a  v  a 2  s. c  o m
public Resource create(final ResourceManager manager, final ResourceData data, final ResourceKey context)
        throws ResourceCreationException, ResourceLoadingException {
    try {
        final SAXParser parser = getParser();

        final XMLReader reader = parser.getXMLReader();
        final XmlFactoryModule[] rootHandlers = getModules();
        if (rootHandlers.length == 0) {
            throw new ResourceCreationException(
                    "There are no root-handlers registered for the factory for type " + getFactoryType());
        }

        final ResourceDataInputSource input = new ResourceDataInputSource(data, manager);

        final ResourceKey contextKey;
        final long version;
        final ResourceKey targetKey = data.getKey();
        if (context == null) {
            contextKey = targetKey;
            version = data.getVersion(manager);
        } else {
            contextKey = context;
            version = -1;
        }

        final RootXmlReadHandler handler = createRootHandler(manager, targetKey, rootHandlers, contextKey,
                version);

        final DefaultConfiguration parserConfiguration = handler.getParserConfiguration();
        final URL value = manager.toURL(contextKey);
        if (value != null) {
            parserConfiguration.setConfigProperty(CONTENTBASE_KEY, value.toExternalForm());
        }

        configureReader(reader, handler);
        reader.setContentHandler(handler);
        reader.setDTDHandler(handler);
        reader.setEntityResolver(handler.getEntityResolver());
        reader.setErrorHandler(getErrorHandler());

        final Map parameters = targetKey.getFactoryParameters();
        final Iterator it = parameters.keySet().iterator();
        while (it.hasNext()) {
            final Object o = it.next();
            if (o instanceof FactoryParameterKey) {
                final FactoryParameterKey fpk = (FactoryParameterKey) o;
                handler.setHelperObject(fpk.getName(), parameters.get(fpk));
            }
        }

        reader.parse(input);

        final Object createdProduct = finishResult(handler.getResult(), manager, data, contextKey);
        handler.getDependencyCollector().add(targetKey, data.getVersion(manager));
        return createResource(targetKey, handler, createdProduct, getFactoryType());
    } catch (ParserConfigurationException e) {
        throw new ResourceCreationException("Unable to initialize the XML-Parser", e);
    } catch (SAXException e) {
        throw new ResourceCreationException("Unable to parse the document: " + data.getKey(), e);
    } catch (IOException e) {
        throw new ResourceLoadingException("Unable to read the stream from document: " + data.getKey(), e);
    }
}

From source file:org.pentaho.reporting.libraries.xmlns.parser.AbstractXmlResourceFactory.java

/**
 * A method to allow to invoke the parsing without accessing the LibLoader layer. The data to be parsed is held in the
 * given InputSource object./* w ww .j  av a2s .  co m*/
 *
 * @param manager    the resource manager used for all resource loading.
 * @param input      the raw-data given as SAX-InputSource.
 * @param context    the resource context used to resolve relative resource paths.
 * @param parameters the parse parameters.
 * @return the parsed result, never null.
 * @throws ResourceCreationException    if the resource could not be parsed due to syntaxctial or logical errors in
 *                                      the data.
 * @throws ResourceLoadingException     if the resource could not be accessed from the physical storage.
 * @throws ResourceKeyCreationException if creating the context key failed.
 */
public Object parseDirectly(final ResourceManager manager, final InputSource input, final ResourceKey context,
        final Map parameters)
        throws ResourceKeyCreationException, ResourceCreationException, ResourceLoadingException {
    try {
        final SAXParser parser = getParser();

        final XMLReader reader = parser.getXMLReader();

        final ResourceKey targetKey = manager.createKey(EMPTY_DATA);
        final ResourceKey contextKey;
        if (context == null) {
            contextKey = targetKey;
        } else {
            contextKey = context;
        }

        final XmlFactoryModule[] rootHandlers = getModules();
        final RootXmlReadHandler handler = createRootHandler(manager, targetKey, rootHandlers, contextKey, -1);

        final DefaultConfiguration parserConfiguration = handler.getParserConfiguration();
        final URL value = manager.toURL(contextKey);
        if (value != null) {
            parserConfiguration.setConfigProperty(CONTENTBASE_KEY, value.toExternalForm());
        }

        configureReader(reader, handler);
        reader.setContentHandler(handler);
        reader.setDTDHandler(handler);
        reader.setEntityResolver(handler.getEntityResolver());
        reader.setErrorHandler(getErrorHandler());

        final Iterator it = parameters.keySet().iterator();
        while (it.hasNext()) {
            final Object o = it.next();
            if (o instanceof FactoryParameterKey) {
                final FactoryParameterKey fpk = (FactoryParameterKey) o;
                handler.setHelperObject(fpk.getName(), parameters.get(fpk));
            }
        }

        reader.parse(input);

        return finishResult(handler.getResult(), manager, new RawResourceData(targetKey), contextKey);
    } catch (ParserConfigurationException e) {
        throw new ResourceCreationException("Unable to initialize the XML-Parser", e);
    } catch (SAXException e) {
        throw new ResourceCreationException("Unable to parse the document", e);
    } catch (IOException e) {
        throw new ResourceLoadingException("Unable to read the stream", e);
    }

}