INT-3384: Rework JMS/XML Module XSD Enumerations

JIRA: https://jira.spring.io/browse/INT-3384

Polishing
This commit is contained in:
David Liu
2014-08-04 11:30:27 +03:00
committed by Artem Bilan
parent 1c84305f8d
commit 9d0089d51d
31 changed files with 629 additions and 383 deletions

View File

@@ -17,10 +17,10 @@
package org.springframework.integration.xml.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractTransformerParser;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -38,8 +38,8 @@ public class MarshallingTransformerParser extends AbstractTransformerParser {
@Override
protected void parseTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String resultTransformer = element.getAttribute("result-transformer");
String resultFactory = element.getAttribute("result-factory");
String resultType = element.getAttribute("result-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "result-type", "resultType");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "result-factory", "resultFactoryName");
String marshaller = element.getAttribute("marshaller");
Assert.hasText(marshaller, "the 'marshaller' attribute is required");
builder.addConstructorArgReference(marshaller);
@@ -50,7 +50,6 @@ public class MarshallingTransformerParser extends AbstractTransformerParser {
if (StringUtils.hasText(extractPayload)) {
builder.addPropertyValue("extractPayload", extractPayload);
}
XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory);
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2002-2013 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 org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.integration.xml.result.StringResultFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Utility methods for the XML namespace.
*
* @author Jonas Partner
* @author Mark Fisher
* @author Artem Bilan
*/
abstract class XmlNamespaceUtils {
private static final String DOM_RESULT = "DOMResult";
private static final String STRING_RESULT = "StringResult";
/**
* Helper method that encapsulates common logic for validating and building
* a bean definition for a {@link ResultFactory} based on either the
* 'result-factory' or 'result-type' attributes.
*/
static void configureResultFactory(BeanDefinitionBuilder builder, String resultType, String resultFactory) {
boolean bothHaveText = StringUtils.hasText(resultFactory) && StringUtils.hasText(resultType);
Assert.state(!bothHaveText, "Only 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'");
}
if (StringUtils.hasText(resultFactory)) {
builder.addPropertyReference("resultFactory", resultFactory);
}
else if (resultType.equals(STRING_RESULT)) {
builder.addPropertyValue("resultFactory", new StringResultFactory());
}
else if (resultType.equals(DOM_RESULT)) {
builder.addPropertyValue("resultFactory", new DomResultFactory());
}
}
}

View File

@@ -34,12 +34,6 @@ import org.springframework.util.StringUtils;
*/
public class XmlPayloadValidatingFilterParser extends AbstractConsumerEndpointParser {
/** Constant that defines a W3C XML Schema. */
public static final String SCHEMA_W3C_XML = "http://www.w3.org/2001/XMLSchema";
/** Constant that defines a RELAX NG Schema. */
public static final String SCHEMA_RELAX_NG = "http://relaxng.org/ns/structure/1.0";
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.genericBeanDefinition(FilterFactoryBean.class);
@@ -58,7 +52,7 @@ public class XmlPayloadValidatingFilterParser extends AbstractConsumerEndpointPa
selectorBuilder.addConstructorArgValue(schemaLocation);
// it is a restriction with the default value of 'xml-schema' which
// corresponds to 'http://www.w3.org/2001/XMLSchema'
String schemaType = "xml-schema".equals(element.getAttribute("schema-type")) ? SCHEMA_W3C_XML : SCHEMA_RELAX_NG;
String schemaType = element.getAttribute("schema-type");
selectorBuilder.addConstructorArgValue(schemaType);
}
else {

View File

@@ -37,6 +37,7 @@ import org.springframework.util.xml.DomUtils;
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Mike Bazos
* @author liujiong
*/
public class XsltPayloadTransformerParser extends AbstractTransformerParser {
@@ -50,8 +51,8 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser {
String xslResource = element.getAttribute("xsl-resource");
String xslTemplates = element.getAttribute("xsl-templates");
String resultTransformer = element.getAttribute("result-transformer");
String resultFactory = element.getAttribute("result-factory");
String resultType = element.getAttribute("result-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "result-type");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "result-factory", "resultFactoryName");
String transformerFactoryClass = element.getAttribute("transformer-factory-class");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "xslt-param-headers");
Assert.isTrue(StringUtils.hasText(xslResource) ^ StringUtils.hasText(xslTemplates),
@@ -62,11 +63,6 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser {
else if (StringUtils.hasText(xslTemplates)) {
builder.addConstructorArgReference(xslTemplates);
}
XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory);
boolean resultFactorySpecified = StringUtils.hasText(resultFactory) || StringUtils.hasText(resultType);
if(resultFactorySpecified){
builder.addPropertyValue("alwaysUseResultFactory", true);
}
if (StringUtils.hasText(resultTransformer)) {
builder.addConstructorArgReference(resultTransformer);
}

View File

@@ -40,10 +40,29 @@ import org.springframework.xml.validation.XmlValidatorFactory;
/**
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Liujiong
* @since 2.0
*/
public class XmlValidatingMessageSelector implements MessageSelector {
public enum SchemaType {
XML_SCHEMA(XmlValidatorFactory.SCHEMA_W3C_XML),
RELAX_NG(XmlValidatorFactory.SCHEMA_RELAX_NG);
private final String url;
private SchemaType(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
}
private final Log logger = LogFactory.getLog(this.getClass());
private final XmlValidator xmlValidator;
@@ -58,6 +77,7 @@ public class XmlValidatingMessageSelector implements MessageSelector {
this.xmlValidator = xmlValidator;
}
/**
* Creates a selector with a default {@link XmlValidator}. The validator will be initialized with
* the provided 'schema' location {@link Resource} and 'schemaType'. The valid options for schema
@@ -69,12 +89,17 @@ public class XmlValidatingMessageSelector implements MessageSelector {
*
* @throws IOException if the XmlValidatorFactory fails to create a validator
*/
public XmlValidatingMessageSelector(Resource schema, String schemaType) throws IOException {
public XmlValidatingMessageSelector(Resource schema, SchemaType schemaType) throws IOException {
Assert.notNull(schema, "You must provide XML schema location to perform validation");
if (!StringUtils.hasText(schemaType)) {
schemaType = XmlValidatorFactory.SCHEMA_W3C_XML;
if (schemaType == null) {
schemaType = SchemaType.XML_SCHEMA;
}
this.xmlValidator = XmlValidatorFactory.createValidator(schema, schemaType);
this.xmlValidator = XmlValidatorFactory.createValidator(schema, schemaType.getUrl());
}
public XmlValidatingMessageSelector(Resource schema, String schemaType) throws IOException {
this(schema, StringUtils.isEmpty(schemaType) ? null :
SchemaType.valueOf(schemaType.toUpperCase().replaceFirst("-", "_")));
}
@@ -106,7 +131,7 @@ public class XmlValidatingMessageSelector implements MessageSelector {
if (this.throwExceptionOnRejection) {
throw new MessageRejectedException(message, "Message was rejected due to XML Validation errors",
new AggregatedXmlMessageValidationException(
Arrays.<Throwable> asList(validationExceptions)));
Arrays.<Throwable>asList(validationExceptions)));
}
if (logger.isDebugEnabled()) {
logger.debug("Message was rejected due to XML Validation errors");

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2002-2013 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 org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.transformer.AbstractTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.integration.xml.result.StringResultFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* super class for XmlTransformer
*
* @author Jonas Partner
* @author Mark Fisher
* @author Artem Bilan
* @author Liujiong
*/
public abstract class AbstractXmlTransformer extends AbstractTransformer {
public static final String DOM_RESULT = "DOMResult";
public static final String STRING_RESULT = "StringResult";
private volatile String resultType;
private volatile String resultFactoryName;
private volatile ResultFactory resultFactory = new DomResultFactory();
public void setResultFactoryName(String resultFactoryName) {
this.resultFactoryName = resultFactoryName;
}
public void setResultType(String resultType) {
this.resultType = resultType;
}
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(resultFactory, "ResultFactory must not be null");
this.resultFactory = resultFactory;
}
public String getResultType() {
return resultType;
}
public String getResultFactoryName() {
return resultFactoryName;
}
public ResultFactory getResultFactory() {
return resultFactory;
}
@Override
protected void onInit() throws Exception {
super.onInit();
ResultFactory generatedResultFactory = configureResultFactory(resultType, resultFactoryName, this.getBeanFactory());
if (generatedResultFactory != null) {
resultFactory = generatedResultFactory;
}
}
/**
* Helper method that encapsulates common logic for validating and building
* a bean definition for a {@link ResultFactory} based on either the
* 'result-factory' or 'result-type' attributes.
*/
private ResultFactory configureResultFactory(String resultType, String resultFactoryName, BeanFactory beanFactory) {
boolean bothHaveText = StringUtils.hasText(resultFactoryName) && StringUtils.hasText(resultType);
ResultFactory resultFactory = null;
Assert.state(!bothHaveText, "Only 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'");
}
if (StringUtils.hasText(resultFactoryName)) {
resultFactory = (ResultFactory) beanFactory.getBean(resultFactoryName);
}
else if (STRING_RESULT.equals(resultType)) {
resultFactory = new StringResultFactory();
}
else if (DOM_RESULT.equals(resultType)) {
resultFactory = new DomResultFactory();
}
return resultFactory;
}
}

View File

@@ -22,8 +22,6 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import org.springframework.integration.transformer.AbstractTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.oxm.Marshaller;
@@ -35,12 +33,10 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Jonas Partner
*/
public class MarshallingTransformer extends AbstractTransformer {
public class MarshallingTransformer extends AbstractXmlTransformer {
private final Marshaller marshaller;
private volatile ResultFactory resultFactory;
private final ResultTransformer resultTransformer;
private volatile boolean extractPayload = true;
@@ -50,7 +46,6 @@ public class MarshallingTransformer extends AbstractTransformer {
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
this.resultTransformer = resultTransformer;
this.resultFactory = new DomResultFactory();
}
public MarshallingTransformer(Marshaller marshaller) throws ParserConfigurationException {
@@ -58,11 +53,6 @@ public class MarshallingTransformer extends AbstractTransformer {
}
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(resultFactory, "ResultFactory must not be null");
this.resultFactory = resultFactory;
}
/**
* Specify whether the source Message's payload should be extracted prior
* to marshalling. This value is set to "true" by default. To send the
@@ -79,11 +69,12 @@ public class MarshallingTransformer extends AbstractTransformer {
return "xml:marshalling-transformer";
}
@Override
public Object doTransform(Message<?> message) {
Object source = (this.extractPayload) ? message.getPayload() : message;
Object transformedPayload = null;
Result result = this.resultFactory.createResult(source);
Result result = this.getResultFactory().createResult(source);
if (result == null) {
throw new MessagingException(
"Unable to marshal payload, ResultFactory returned null.");

View File

@@ -36,7 +36,6 @@ import org.springframework.core.io.Resource;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.transformer.AbstractTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.integration.xml.source.DomSourceFactory;
@@ -47,6 +46,7 @@ import org.springframework.messaging.MessagingException;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
@@ -77,7 +77,7 @@ import org.springframework.xml.transform.StringSource;
* @author Mike Bazos
* @author Gary Russell
*/
public class XsltPayloadTransformer extends AbstractTransformer implements BeanClassLoaderAware {
public class XsltPayloadTransformer extends AbstractXmlTransformer implements BeanClassLoaderAware {
private final ResultTransformer resultTransformer;
@@ -93,8 +93,6 @@ public class XsltPayloadTransformer extends AbstractTransformer implements BeanC
private volatile SourceFactory sourceFactory = new DomSourceFactory();
private volatile ResultFactory resultFactory = new DomResultFactory();
private volatile boolean resultFactoryExplicitlySet;
private volatile boolean alwaysUseSourceFactory = false;
@@ -155,8 +153,7 @@ public class XsltPayloadTransformer extends AbstractTransformer implements BeanC
* @param resultFactory The result factory.
*/
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(sourceFactory, "ResultFactory must not be null");
this.resultFactory = resultFactory;
super.setResultFactory(resultFactory);
this.resultFactoryExplicitlySet = true;
}
@@ -192,6 +189,21 @@ public class XsltPayloadTransformer extends AbstractTransformer implements BeanC
this.classLoader = classLoader;
}
@Override
public void setResultType(String resultType) {
super.setResultType(resultType);
if (StringUtils.hasText(resultType)) {
this.alwaysUseResultFactory = true;
}
}
@Override
public void setResultFactoryName(String resultFactoryName) {
super.setResultFactoryName(resultFactoryName);
if (StringUtils.hasText(resultFactoryName)) {
this.alwaysUseResultFactory = true;
}
}
@Override
public String getComponentType() {
@@ -270,7 +282,7 @@ public class XsltPayloadTransformer extends AbstractTransformer implements BeanC
result = new StringResult();
}
else {
result = this.resultFactory.createResult(payload);
result = this.getResultFactory().createResult(payload);
}
transformer.transform(source, result);
if (this.resultTransformer != null) {
@@ -300,7 +312,7 @@ public class XsltPayloadTransformer extends AbstractTransformer implements BeanC
else {
source = new DOMSource(documentPayload);
}
Result result = this.resultFactory.createResult(documentPayload);
Result result = this.getResultFactory().createResult(documentPayload);
if (!DOMResult.class.isAssignableFrom(result.getClass())) {
throw new MessagingException(
"Document to Document conversion requires a DOMResult-producing ResultFactory implementation.");

View File

@@ -38,10 +38,7 @@
</xsd:attribute>
<xsd:attribute name="result-type" use="optional">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult"/>
<xsd:enumeration value="StringResult"/>
</xsd:restriction>
<xsd:union memberTypes="resultType xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-factory" type="xsd:string" use="optional">
@@ -211,10 +208,7 @@
]]></xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult"/>
<xsd:enumeration value="StringResult"/>
</xsd:restriction>
<xsd:union memberTypes="resultType xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="result-transformer" type="xsd:string" use="optional">
@@ -294,13 +288,7 @@
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="BOOLEAN_RESULT"/>
<xsd:enumeration value="STRING_RESULT"/>
<xsd:enumeration value="NUMBER_RESULT"/>
<xsd:enumeration value="NODE_RESULT"/>
<xsd:enumeration value="NODE_LIST_RESULT"/>
</xsd:restriction>
<xsd:union memberTypes="evaluationType xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="node-mapper">
@@ -421,13 +409,7 @@
</xsd:documentation>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="BOOLEAN_RESULT"/>
<xsd:enumeration value="STRING_RESULT"/>
<xsd:enumeration value="NUMBER_RESULT"/>
<xsd:enumeration value="NODE_RESULT"/>
<xsd:enumeration value="NODE_LIST_RESULT"/>
</xsd:restriction>
<xsd:union memberTypes="evaluationType xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="overwrite">
@@ -796,10 +778,7 @@
<xsd:attribute name="schema-location"/>
<xsd:attribute name="schema-type" default="xml-schema">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="xml-schema"/>
<xsd:enumeration value="relax-ng"/>
</xsd:restriction>
<xsd:union memberTypes="schemaType xsd:string"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
@@ -883,5 +862,29 @@
<xsd:attribute name="expression" type="xsd:string" use="optional"/>
<xsd:attribute name="value" type="xsd:string" use="optional"/>
</xsd:complexType>
<xsd:simpleType name="resultType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult"/>
<xsd:enumeration value="StringResult"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="evaluationType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="BOOLEAN_RESULT"/>
<xsd:enumeration value="STRING_RESULT"/>
<xsd:enumeration value="NUMBER_RESULT"/>
<xsd:enumeration value="NODE_RESULT"/>
<xsd:enumeration value="NODE_LIST_RESULT"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="schemaType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="xml-schema"/>
<xsd:enumeration value="relax-ng"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>