Example usage for org.xml.sax XMLReader getEntityResolver

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

Introduction

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

Prototype

public EntityResolver getEntityResolver();

Source Link

Document

Return the current entity resolver.

Usage

From source file:org.jboss.confluence.plugin.docbook_tools.docbookimport.DocbookImporter.java

/**
 * Process XSLT transformation.//from  ww w . java 2 s. c  o m
 * 
 * @param xsltTemplate input stream with XSLT template file used to transform (closed inside this method)
 * @param xmlToTransform input stream with XML file to transform (closed inside this method)
 * @param xmlToTransformURL URL of <code>xmlToTransform</code> file (may be <code>file://</code> too). We need it to
 *          correctly evaluate relative paths.
 * @param output stream to write transformed output to
 * @throws javax.xml.transform.TransformerException
 */
protected void processXslt(final InputStream xsltTemplate, final InputStream xmlToTransform,
        final String xmlToTransformURL, final OutputStream output) throws Exception {

    final XSLTErrorListener errorListener = new XSLTErrorListener();
    final SAXErrorHandler eh = new SAXErrorHandler();

    Thread th = new Thread(new Runnable() {

        public void run() {
            try {
                org.xml.sax.InputSource xmlSource = new org.xml.sax.InputSource(xmlToTransform);
                xmlSource.setSystemId(xmlToTransformURL);
                javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(
                        xsltTemplate);
                javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(output);

                // prepare XInclude aware parser which resolves necessary entities correctly
                XMLReader reader = new ParserAdapter(saxParserFactory.newSAXParser().getParser());
                reader.setEntityResolver(new JDGEntityResolver(reader.getEntityResolver()));
                reader.setErrorHandler(eh);
                SAXSource xmlSAXSource = new SAXSource(reader, xmlSource);

                javax.xml.transform.Transformer trans = transformerFact.newTransformer(xsltSource);

                trans.setErrorListener(errorListener);
                trans.transform(xmlSAXSource, result);

            } catch (Exception e) {
                if (e instanceof TransformerException) {
                    errorListener.setException((TransformerException) e);
                } else {
                    errorListener.setException(new TransformerException(e));
                }
            } finally {
                FileUtils.closeInputStream(xmlToTransform);
                FileUtils.closeInputStream(xsltTemplate);
            }
        }
    });
    th.setName("DocbookImporter XSLT transformation thread");
    th.setDaemon(true);
    th.setContextClassLoader(DocbookImporter.class.getClassLoader());
    th.start();
    th.join();

    if (eh.getException() != null) {
        throw eh.getException();
    }

    if (errorListener.getException() != null) {
        throw errorListener.getException();
    }

}