Search in sources :

Example 6 with PrettyPrintFilter

use of nl.nn.adapterframework.xml.PrettyPrintFilter 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)

Example 7 with PrettyPrintFilter

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

the class XmlUtils method canonicalize.

public static String canonicalize(String input) throws IOException {
    XmlWriter xmlWriter = new XmlWriter();
    xmlWriter.setIncludeComments(false);
    ContentHandler handler = new PrettyPrintFilter(xmlWriter);
    handler = new CanonicalizeFilter(handler);
    try {
        XmlUtils.parseXml(input, handler);
        return xmlWriter.toString();
    } catch (SAXException e) {
        throw new IOException("ERROR: could not canonicalize [" + input + "]", e);
    }
}
Also used : CanonicalizeFilter(nl.nn.adapterframework.xml.CanonicalizeFilter) IOException(java.io.IOException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) XmlValidatorContentHandler(nl.nn.adapterframework.validation.XmlValidatorContentHandler) ContentHandler(org.xml.sax.ContentHandler) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) SAXException(org.xml.sax.SAXException)

Example 8 with PrettyPrintFilter

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

the class JdbcQuerySenderBase method getResult.

protected PipeRunResult getResult(ResultSet resultset, Object blobSessionVar, Object clobSessionVar, HttpServletResponse response, String contentType, String contentDisposition, PipeLineSession session, IForwardTarget next) throws JdbcException, SQLException, IOException {
    if (isScalar()) {
        String result = null;
        if (resultset.next()) {
            // result = resultset.getString(1);
            ResultSetMetaData rsmeta = resultset.getMetaData();
            int numberOfColumns = rsmeta.getColumnCount();
            if (numberOfColumns > 1) {
                log.warn(getLogPrefix() + "has set scalar=true but the resultset contains [" + numberOfColumns + "] columns. Consider optimizing the query.");
            }
            if (getDbmsSupport().isBlobType(rsmeta, 1)) {
                if (response != null) {
                    if (StringUtils.isNotEmpty(contentType)) {
                        response.setHeader("Content-Type", contentType);
                    }
                    if (StringUtils.isNotEmpty(contentDisposition)) {
                        response.setHeader("Content-Disposition", contentDisposition);
                    }
                    JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), response.getOutputStream(), isCloseOutputstreamOnExit());
                    return new PipeRunResult(null, Message.nullMessage());
                }
                if (blobSessionVar != null) {
                    JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), blobSessionVar, isCloseOutputstreamOnExit());
                    return new PipeRunResult(null, Message.nullMessage());
                }
                if (!isBlobSmartGet()) {
                    try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
                        if (StringUtils.isNotEmpty(getBlobCharset())) {
                            JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), target.asWriter(), isCloseOutputstreamOnExit());
                        } else {
                            JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, null, isBlobsCompressed(), getBlobBase64Direction(), target.asStream(), isCloseOutputstreamOnExit());
                        }
                        return target.getPipeRunResult();
                    } catch (Exception e) {
                        throw new JdbcException(e);
                    }
                }
            }
            if (getDbmsSupport().isClobType(rsmeta, 1)) {
                if (clobSessionVar != null) {
                    JdbcUtil.streamClob(getDbmsSupport(), resultset, 1, clobSessionVar, isCloseOutputstreamOnExit());
                    return new PipeRunResult(null, Message.nullMessage());
                }
                try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
                    JdbcUtil.streamClob(getDbmsSupport(), resultset, 1, target.asWriter(), isCloseOutputstreamOnExit());
                    return target.getPipeRunResult();
                } catch (Exception e) {
                    throw new JdbcException(e);
                }
            }
            result = JdbcUtil.getValue(getDbmsSupport(), resultset, 1, rsmeta, getBlobCharset(), isBlobsCompressed(), getNullValue(), isTrimSpaces(), isBlobSmartGet(), getBlobBase64Direction() == Direction.ENCODE);
            if (resultset.wasNull()) {
                if (isScalarExtended()) {
                    result = "[null]";
                } else {
                    result = null;
                }
            } else {
                if (result.length() == 0 && isScalarExtended()) {
                    result = "[empty]";
                }
            }
            if (resultset.next()) {
                log.warn(getLogPrefix() + "has set scalar=true but the query returned more than 1 row. Consider optimizing the query.");
            }
        } else if (isScalarExtended()) {
            result = "[absent]";
        }
        return new PipeRunResult(null, new Message(result));
    }
    try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
        // Create XML and give the maxlength as a parameter
        DB2XMLWriter db2xml = new DB2XMLWriter();
        db2xml.setNullValue(getNullValue());
        db2xml.setTrimSpaces(isTrimSpaces());
        if (StringUtils.isNotEmpty(getBlobCharset()))
            db2xml.setBlobCharset(getBlobCharset());
        db2xml.setDecompressBlobs(isBlobsCompressed());
        db2xml.setGetBlobSmart(isBlobSmartGet());
        ContentHandler handler = target.asContentHandler();
        if (isPrettyPrint()) {
            handler = new PrettyPrintFilter(handler);
        }
        db2xml.getXML(getDbmsSupport(), resultset, getMaxRows(), isIncludeFieldDefinition(), handler);
        return target.getPipeRunResult();
    } catch (Exception e) {
        throw new JdbcException(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) DB2XMLWriter(nl.nn.adapterframework.util.DB2XMLWriter) Message(nl.nn.adapterframework.stream.Message) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ParseException(java.text.ParseException) StreamingException(nl.nn.adapterframework.stream.StreamingException) JMSException(javax.jms.JMSException) SenderException(nl.nn.adapterframework.core.SenderException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) ContentHandler(org.xml.sax.ContentHandler) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Example 9 with PrettyPrintFilter

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

the class DB2XMLWriter method getXML.

public String getXML(IDbmsSupport dbmsSupport, ResultSet rs, int maxlength, boolean includeFieldDefinition) {
    try {
        XmlWriter xmlWriter = new XmlWriter();
        PrettyPrintFilter ppf = new PrettyPrintFilter(xmlWriter);
        getXML(dbmsSupport, rs, maxlength, includeFieldDefinition, ppf);
        return xmlWriter.toString();
    } catch (SAXException e) {
        log.warn("cannot convert ResultSet to XML", e);
        return "<error>" + XmlUtils.encodeCharsAndReplaceNonValidXmlCharacters(e.getMessage()) + "</error>";
    }
}
Also used : XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) SAXException(org.xml.sax.SAXException)

