Search in sources :

Example 11 with Templates

use of javax.xml.transform.Templates in project nifi by apache.

the class TransformXml method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final FlowFile original = session.get();
    if (original == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final StopWatch stopWatch = new StopWatch(true);
    final String xsltFileName = context.getProperty(XSLT_FILE_NAME).evaluateAttributeExpressions(original).getValue();
    final Boolean indentOutput = context.getProperty(INDENT_OUTPUT).asBoolean();
    try {
        FlowFile transformed = session.write(original, new StreamCallback() {

            @Override
            public void process(final InputStream rawIn, final OutputStream out) throws IOException {
                try (final InputStream in = new BufferedInputStream(rawIn)) {
                    final Templates templates;
                    if (cache != null) {
                        templates = cache.get(xsltFileName);
                    } else {
                        templates = newTemplates(context, xsltFileName);
                    }
                    final Transformer transformer = templates.newTransformer();
                    transformer.setOutputProperty(OutputKeys.INDENT, (indentOutput ? "yes" : "no"));
                    // pass all dynamic properties to the transformer
                    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
                        if (entry.getKey().isDynamic()) {
                            String value = context.newPropertyValue(entry.getValue()).evaluateAttributeExpressions(original).getValue();
                            transformer.setParameter(entry.getKey().getName(), value);
                        }
                    }
                    // use a StreamSource with Saxon
                    StreamSource source = new StreamSource(in);
                    StreamResult result = new StreamResult(out);
                    transformer.transform(source, result);
                } catch (final Exception e) {
                    throw new IOException(e);
                }
            }
        });
        session.transfer(transformed, REL_SUCCESS);
        session.getProvenanceReporter().modifyContent(transformed, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        logger.info("Transformed {}", new Object[] { original });
    } catch (ProcessException e) {
        logger.error("Unable to transform {} due to {}", new Object[] { original, e });
        session.transfer(original, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) ComponentLog(org.apache.nifi.logging.ComponentLog) StreamCallback(org.apache.nifi.processor.io.StreamCallback) ProcessException(org.apache.nifi.processor.exception.ProcessException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StopWatch(org.apache.nifi.util.StopWatch) ProcessException(org.apache.nifi.processor.exception.ProcessException) BufferedInputStream(org.apache.nifi.stream.io.BufferedInputStream)

Example 12 with Templates

use of javax.xml.transform.Templates in project cxf by apache.

the class XSLTJaxbProvider method unmarshalFromInputStream.

@Override
protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt) throws JAXBException {
    try {
        Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
        if (t == null && supportJaxbOnly) {
            return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
        }
        if (unmarshaller.getClass().getName().contains("eclipse")) {
            // eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
            // so we need to bounce through a DOM
            Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
            DOMResult dom = new DOMResult();
            t.newTransformer().transform(reader, dom);
            return unmarshaller.unmarshal(dom.getNode());
        }
        XMLFilter filter = null;
        try {
            filter = factory.newXMLFilter(t);
        } catch (TransformerConfigurationException ex) {
            TemplatesImpl ti = (TemplatesImpl) t;
            filter = factory.newXMLFilter(ti.getTemplates());
            trySettingProperties(filter, ti);
        }
        XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
        filter.setParent(reader);
        SAXSource source = new SAXSource();
        source.setXMLReader(filter);
        if (systemId != null) {
            source.setSystemId(systemId);
        }
        return unmarshaller.unmarshal(source);
    } catch (TransformerException ex) {
        LOG.warning("Transformation exception : " + ex.getMessage());
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StaxSource(org.apache.cxf.staxutils.StaxSource) XMLFilter(org.xml.sax.XMLFilter) Templates(javax.xml.transform.Templates) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 13 with Templates

use of javax.xml.transform.Templates in project cxf by apache.

the class XSLTJaxbProvider method getAnnotationTemplates.

protected Templates getAnnotationTemplates(Annotation[] anns) {
    Templates t = null;
    XSLTTransform ann = AnnotationUtils.getAnnotation(anns, XSLTTransform.class);
    if (ann != null) {
        t = annotationTemplates.get(ann.value());
    }
    return t;
}
Also used : XSLTTransform(org.apache.cxf.jaxrs.ext.xml.XSLTTransform) Templates(javax.xml.transform.Templates)

Example 14 with Templates

use of javax.xml.transform.Templates in project cxf by apache.

the class XSLTJaxbProvider method marshalToOutputStream.

@Override
protected void marshalToOutputStream(Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt) throws Exception {
    Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
    if (t == null && supportJaxbOnly) {
        super.marshalToOutputStream(ms, obj, os, anns, mt);
        return;
    }
    org.apache.cxf.common.jaxb.JAXBUtils.setMinimumEscapeHandler(ms);
    TransformerHandler th = null;
    try {
        th = factory.newTransformerHandler(t);
    } catch (TransformerConfigurationException ex) {
        TemplatesImpl ti = (TemplatesImpl) t;
        th = factory.newTransformerHandler(ti.getTemplates());
        this.trySettingProperties(th, ti);
    }
    Result result = new StreamResult(os);
    if (systemId != null) {
        result.setSystemId(systemId);
    }
    th.setResult(result);
    if (getContext() == null) {
        th.startDocument();
    }
    ms.marshal(obj, th);
    if (getContext() == null) {
        th.endDocument();
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Example 15 with Templates

use of javax.xml.transform.Templates in project cxf by apache.

the class XSLTJaxbProvider method getTemplatesFromAnnotation.

protected Templates getTemplatesFromAnnotation(Class<?> cls, Annotation[] anns, MediaType mt) {
    Templates t = null;
    XSLTTransform ann = getXsltTransformAnn(anns, mt);
    if (ann != null) {
        t = annotationTemplates.get(ann.value());
        if (t == null || refreshTemplates) {
            String path = ann.value();
            final String cp = "classpath:";
            if (!path.startsWith(cp)) {
                path = cp + path;
            }
            t = createTemplates(path);
            if (t == null) {
                createTemplates(ClassLoaderUtils.getResource(ann.value(), cls));
            }
            if (t != null) {
                annotationTemplates.put(ann.value(), t);
            }
        }
    }
    return t;
}
Also used : XSLTTransform(org.apache.cxf.jaxrs.ext.xml.XSLTTransform) Templates(javax.xml.transform.Templates)

Aggregations

Templates (javax.xml.transform.Templates)60 TransformerFactory (javax.xml.transform.TransformerFactory)25 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)20 Source (javax.xml.transform.Source)19 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)17 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3