diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java new file mode 100644 index 0000000000..02abf09b77 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java @@ -0,0 +1,96 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.xml.transformer; + +import javax.xml.transform.Source; +import javax.xml.transform.Templates; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; + +import org.springframework.core.io.Resource; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.transformer.MessageTransformer; +import org.springframework.xml.transform.StringResult; +import org.springframework.xml.transform.StringSource; +import org.w3c.dom.Document; + +/** + * Simple Xslt transformer implementation which returns a transformed {@link Source} a {@link Document} or a {@link String} + * @author Jonas Partner + * + */ +public class XsltPayloadTransformer implements MessageTransformer { + + private final Templates templates; + + public XsltPayloadTransformer(Templates templates) { + this.templates = templates; + } + + public XsltPayloadTransformer(Resource xslResource) throws Exception { + this.templates = TransformerFactory.newInstance().newTemplates(new StreamSource(xslResource.getInputStream())); + } + + + @SuppressWarnings("unchecked") + public void transform(Message message) { + try{ + + if( (Document.class.isAssignableFrom(message.getPayload().getClass()))){ + transformDocument(message); + } else if(Source.class.isAssignableFrom(message.getPayload().getClass())){ + transformSource(message); + } else if (String.class.equals(message.getPayload().getClass())){ + transformString(message); + } else { + throw new MessagingException(message,"Unsupproted payload type for transformation " + message.getPayload().getClass().getName()); + } + + } catch (TransformerException transE){ + throw new MessagingException(message,"XSLT transformation failed", transE); + } + + } + + @SuppressWarnings("unchecked") + protected void transformSource(Message message) throws TransformerException { + StringResult result = new StringResult(); + templates.newTransformer().transform((Source)message.getPayload(), result); + message.setPayload(new StringSource(result.toString())); + } + + + @SuppressWarnings("unchecked") + private void transformString(Message message) throws TransformerException { + StringResult result = new StringResult(); + templates.newTransformer().transform(new StringSource((String)message.getPayload()), result); + message.setPayload(result.toString()); + } + + @SuppressWarnings("unchecked") + protected void transformDocument(Message message) throws TransformerException{ + Document doc = (Document)message.getPayload(); + DOMResult result = new DOMResult(); + templates.newTransformer().transform(new DOMSource(doc), result); + message.setPayload(result.getNode()); + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java new file mode 100644 index 0000000000..0e270d9b9f --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.xml.transformer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.message.StringMessage; +import org.springframework.xml.transform.StringSource; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +/** + * + * @author Jonas Partner + * + */ +public class XsltPayloadTransformerTest { + + XsltPayloadTransformer transformer; + + @Before + public void setUp() throws Exception{ + transformer = new XsltPayloadTransformer(getXslResource()); + } + + @Test + public void testDocumentAsPayload() throws Exception { + Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + new InputSource(new StringReader(getInputString()))); + GenericMessage documentMessage = new GenericMessage(input); + transformer.transform(documentMessage); + String rootNodeName = ((Document)documentMessage.getPayload()).getDocumentElement().getNodeName(); + assertEquals("Wrong name for root element after transform", "bob", rootNodeName); + } + + @Test + public void testXmlAsStringPayload() throws Exception { + StringMessage message = new StringMessage(getInputString()); + transformer.transform(message); + String rootNodeName = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(message.getPayload()))).getDocumentElement().getNodeName(); + assertEquals("Wrong name for root element after transform", "bob", rootNodeName); + } + + @Test + public void testSourceAsPayload() throws Exception { + GenericMessage message = new GenericMessage(new StringSource(getInputString())); + transformer.transform(message); + + DOMResult result = new DOMResult(); + TransformerFactory.newInstance().newTransformer().transform(message.getPayload(), result); + String rootNodeName = ((Document)result.getNode()).getDocumentElement().getNodeName(); + assertEquals("Wrong name for root element after transform", "bob", rootNodeName); + } + + @Test(expected = MessagingException.class) + public void testNonXmlString() throws Exception { + transformer.transform(new StringMessage("test")); + } + + @Test(expected = MessagingException.class) + public void testUnsupportedPayloadType() throws Exception { + transformer.transform(new GenericMessage(new Long(12))); + } + + + + + + public String getInputString() { + return "test"; + } + + public Resource getXslResource() throws Exception{ + String xsl = "test"; + return new ByteArrayResource(xsl.getBytes("UTF-8")); + } + +}