Search in sources :

Example 1 with MCRErrorListener

use of org.mycore.common.xsl.MCRErrorListener in project mycore by MyCoRe-Org.

the class MCRXSLTransformer method transform.

@Override
public void transform(MCRContent source, OutputStream out, MCRParameterCollector parameter) throws IOException {
    MCRErrorListener el = null;
    try {
        LinkedList<TransformerHandler> transformHandlerList = getTransformHandlerList(parameter);
        XMLReader reader = getXMLReader(transformHandlerList);
        TransformerHandler lastTransformerHandler = transformHandlerList.getLast();
        el = (MCRErrorListener) lastTransformerHandler.getTransformer().getErrorListener();
        StreamResult result = new StreamResult(out);
        lastTransformerHandler.setResult(result);
        reader.parse(source.getInputSource());
    } catch (TransformerConfigurationException | SAXException | IllegalArgumentException | ParserConfigurationException e) {
        throw new IOException(e);
    } catch (RuntimeException e) {
        if (el != null && e.getCause() == null && el.getExceptionThrown() != null) {
            // typically if a RuntimeException has no cause, we can get the "real cause" from MCRErrorListener, yeah!!!
            throw new IOException(el.getExceptionThrown());
        }
        throw e;
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) MCRErrorListener(org.mycore.common.xsl.MCRErrorListener) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 2 with MCRErrorListener

use of org.mycore.common.xsl.MCRErrorListener in project mycore by MyCoRe-Org.

the class MCRXSLTransformer method getTransformHandlerList.

protected LinkedList<TransformerHandler> getTransformHandlerList(MCRParameterCollector parameterCollector) throws TransformerConfigurationException, SAXException, ParserConfigurationException {
    checkTemplateUptodate();
    LinkedList<TransformerHandler> xslSteps = new LinkedList<>();
    // every transformhandler shares the same ErrorListener instance
    MCRErrorListener errorListener = MCRErrorListener.getInstance();
    for (Templates template : templates) {
        TransformerHandler handler = tFactory.newTransformerHandler(template);
        parameterCollector.setParametersTo(handler.getTransformer());
        handler.getTransformer().setErrorListener(errorListener);
        if (TRACE_LISTENER_ENABLED) {
            TransformerImpl transformer = (TransformerImpl) handler.getTransformer();
            TraceManager traceManager = transformer.getTraceManager();
            try {
                traceManager.addTraceListener(TRACE_LISTENER);
            } catch (TooManyListenersException e) {
                LOGGER.warn("Could not add MCRTraceListener.", e);
            }
        }
        if (!xslSteps.isEmpty()) {
            Result result = new SAXResult(handler);
            xslSteps.getLast().setResult(result);
        }
        xslSteps.add(handler);
    }
    return xslSteps;
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) TransformerHandler(javax.xml.transform.sax.TransformerHandler) TooManyListenersException(java.util.TooManyListenersException) MCRErrorListener(org.mycore.common.xsl.MCRErrorListener) SAXResult(javax.xml.transform.sax.SAXResult) Templates(javax.xml.transform.Templates) TraceManager(org.apache.xalan.trace.TraceManager) LinkedList(java.util.LinkedList) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)2 StreamResult (javax.xml.transform.stream.StreamResult)2 MCRErrorListener (org.mycore.common.xsl.MCRErrorListener)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 TooManyListenersException (java.util.TooManyListenersException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Result (javax.xml.transform.Result)1 Templates (javax.xml.transform.Templates)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 SAXResult (javax.xml.transform.sax.SAXResult)1 TraceManager (org.apache.xalan.trace.TraceManager)1 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1