Search in sources :

Example 1 with OutputType

use of nl.nn.adapterframework.util.TransformerPool.OutputType 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 2 with OutputType

use of nl.nn.adapterframework.util.TransformerPool.OutputType in project iaf by ibissource.

the class Parameter method configure.

@Override
public void configure() throws ConfigurationException {
    if (StringUtils.isNotEmpty(getSessionKey()) && StringUtils.isNotEmpty(getSessionKeyXPath())) {
        throw new ConfigurationException("Parameter [" + getName() + "] cannot have both sessionKey and sessionKeyXPath specified");
    }
    if (StringUtils.isNotEmpty(getXpathExpression()) || StringUtils.isNotEmpty(styleSheetName)) {
        if (paramList != null) {
            paramList.configure();
        }
        OutputType outputType = getType() == ParameterType.XML || getType() == ParameterType.NODE || getType() == ParameterType.DOMDOC ? OutputType.XML : OutputType.TEXT;
        boolean includeXmlDeclaration = false;
        transformerPool = TransformerPool.configureTransformer0("Parameter [" + getName() + "] ", this, getNamespaceDefs(), getXpathExpression(), getStyleSheetName(), outputType, includeXmlDeclaration, paramList, getXsltVersion());
    } else {
        if (paramList != null && StringUtils.isEmpty(getXpathExpression())) {
            throw new ConfigurationException("Parameter [" + getName() + "] can only have parameters itself if a styleSheetName or xpathExpression is specified");
        }
    }
    if (isRemoveNamespaces()) {
        transformerPoolRemoveNamespaces = XmlUtils.getRemoveNamespacesTransformerPool(true, false);
    }
    if (StringUtils.isNotEmpty(getSessionKeyXPath())) {
        tpDynamicSessionKey = TransformerPool.configureTransformer("SessionKey for parameter [" + getName() + "] ", this, getNamespaceDefs(), getSessionKeyXPath(), null, OutputType.TEXT, false, null);
    }
    if (getType() == null) {
        log.info("parameter [" + getName() + " has no type. Setting the type to [" + ParameterType.STRING + "]");
        setType(ParameterType.STRING);
    }
    if (StringUtils.isEmpty(getFormatString())) {
        switch(getType()) {
            case DATE:
                setFormatString(TYPE_DATE_PATTERN);
                break;
            case DATETIME:
                setFormatString(TYPE_DATETIME_PATTERN);
                break;
            case TIMESTAMP:
                setFormatString(TYPE_TIMESTAMP_PATTERN);
                break;
            case TIME:
                setFormatString(TYPE_TIME_PATTERN);
                break;
            default:
                break;
        }
    }
    if (getType() == ParameterType.NUMBER) {
        decimalFormatSymbols = new DecimalFormatSymbols();
        if (StringUtils.isNotEmpty(getDecimalSeparator())) {
            decimalFormatSymbols.setDecimalSeparator(getDecimalSeparator().charAt(0));
        }
        if (StringUtils.isNotEmpty(getGroupingSeparator())) {
            decimalFormatSymbols.setGroupingSeparator(getGroupingSeparator().charAt(0));
        }
    }
    configured = true;
    if (getMinInclusiveString() != null || getMaxInclusiveString() != null) {
        if (getType() != ParameterType.NUMBER) {
            throw new ConfigurationException("minInclusive and maxInclusive only allowed in combination with type [" + ParameterType.NUMBER + "]");
        }
        if (getMinInclusiveString() != null) {
            DecimalFormat df = new DecimalFormat();
            df.setDecimalFormatSymbols(decimalFormatSymbols);
            try {
                minInclusive = df.parse(getMinInclusiveString());
            } catch (ParseException e) {
                throw new ConfigurationException("Attribute [minInclusive] could not parse result [" + getMinInclusiveString() + "] to number; decimalSeparator [" + decimalFormatSymbols.getDecimalSeparator() + "] groupingSeparator [" + decimalFormatSymbols.getGroupingSeparator() + "]", e);
            }
        }
        if (getMaxInclusiveString() != null) {
            DecimalFormat df = new DecimalFormat();
            df.setDecimalFormatSymbols(decimalFormatSymbols);
            try {
                maxInclusive = df.parse(getMaxInclusiveString());
            } catch (ParseException e) {
                throw new ConfigurationException("Attribute [maxInclusive] could not parse result [" + getMaxInclusiveString() + "] to number; decimalSeparator [" + decimalFormatSymbols.getDecimalSeparator() + "] groupingSeparator [" + decimalFormatSymbols.getGroupingSeparator() + "]", e);
            }
        }
    }
    if (StringUtils.isNotEmpty(getAuthAlias()) || StringUtils.isNotEmpty(getUsername()) || StringUtils.isNotEmpty(getPassword())) {
        cf = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) DecimalFormat(java.text.DecimalFormat) ParseException(java.text.ParseException) OutputType(nl.nn.adapterframework.util.TransformerPool.OutputType)

Aggregations

ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 OutputType (nl.nn.adapterframework.util.TransformerPool.OutputType)2 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 ParseException (java.text.ParseException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SenderException (nl.nn.adapterframework.core.SenderException)1 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)1 Message (nl.nn.adapterframework.stream.Message)1 StreamingException (nl.nn.adapterframework.stream.StreamingException)1 CredentialFactory (nl.nn.adapterframework.util.CredentialFactory)1 TransformerPool (nl.nn.adapterframework.util.TransformerPool)1 PrettyPrintFilter (nl.nn.adapterframework.xml.PrettyPrintFilter)1 SkipEmptyTagsFilter (nl.nn.adapterframework.xml.SkipEmptyTagsFilter)1 TransformerFilter (nl.nn.adapterframework.xml.TransformerFilter)1 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)1 ContentHandler (org.xml.sax.ContentHandler)1 SAXException (org.xml.sax.SAXException)1