added ResultToStringTransformer

This commit is contained in:
Jonas Partner
2008-08-19 16:36:39 +00:00
parent 3f79525907
commit 17832c2913
5 changed files with 170 additions and 8 deletions

View File

@@ -18,7 +18,9 @@ package org.springframework.integration.xml.source;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
@@ -69,8 +71,7 @@ public class DomSourceFactory implements SourceFactory {
protected DOMSource createDomSourceForString(String s) {
try {
Document doc = docBuilderFactory.newDocumentBuilder().parse(
new InputSource(new StringReader(s)));
Document doc = getNewDocumentBuilder().parse(new InputSource(new StringReader(s)));
DOMSource source = new DOMSource(doc.getDocumentElement());
return source;
} catch (Exception e) {
@@ -78,4 +79,10 @@ public class DomSourceFactory implements SourceFactory {
}
}
protected DocumentBuilder getNewDocumentBuilder() throws ParserConfigurationException{
synchronized (docBuilderFactory) {
return docBuilderFactory.newDocumentBuilder();
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2007 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.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.springframework.integration.message.MessagingException;
import org.springframework.xml.transform.StringResult;
/**
* Converts the passed {@link Result} to an instance of {@link String}
*
* Supports {@link StringResult} and {@link DOMResult}
*
* @author Jonas Partner
*
*/
public class ResultToStringTransformer implements ResultTransformer {
private DocumentBuilderFactory docBuilderFactory;
private TransformerFactory transformerFactory;
public ResultToStringTransformer() {
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
this.transformerFactory = TransformerFactory.newInstance();
}
protected Transformer getNewTransformer()
throws TransformerConfigurationException {
synchronized (transformerFactory) {
return transformerFactory.newTransformer();
}
}
public Object transformResult(Result res) {
String returnString = null;
if (res instanceof StringResult) {
returnString = ((StringResult) res).toString();
} else if (res instanceof DOMResult) {
try {
StringResult strRes = new StringResult();
getNewTransformer().transform(
new DOMSource(((DOMResult) res).getNode()), strRes);
returnString = strRes.toString();
} catch (TransformerException transE) {
throw new MessagingException(
"Transformation from DOMSOurce failed", transE);
}
}
if (returnString == null) {
throw new MessagingException("Could not convert Result type "
+ res.getClass().getName() + " to string");
}
return returnString;
}
protected DocumentBuilder getNewDocumentBuilder()
throws ParserConfigurationException {
synchronized (docBuilderFactory) {
return docBuilderFactory.newDocumentBuilder();
}
}
}

View File

@@ -45,7 +45,8 @@ public class XmlPayloadMarshallingTransformer implements
private final ResultTransformer resultTransformer;
public XmlPayloadMarshallingTransformer(Marshaller marshaller,
ResultTransformer resultTransformer) throws ParserConfigurationException {
ResultTransformer resultTransformer)
throws ParserConfigurationException {
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
this.resultTransformer = resultTransformer;
@@ -78,10 +79,9 @@ public class XmlPayloadMarshallingTransformer implements
if (transformedPayload == null) {
throw new MessagingException("Failed to transform payload");
}
if(resultTransformer != null){
if (resultTransformer != null) {
transformedPayload = resultTransformer.transformResult(result);
}
return transformedPayload;
}

View File

@@ -35,7 +35,7 @@ import org.springframework.xml.transform.StringResult;
*/
public class ResultToDocumentTransformerTests {
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
private String startDoc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
private ResultToDocumentTransformer resToDocTransformer;
@@ -48,7 +48,7 @@ public class ResultToDocumentTransformerTests {
@Test
public void testWithDomResult() throws Exception {
DOMResult result = XmlTestUtil.getDomResultForString(doc);
DOMResult result = XmlTestUtil.getDomResultForString(startDoc);
Object transformed = resToDocTransformer.transformResult(result);
assertTrue("Wrong transformed type expected Document", transformed instanceof Document);
Document doc = (Document) transformed;
@@ -57,7 +57,7 @@ public class ResultToDocumentTransformerTests {
@Test
public void testWithStringResult() throws Exception {
StringResult result = XmlTestUtil.getStringResultForString(doc);
StringResult result = XmlTestUtil.getStringResultForString(startDoc);
Object transformed = resToDocTransformer.transformResult(result);
assertTrue("Wrong transformed type expected Document", transformed instanceof Document);
Document doc = (Document) transformed;

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2002-2007 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.custommonkey.xmlunit.XMLAssert.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
public class ResultToStringTransfomerTests {
ResultToStringTransformer transformer;
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
@Before
public void setUp(){
transformer = new ResultToStringTransformer();
}
@Test
public void testWithDomResult() throws Exception {
DOMResult result = XmlTestUtil.getDomResultForString(doc);
Object transformed = transformer.transformResult(result);
assertTrue("Wrong transformed type expected String", transformed instanceof String);
String transformedString = (String) transformed;
assertXMLEqual("Wrong content", doc, transformedString);
}
@Test
public void testWithStringResult() throws Exception {
StringResult result = XmlTestUtil.getStringResultForString(doc);
Object transformed = transformer.transformResult(result);
assertTrue("Wrong transformed type expected String", transformed instanceof String);
String transformedString = (String) transformed;
assertXMLEqual("Wrong content", doc, transformedString);
}
@Test(expected = MessagingException.class)
public void testWithUnsupportedSaxResult() throws Exception {
SAXResult result = new SAXResult();
transformer.transformResult(result);
}
}