use of nl.nn.adapterframework.xml.TransformerFilter in project iaf by ibissource.
the class XsltExceptionTest method testXsltException.
public void testXsltException(boolean expectChildThreads, int tailCount) throws Exception {
String xpathExpression = "*/*";
int xsltVersion = 1;
TransformerPool tp = TransformerPool.configureTransformer0(null, null, null, xpathExpression, null, OutputType.XML, false, null, xsltVersion);
XmlWriter writer = new XmlWriter();
FullXmlFilter filter = new FullXmlFilter(writer) {
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("error")) {
throw new SaxException("Found error");
}
super.startElement(uri, localName, qName, atts);
}
};
try (ThreadConnector threadConnector = expectChildThreads ? new ThreadConnector(null, null, null, (PipeLineSession) null) : null) {
TransformerFilter transformer = tp.getTransformerFilter(threadConnector, filter);
try {
try (SaxDocumentBuilder seb = new SaxDocumentBuilder("root", transformer)) {
seb.addElement("elem");
seb.addElement("error");
for (int i = 0; i < tailCount; i++) {
seb.addElement("elem");
}
}
fail("Expected exception to be caught while processing");
} catch (Exception e) {
System.out.println("Expected exception: " + e.getMessage());
}
System.out.println(writer);
}
}
use of nl.nn.adapterframework.xml.TransformerFilter in project iaf by ibissource.
the class ConfigurationDigester method getStub4TesttoolContentHandler.
/**
* Get the contenthandler to stub configurations
* If stubbing is disabled, the input ContentHandler is returned as-is
*/
public ContentHandler getStub4TesttoolContentHandler(ContentHandler handler, Properties properties) throws IOException, TransformerConfigurationException {
if (Boolean.parseBoolean(properties.getProperty(ConfigurationUtils.STUB4TESTTOOL_CONFIGURATION_KEY, "false"))) {
Resource xslt = Resource.getResource(ConfigurationUtils.STUB4TESTTOOL_XSLT);
TransformerPool tp = TransformerPool.getInstance(xslt);
TransformerFilter filter = tp.getTransformerFilter(null, handler);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(ConfigurationUtils.STUB4TESTTOOL_XSLT_VALIDATORS_PARAM, Boolean.parseBoolean(properties.getProperty(ConfigurationUtils.STUB4TESTTOOL_VALIDATORS_DISABLED_KEY, "false")));
XmlUtils.setTransformerParameters(filter.getTransformer(), parameters);
return filter;
}
return handler;
}
use of nl.nn.adapterframework.xml.TransformerFilter in project iaf by ibissource.
the class ForEachChildElementPipe method createHandler.
protected void createHandler(HandlerRecord result, ThreadConnector threadConnector, PipeLineSession session, ItemCallback callback) throws TransformerConfigurationException {
result.itemHandler = new ItemCallbackCallingHandler(callback);
result.inputHandler = result.itemHandler;
if (getXmlDebugger() != null && (StringUtils.isNotEmpty(getContainerElement()) || StringUtils.isNotEmpty(getTargetElement()) || getExtractElementsTp() != null)) {
String containerElementString = StringUtils.isNotEmpty(getContainerElement()) ? "filter to containerElement '" + getContainerElement() + "'" : null;
String targetElementString = StringUtils.isNotEmpty(getTargetElement()) ? "filter to targetElement '" + getTargetElement() + "'" : null;
String xpathString = getExtractElementsTp() != null ? "filter XPath '" + getElementXPathExpression() + "'" : null;
String label = "XML after preprocessing: " + Misc.concat(", ", containerElementString, targetElementString, xpathString);
result.inputHandler = getXmlDebugger().inspectXml(session, label, result.inputHandler);
}
if (isRemoveNamespaces()) {
result.inputHandler = new NamespaceRemovingFilter(result.inputHandler);
}
if (getExtractElementsTp() != null) {
if (log.isDebugEnabled())
log.debug("transforming input to obtain list of elements using xpath [" + getElementXPathExpression() + "]");
TransformerFilter transformerFilter = getExtractElementsTp().getTransformerFilter(threadConnector, result.inputHandler);
result.inputHandler = transformerFilter;
result.transformerErrorListener = (TransformerErrorListener) transformerFilter.getErrorListener();
result.errorMessage = "Could not process list of elements using xpath [" + getElementXPathExpression() + "]";
}
if (StringUtils.isNotEmpty(getTargetElement())) {
result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getTargetElement(), true, true, result.inputHandler);
}
if (StringUtils.isNotEmpty(getContainerElement())) {
result.inputHandler = new NodeSetFilter(XmlUtils.getNamespaceMap(getNamespaceDefs()), getContainerElement(), false, true, result.inputHandler);
}
result.inputHandler = new StopSensor(result.itemHandler, result.inputHandler);
result.inputHandler = new ExceptionCatchingFilter(result.inputHandler) {
@Override
protected void handleException(Exception e) throws SAXException {
if (e instanceof SaxTimeoutException) {
throw (SaxTimeoutException) e;
}
if (result.itemHandler.isStopRequested()) {
if (e instanceof SaxAbortException) {
throw (SaxAbortException) e;
}
throw new SaxAbortException(e);
}
// For improved diagnosability of error situations, rethrow the original exception, where applicable.
if (result.transformerErrorListener != null) {
TransformerException tex = result.transformerErrorListener.getFatalTransformerException();
if (tex != null) {
throw new SaxException(result.errorMessage, tex);
}
IOException iox = result.transformerErrorListener.getFatalIOException();
if (iox != null) {
throw new SaxException(result.errorMessage, iox);
}
}
throw new SaxException(result.errorMessage, e);
}
};
}
use of nl.nn.adapterframework.xml.TransformerFilter in project iaf by ibissource.
the class XsltSender method createHandler.
protected ContentHandler createHandler(Message input, ThreadConnector threadConnector, PipeLineSession session, MessageOutputStream target) throws StreamingException {
ContentHandler handler = null;
try {
ParameterValueList pvl = null;
if (paramList != null) {
pvl = paramList.getValues(input, session);
}
TransformerPool poolToUse = transformerPool;
if (StringUtils.isNotEmpty(styleSheetNameSessionKey)) {
Message styleSheetNameToUse = session.getMessage(styleSheetNameSessionKey);
if (!Message.isEmpty(styleSheetNameToUse)) {
String styleSheetNameFromSessionKey = styleSheetNameToUse.asString();
if (!dynamicTransformerPoolMap.containsKey(styleSheetNameFromSessionKey)) {
dynamicTransformerPoolMap.put(styleSheetNameFromSessionKey, poolToUse = TransformerPool.configureTransformer(getLogPrefix(), this, null, null, styleSheetNameFromSessionKey, null, true, getParameterList()));
poolToUse.open();
} else {
poolToUse = dynamicTransformerPoolMap.get(styleSheetNameFromSessionKey);
}
}
if (poolToUse == null) {
throw new SenderException("no XSLT stylesheet found from styleSheetNameSessionKey [" + styleSheetNameSessionKey + "], and neither one statically configured");
}
}
OutputType outputType = getOutputType();
if (log.isTraceEnabled())
log.trace("Configured outputmethod [" + outputType + "]");
if (outputType == null) {
String parsedOutputType = poolToUse.getOutputMethod();
if (StringUtils.isNotEmpty(parsedOutputType)) {
outputType = EnumUtils.parse(OutputType.class, parsedOutputType);
}
if (log.isTraceEnabled())
log.trace("Detected outputmethod [" + parsedOutputType + "]");
}
if (outputType == null) {
outputType = DEFAULT_OUTPUT_METHOD;
if (log.isTraceEnabled())
log.trace("Default outputmethod [" + outputType + "]");
}
Object targetStream = target.asNative();
Boolean indentXml = getIndentXml();
if (log.isTraceEnabled())
log.trace("Configured indentXml [" + indentXml + "]");
if (indentXml == null) {
indentXml = poolToUse.getIndent();
if (log.isTraceEnabled())
log.trace("Detected indentXml [" + indentXml + "]");
}
if (indentXml == null) {
indentXml = DEFAULT_INDENT;
if (log.isTraceEnabled())
log.trace("Default indentXml [" + indentXml + "]");
}
Boolean omitXmlDeclaration = getOmitXmlDeclaration();
if (targetStream instanceof ContentHandler) {
handler = (ContentHandler) targetStream;
} else {
XmlWriter xmlWriter = new XmlWriter(target.asWriter());
if (log.isTraceEnabled())
log.trace("Configured omitXmlDeclaration [" + omitXmlDeclaration + "]");
if (outputType == OutputType.XML) {
if (omitXmlDeclaration == null) {
omitXmlDeclaration = poolToUse.getOmitXmlDeclaration();
if (log.isTraceEnabled())
log.trace("Detected omitXmlDeclaration [" + omitXmlDeclaration + "]");
if (omitXmlDeclaration == null) {
omitXmlDeclaration = DEFAULT_OMIT_XML_DECLARATION;
if (log.isTraceEnabled())
log.trace("Default omitXmlDeclaration [" + omitXmlDeclaration + "]");
}
}
xmlWriter.setIncludeXmlDeclaration(!omitXmlDeclaration);
if (indentXml) {
xmlWriter.setNewlineAfterXmlDeclaration(true);
}
} else {
xmlWriter.setTextMode(true);
}
handler = xmlWriter;
}
if (indentXml) {
handler = new PrettyPrintFilter(handler);
}
if (isSkipEmptyTags()) {
handler = new SkipEmptyTagsFilter(handler);
}
TransformerFilter mainFilter = poolToUse.getTransformerFilter(threadConnector, handler);
if (pvl != null) {
XmlUtils.setTransformerParameters(mainFilter.getTransformer(), pvl.getValueMap());
}
handler = filterInput(mainFilter, session);
return handler;
} catch (Exception e) {
// log.warn(getLogPrefix()+"intermediate exception logging",e);
throw new StreamingException(getLogPrefix() + "Exception on creating transformerHandler chain", e);
}
}
Aggregations