Search in sources :

Example 1 with NamespaceRemovingFilter

use of nl.nn.adapterframework.xml.NamespaceRemovingFilter in project iaf by ibissource.

the class MatchUtils method xmlPretty.

public static String xmlPretty(String xml, boolean removeNamespaces) {
    XmlWriter xmlWriter = new XmlWriter();
    xmlWriter.setIncludeComments(false);
    ContentHandler contentHandler = new PrettyPrintFilter(xmlWriter);
    if (removeNamespaces) {
        contentHandler = new NamespaceRemovingFilter(contentHandler);
    }
    try {
        XmlUtils.parseXml(xml, contentHandler);
        return xmlWriter.toString();
    } catch (IOException | SAXException e) {
        throw new RuntimeException("ERROR: could not prettify [" + xml + "]", e);
    }
}
Also used : IOException(java.io.IOException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) ContentHandler(org.xml.sax.ContentHandler) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) NamespaceRemovingFilter(nl.nn.adapterframework.xml.NamespaceRemovingFilter) SAXException(org.xml.sax.SAXException)

Example 2 with NamespaceRemovingFilter

use of nl.nn.adapterframework.xml.NamespaceRemovingFilter 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 3 with NamespaceRemovingFilter

use of nl.nn.adapterframework.xml.NamespaceRemovingFilter in project iaf by ibissource.

the class Json2XmlValidator method alignJson.

protected PipeRunResult alignJson(String messageToValidate, PipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
    ValidationContext context;
    ValidatorHandler validatorHandler;
    try {
        context = validator.createValidationContext(session, getJsonRootValidations(responseMode), getInvalidRootNamespaces());
        validatorHandler = validator.getValidatorHandler(session, context);
    } catch (ConfigurationException e) {
        throw new PipeRunException(this, "Cannot create ValidationContext", e);
    }
    String resultEvent;
    String out = null;
    try {
        Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
        if (StringUtils.isNotEmpty(getTargetNamespace())) {
            aligner.setTargetNamespace(getTargetNamespace());
        }
        aligner.setDeepSearch(isDeepSearch());
        aligner.setErrorHandler(context.getErrorHandler());
        aligner.setFailOnWildcards(isFailOnWildcards());
        aligner.setIgnoreUndeclaredElements(isIgnoreUndeclaredElements());
        ParameterList parameterList = getParameterList();
        if (parameterList != null) {
            Map<String, Object> parametervalues = null;
            parametervalues = parameterList.getValues(new Message(messageToValidate), session).getValueMap();
            // remove parameters with null values, to support optional request parameters
            for (Iterator<String> it = parametervalues.keySet().iterator(); it.hasNext(); ) {
                String key = it.next();
                if (parametervalues.get(key) == null) {
                    it.remove();
                }
            }
            aligner.setOverrideValues(parametervalues);
        }
        JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
        // cannot build filter chain as usual backwardly, because it ends differently.
        // This will be fixed once an OutputStream can be provided to Xml2Json
        XMLFilterImpl sourceFilter = aligner;
        if (StringUtils.isNotEmpty(getRootElementSessionKey())) {
            XMLFilterImpl storeRootFilter = new RootElementToSessionKeyFilter(session, getRootElementSessionKey(), getRootNamespaceSessionKey(), null);
            aligner.setContentHandler(storeRootFilter);
            sourceFilter = storeRootFilter;
        }
        if (getOutputFormat(session, responseMode) == DocumentFormat.JSON) {
            Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
            sourceFilter.setContentHandler(xml2json);
            aligner.startParse(jsonStructure);
            out = xml2json.toString();
        } else {
            XmlWriter xmlWriter = new XmlWriter();
            xmlWriter.setIncludeXmlDeclaration(true);
            ContentHandler handler = xmlWriter;
            if (isProduceNamespaceLessXml()) {
                handler = new NamespaceRemovingFilter(handler);
            }
            sourceFilter.setContentHandler(handler);
            aligner.startParse(jsonStructure);
            out = xmlWriter.toString();
        }
    } catch (Exception e) {
        resultEvent = validator.finalizeValidation(context, session, e);
    }
    resultEvent = validator.finalizeValidation(context, session, null);
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : RootElementToSessionKeyFilter(nl.nn.adapterframework.xml.RootElementToSessionKeyFilter) ValidatorHandler(javax.xml.validation.ValidatorHandler) Message(nl.nn.adapterframework.stream.Message) PipeForward(nl.nn.adapterframework.core.PipeForward) ContentHandler(org.xml.sax.ContentHandler) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) ValidationContext(nl.nn.adapterframework.validation.ValidationContext) NamespaceRemovingFilter(nl.nn.adapterframework.xml.NamespaceRemovingFilter) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) ParameterList(nl.nn.adapterframework.parameters.ParameterList) JsonObject(javax.json.JsonObject) Xml2Json(nl.nn.adapterframework.align.Xml2Json) Json2Xml(nl.nn.adapterframework.align.Json2Xml) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) JsonStructure(javax.json.JsonStructure)

Aggregations

IOException (java.io.IOException)3 NamespaceRemovingFilter (nl.nn.adapterframework.xml.NamespaceRemovingFilter)3 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)2 ContentHandler (org.xml.sax.ContentHandler)2 SAXException (org.xml.sax.SAXException)2 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 JsonObject (javax.json.JsonObject)1 JsonStructure (javax.json.JsonStructure)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 ValidatorHandler (javax.xml.validation.ValidatorHandler)1 Json2Xml (nl.nn.adapterframework.align.Json2Xml)1 Xml2Json (nl.nn.adapterframework.align.Xml2Json)1 PipeForward (nl.nn.adapterframework.core.PipeForward)1 PipeRunException (nl.nn.adapterframework.core.PipeRunException)1 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)1 PipeStartException (nl.nn.adapterframework.core.PipeStartException)1 SenderException (nl.nn.adapterframework.core.SenderException)1