Search in sources :

Example 1 with XmlTransformer

use of com.adaptris.util.text.xml.XmlTransformer in project interlok by adaptris.

the class XmlTransformerTest method testTransform_InputStreamOutputStream.

@Test
public void testTransform_InputStreamOutputStream() throws Exception {
    XmlTransformerFactory factory = new XsltTransformerFactory();
    XmlTransformer transform = factory.configure(new XmlTransformer());
    String xsl = backslashToSlash(PROPERTIES.getProperty(KEY_XML_TEST_TRANSFORM_URL));
    AdaptrisMessage m1 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_XML_TEST_INPUT));
    try (InputStream in = m1.getInputStream();
        OutputStream out = m1.getOutputStream()) {
        transform.transform(factory.createTransformer(xsl), in, out, xsl);
    }
}
Also used : XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XmlTransformerFactory(com.adaptris.util.text.xml.XmlTransformerFactory) XsltTransformerFactory(com.adaptris.util.text.xml.XsltTransformerFactory) Test(org.junit.Test)

Example 2 with XmlTransformer

use of com.adaptris.util.text.xml.XmlTransformer in project interlok by adaptris.

the class MultiPayloadXmlTransformService method doTransform.

private void doTransform(MultiPayloadAdaptrisMessage msg, String urlToUse) throws ServiceException {
    XmlTransformer xmlTransformerImpl = new XmlTransformer();
    Transformer transformer;
    try {
        if (cacheTransforms()) {
            transformer = cacheAndGetTransformer(urlToUse, getXmlTransformerFactory());
        } else {
            transformer = getXmlTransformerFactory().createTransformer(urlToUse);
        }
        getXmlTransformerFactory().configure(xmlTransformerImpl);
    } catch (Exception ex) {
        throw new ServiceException(ex);
    }
    try (InputStream input = msg.getInputStream(sourcePayloadId);
        OutputStream output = msg.getOutputStream(outputPayloadId)) {
        Map<Object, Object> parameters = getParameterBuilder().createParameters(msg, null);
        xmlTransformerImpl.transform(transformer, input, output, urlToUse, parameters);
        if (!StringUtils.isBlank(getOutputMessageEncoding())) {
            msg.setContentEncoding(outputPayloadId, getOutputMessageEncoding());
        }
    } catch (Exception e) {
        throw new ServiceException("Failed to transform message", e);
    }
}
Also used : XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) Transformer(javax.xml.transform.Transformer) XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) ServiceException(com.adaptris.core.ServiceException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServiceException(com.adaptris.core.ServiceException)

Example 3 with XmlTransformer

use of com.adaptris.util.text.xml.XmlTransformer in project interlok by adaptris.

the class XmlTransformerTest method testTransform_ReaderWriter.

@Test
public void testTransform_ReaderWriter() throws Exception {
    XmlTransformerFactory factory = new XsltTransformerFactory();
    XmlTransformer transform = new XmlTransformer();
    String xsl = backslashToSlash(PROPERTIES.getProperty(KEY_XML_TEST_TRANSFORM_URL));
    AdaptrisMessage m1 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_XML_TEST_INPUT));
    try (Reader in = m1.getReader();
        Writer out = m1.getWriter()) {
        transform.transform(factory.createTransformer(xsl), in, out, xsl);
    }
}
Also used : XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) XmlTransformerFactory(com.adaptris.util.text.xml.XmlTransformerFactory) XsltTransformerFactory(com.adaptris.util.text.xml.XsltTransformerFactory) Reader(java.io.Reader) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with XmlTransformer

use of com.adaptris.util.text.xml.XmlTransformer in project interlok by adaptris.

the class XmlTransformerTest method testTransform.

@Test
public void testTransform() throws Exception {
    XmlTransformerFactory factory = new XsltTransformerFactory();
    XmlTransformer transform = factory.configure(new XmlTransformer());
    transform.registerBuilder(DocumentBuilderFactoryBuilder.newInstance());
    String xsl = backslashToSlash(PROPERTIES.getProperty(KEY_XML_TEST_TRANSFORM_URL));
    AdaptrisMessage m1 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_XML_TEST_INPUT));
    try (InputStream in = m1.getInputStream();
        OutputStream out = m1.getOutputStream()) {
        StreamResult output = new StreamResult(out);
        StreamSource input = new StreamSource(in);
        transform.transform(factory.createTransformer(xsl), input, output, xsl, new HashMap<>(System.getProperties()));
    }
    AdaptrisMessage m2 = MessageHelper.createMessage(PROPERTIES.getProperty(KEY_XML_TEST_INPUT));
    try (InputStream in = m2.getInputStream();
        OutputStream out = m2.getOutputStream()) {
        StreamResult output = new StreamResult(out);
        StreamSource input = new StreamSource(in);
        transform.transform(factory.createTransformer(xsl), input, output, xsl);
    }
}
Also used : XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) StreamResult(javax.xml.transform.stream.StreamResult) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StreamSource(javax.xml.transform.stream.StreamSource) XmlTransformerFactory(com.adaptris.util.text.xml.XmlTransformerFactory) XsltTransformerFactory(com.adaptris.util.text.xml.XsltTransformerFactory) Test(org.junit.Test)

Example 5 with XmlTransformer

use of com.adaptris.util.text.xml.XmlTransformer in project interlok by adaptris.

the class XmlTransformService method doTransform.

private void doTransform(AdaptrisMessage msg, String urlToUse) throws ServiceException {
    XmlTransformer xmlTransformerImpl = new XmlTransformer();
    Transformer transformer = null;
    try {
        if (cacheTransforms()) {
            transformer = cacheAndGetTransformer(urlToUse, getXmlTransformerFactory());
        } else {
            transformer = getXmlTransformerFactory().createTransformer(urlToUse);
        }
        getXmlTransformerFactory().configure(xmlTransformerImpl);
    } catch (Exception ex) {
        throw new ServiceException(ex);
    }
    // INTERLOK-2022 Let the XML parser do its thing, rather than using a reader/writer.
    try (InputStream input = msg.getInputStream();
        OutputStream output = msg.getOutputStream()) {
        Map<Object, Object> parameters = getParameterBuilder().createParameters(msg, null);
        xmlTransformerImpl.transform(transformer, input, output, urlToUse, parameters);
        if (!StringUtils.isBlank(getOutputMessageEncoding())) {
            msg.setContentEncoding(getOutputMessageEncoding());
        }
    } catch (Exception e) {
        throw new ServiceException("failed to transform message", e);
    }
}
Also used : XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) Transformer(javax.xml.transform.Transformer) XmlTransformer(com.adaptris.util.text.xml.XmlTransformer) ServiceException(com.adaptris.core.ServiceException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException)

Aggregations

XmlTransformer (com.adaptris.util.text.xml.XmlTransformer)5 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)3 XmlTransformerFactory (com.adaptris.util.text.xml.XmlTransformerFactory)3 XsltTransformerFactory (com.adaptris.util.text.xml.XsltTransformerFactory)3 Test (org.junit.Test)3 ServiceException (com.adaptris.core.ServiceException)2 Transformer (javax.xml.transform.Transformer)2 CoreException (com.adaptris.core.CoreException)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1