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);
}
}
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);
}
}
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;
}
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();
}
}
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;
}
Aggregations