Example 10 with PrettyPrintFilter

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

the class JdbcUtilTest method testWarningsToXml.

@Test
public void testWarningsToXml() throws SAXException {
    String expected = getExpectedWarningXml();
    XmlWriter writer = new XmlWriter();
    PrettyPrintFilter ppf = new PrettyPrintFilter(writer);
    try (SaxElementBuilder seb = new SaxElementBuilder(ppf)) {
        JdbcUtil.warningsToXml(getWarnings(), seb);
        MatchUtils.assertXmlEquals(expected, writer.toString());
    }
}
Also used : SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) Test(org.junit.Test)

Aggregations

PrettyPrintFilter (nl.nn.adapterframework.xml.PrettyPrintFilter)12 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)11 ContentHandler (org.xml.sax.ContentHandler)8 SAXException (org.xml.sax.SAXException)6 IOException (java.io.IOException)5 XmlTee (nl.nn.adapterframework.stream.xml.XmlTee)4 Writer (java.io.Writer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringWriter (java.io.StringWriter)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 SenderException (nl.nn.adapterframework.core.SenderException)2 Message (nl.nn.adapterframework.stream.Message)2 StreamingException (nl.nn.adapterframework.stream.StreamingException)2 JsonTee (nl.nn.adapterframework.stream.json.JsonTee)2 JsonWriter (nl.nn.adapterframework.stream.json.JsonWriter)2 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1