fix for INT-324

This commit is contained in:
Jonas Partner
2008-08-07 14:48:32 +00:00
parent 52f61b3d88
commit 99d26a5b04
6 changed files with 129 additions and 22 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.xml.config;
import org.w3c.dom.Element;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
@@ -26,12 +26,14 @@ import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.StringResultFactory;
import org.springframework.integration.xml.transformer.XmlPayloadMarshallingTransformer;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerParser {
public class XmlMarshallingTransformerParser extends
AbstractPayloadTransformerParser {
@Override
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
@@ -39,18 +41,28 @@ public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerP
}
@Override
protected void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
protected void parsePayloadTransformer(Element element,
ParserContext parserContext, BeanDefinitionBuilder builder) {
String resultFactory = element.getAttribute("result-factory");
String marshaller = element.getAttribute("marshaller");
Assert.hasText(marshaller, "the 'marshaller' attribute is required");
Assert.hasText(resultFactory, "the 'result-factory' attribute is required");
Assert.hasText(resultFactory,
"the 'result-factory' attribute is required");
builder.addConstructorArgReference(marshaller);
if (resultFactory.equals("DOMResult")) {
builder.addPropertyValue("resultFactory", new DomResultFactory());
}
else if (resultFactory.equals("StringResult")) {
builder.addPropertyValue("resultFactory", new StringResultFactory());
try {
builder.addPropertyValue("resultFactory",
new DomResultFactory());
} catch (ParserConfigurationException e) {
throw new org.springframework.integration.ConfigurationException(
"Exception creating DomResultFactory");
}
} else if (resultFactory.equals("StringResult")) {
builder
.addPropertyValue("resultFactory",
new StringResultFactory());
}
}
}

View File

@@ -16,6 +16,9 @@
package org.springframework.integration.xml.result;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
@@ -24,8 +27,18 @@ import javax.xml.transform.dom.DOMResult;
*/
public class DomResultFactory implements ResultFactory {
public Result createResult(Object payload) {
return new DOMResult();
private final DocumentBuilder documentBuilder;
public DomResultFactory(DocumentBuilder documentBuilder){
this.documentBuilder = documentBuilder;
}
public DomResultFactory() throws ParserConfigurationException{
this(DocumentBuilderFactory.newInstance().newDocumentBuilder());
}
public synchronized Result createResult(Object payload) {
return new DOMResult(documentBuilder.newDocument());
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.source;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.springframework.integration.message.MessagingException;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
/**
*
* @author Jonas Partner
*
*/
public class StringSourceFactory implements SourceFactory {
private final TransformerFactory transformerFactory;
public StringSourceFactory() {
this(TransformerFactory.newInstance());
}
public StringSourceFactory(TransformerFactory transformerFactory) {
this.transformerFactory = transformerFactory;
}
public Source createSource(Object payload) {
if (Document.class.isAssignableFrom(payload.getClass())) {
return createStringSourceForDocument((Document) payload);
} else if (payload instanceof String) {
return new StringSource((String) payload);
}
throw new MessagingException(
"Failed to create Source for payload type ["
+ payload.getClass().getName() + "]");
}
protected StringSource createStringSourceForDocument(Document doc) {
try {
StringResult result = new StringResult();
Transformer transformer = getTransformer();
transformer.transform(new DOMSource(doc), result);
return new StringSource(result.toString());
} catch (Exception e) {
throw new MessagingException(
"Exception creating StringSource from document", e);
}
}
protected synchronized Transformer getTransformer() {
try {
return transformerFactory.newTransformer();
} catch (Exception e) {
throw new MessagingException("Exception creating transformer", e);
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.xml.transformer;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import org.springframework.integration.message.MessagingException;
@@ -38,12 +39,13 @@ public class XmlPayloadMarshallingTransformer implements PayloadTransformer<Obje
private final Marshaller marshaller;
private ResultFactory resultFactory = new DomResultFactory();
private ResultFactory resultFactory;
public XmlPayloadMarshallingTransformer(Marshaller marshaller) {
public XmlPayloadMarshallingTransformer(Marshaller marshaller) throws ParserConfigurationException {
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
resultFactory = new DomResultFactory();
}

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.xml.transformer;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
@@ -45,15 +46,16 @@ public class XsltPayloadTransformer implements PayloadTransformer<Object, Result
private SourceFactory sourceFactory = new DomSourceFactory();
private ResultFactory resultFactory = new DomResultFactory();
private ResultFactory resultFactory;
public XsltPayloadTransformer(Templates templates) {
public XsltPayloadTransformer(Templates templates) throws ParserConfigurationException {
this.templates = templates;
resultFactory = new DomResultFactory();
}
public XsltPayloadTransformer(Resource xslResource) throws Exception {
this.templates = TransformerFactory.newInstance().newTemplates(new StreamSource(xslResource.getInputStream()));
this(TransformerFactory.newInstance().newTemplates(new StreamSource(xslResource.getInputStream())));
}

View File

@@ -37,9 +37,10 @@ import org.springframework.xml.transform.StringResult;
public class XmlPayloadMarshallingTransformerTests {
@Test
public void testStringToStringResult() {
public void testStringToStringResult() throws Exception {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(
marshaller);
transformer.setResultFactory(new StringResultFactory());
Object result = transformer.transform("world");
assertEquals(StringResult.class, result.getClass());
@@ -48,15 +49,15 @@ public class XmlPayloadMarshallingTransformerTests {
}
@Test
public void testDefaultResultFactory() {
public void testDefaultResultFactory() throws Exception {
TestMarshaller marshaller = new TestMarshaller();
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(
marshaller);
Object result = transformer.transform("world");
assertEquals(DOMResult.class, result.getClass());
assertEquals("world", marshaller.payloads.get(0));
}
private static class TestMarshaller implements Marshaller {
private List<Object> payloads = new ArrayList<Object>();
@@ -66,7 +67,8 @@ public class XmlPayloadMarshallingTransformerTests {
return true;
}
public void marshal(Object originalPayload, Result result) throws XmlMappingException, IOException {
public void marshal(Object originalPayload, Result result)
throws XmlMappingException, IOException {
payloads.add(originalPayload);
if (result instanceof StringResult) {
((StringResult) result).getWriter().write("hello world");