Added ability to specify a result-factory rather than a result-type on XmlPayloadMarshallingTransformer
relates to INT-310: XML Document Builders Not Namespace Aware
This commit is contained in:
@@ -36,6 +36,10 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerParser {
|
||||
|
||||
public static final String DOM_RESULT = "DOMResult";
|
||||
|
||||
public static final String STRING_RESULT = "StringResult";
|
||||
|
||||
@Override
|
||||
protected Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass() {
|
||||
return XmlPayloadMarshallingTransformer.class;
|
||||
@@ -45,16 +49,22 @@ public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerP
|
||||
protected void parsePayloadTransformer(Element element,
|
||||
ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String resultFactory = element.getAttribute("result-factory");
|
||||
String resultType = element.getAttribute("result-type");
|
||||
String marshaller = element.getAttribute("marshaller");
|
||||
String resultTransformer = element.getAttribute("result-transformer");
|
||||
Assert.hasText(marshaller, "the 'marshaller' attribute is required");
|
||||
Assert.hasText(resultFactory,
|
||||
"the 'result-factory' attribute is required");
|
||||
|
||||
assertResultFactoryAndTypeValid(resultFactory, resultType);
|
||||
|
||||
builder.addConstructorArgReference(marshaller);
|
||||
|
||||
if(StringUtils.hasText(resultTransformer)){
|
||||
builder.addConstructorArgReference(resultTransformer);
|
||||
}
|
||||
if (resultFactory.equals("DOMResult")) {
|
||||
|
||||
if(StringUtils.hasText(resultFactory)){
|
||||
builder.addPropertyReference("resultFactory", resultFactory);
|
||||
} else if (resultType.equals(DOM_RESULT) || !StringUtils.hasText(resultType)) {
|
||||
try {
|
||||
builder.addPropertyValue("resultFactory", new DomResultFactory());
|
||||
}
|
||||
@@ -63,9 +73,22 @@ public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerP
|
||||
"Exception creating DomResultFactory");
|
||||
}
|
||||
}
|
||||
else if (resultFactory.equals("StringResult")) {
|
||||
else if (resultType.equals(STRING_RESULT)) {
|
||||
builder.addPropertyValue("resultFactory", new StringResultFactory());
|
||||
}
|
||||
}
|
||||
|
||||
protected void assertResultFactoryAndTypeValid(String resultFactory, String resultType){
|
||||
boolean bothHaveText = StringUtils.hasText(resultFactory) && StringUtils.hasText(resultType);
|
||||
if(bothHaveText){
|
||||
System.out.println("bob");
|
||||
}
|
||||
Assert.state(!bothHaveText , "Exactly one of result-factory or result-type should be specified");
|
||||
if(StringUtils.hasText(resultType)){
|
||||
Assert.state(resultType.equals(DOM_RESULT) || resultType.equals(STRING_RESULT), "Result type must be either DOMResult or StringResult");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="marshaller" type="xsd:string"
|
||||
use="required" />
|
||||
<xsd:attribute name="result-factory" default="DOMResult">
|
||||
<xsd:attribute name="result-type" use="optional">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="DOMResult" />
|
||||
@@ -36,6 +36,7 @@
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="result-factory" use="optional" />
|
||||
<xsd:attribute name="result-transformer" type="xsd:string" use="optional" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
public class StubResultFactory implements ResultFactory {
|
||||
|
||||
public Result createResult(Object payload) {
|
||||
return new StubStringResult();
|
||||
}
|
||||
|
||||
public class StubStringResult extends StringResult {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,11 +10,15 @@
|
||||
|
||||
<si-xml:marshalling-transformer
|
||||
id="marshallingTransformerStringResultFactory" marshaller="marshaller"
|
||||
result-factory="StringResult" />
|
||||
result-type="StringResult" />
|
||||
|
||||
<si-xml:marshalling-transformer
|
||||
id="marshallingTransformerDOMResultFactory" marshaller="marshaller"
|
||||
result-factory="DOMResult" />
|
||||
result-type="DOMResult" />
|
||||
|
||||
<si-xml:marshalling-transformer
|
||||
id="marshallingTransformerCustomResultFactory" marshaller="marshaller"
|
||||
result-factory="stubResultFactory" />
|
||||
|
||||
<si-xml:marshalling-transformer
|
||||
id="marshallingTransformerWithResultTransformer"
|
||||
@@ -26,4 +30,6 @@
|
||||
<constructor-arg value="testReturn" />
|
||||
</bean>
|
||||
|
||||
<bean id="stubResultFactory" class="org.springframework.integration.xml.config.StubResultFactory" />
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -20,18 +20,18 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.transformer.Transformer;
|
||||
import org.springframework.integration.xml.config.StubResultFactory.StubStringResult;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
@@ -84,5 +84,13 @@ public class XmlMarshallingTransformerParserTests {
|
||||
Message<?> result = transformer.transform(message);
|
||||
assertTrue("Wrong payload type", result.getPayload() instanceof StringResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomResultFactory() throws Exception {
|
||||
Transformer transformer = (Transformer) appContext.getBean("marshallingTransformerCustomResultFactory");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>("hello");
|
||||
Message<?> result = transformer.transform(message);
|
||||
assertTrue("Wrong payload type", result.getPayload() instanceof StubStringResult);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user