Search in sources :

Example 1 with TransformerFilter

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);
    }
}
Also used : FullXmlFilter(nl.nn.adapterframework.xml.FullXmlFilter) TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) Attributes(org.xml.sax.Attributes) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) SaxException(nl.nn.adapterframework.xml.SaxException) TransformerPool(nl.nn.adapterframework.util.TransformerPool) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) SaxException(nl.nn.adapterframework.xml.SaxException) SAXException(org.xml.sax.SAXException)

Example 2 with TransformerFilter

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;
}
Also used : TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) HashMap(java.util.HashMap) Resource(nl.nn.adapterframework.core.Resource) TransformerPool(nl.nn.adapterframework.util.TransformerPool)

Example 3 with TransformerFilter

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);
        }
    };
}
Also used : TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) SaxException(nl.nn.adapterframework.xml.SaxException) IOException(java.io.IOException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) ExceptionCatchingFilter(nl.nn.adapterframework.xml.ExceptionCatchingFilter) TransformerException(javax.xml.transform.TransformerException) SaxException(nl.nn.adapterframework.xml.SaxException) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) NamespaceRemovingFilter(nl.nn.adapterframework.xml.NamespaceRemovingFilter) SAXException(org.xml.sax.SAXException) NodeSetFilter(nl.nn.adapterframework.xml.NodeSetFilter) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) TransformerException(javax.xml.transform.TransformerException)

Example 4 with TransformerFilter

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);
    }
}
Also used : TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) StreamingException(nl.nn.adapterframework.stream.StreamingException) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message) ContentHandler(org.xml.sax.ContentHandler) TransformerPool(nl.nn.adapterframework.util.TransformerPool) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) SkipEmptyTagsFilter(nl.nn.adapterframework.xml.SkipEmptyTagsFilter) SenderException(nl.nn.adapterframework.core.SenderException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) OutputType(nl.nn.adapterframework.util.TransformerPool.OutputType) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Aggregations

TransformerFilter (nl.nn.adapterframework.xml.TransformerFilter)4 TransformerPool (nl.nn.adapterframework.util.TransformerPool)3 SAXException (org.xml.sax.SAXException)3 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 SenderException (nl.nn.adapterframework.core.SenderException)2 StreamingException (nl.nn.adapterframework.stream.StreamingException)2 SaxException (nl.nn.adapterframework.xml.SaxException)2 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)1 PipeStartException (nl.nn.adapterframework.core.PipeStartException)1 Resource (nl.nn.adapterframework.core.Resource)1 TimeoutException (nl.nn.adapterframework.core.TimeoutException)1 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)1 Message (nl.nn.adapterframework.stream.Message)1