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;
}
}
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;
}
Aggregations