Namespace support for XSLT transformer
This commit is contained in:
@@ -28,6 +28,7 @@ public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
|
||||
public void init() {
|
||||
registerBeanDefinitionParser("marshalling-transformer", new XmlMarshallingTransformerParser());
|
||||
registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser());
|
||||
registerBeanDefinitionParser("xslt-transformer", new XsltPayloadTransformerParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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 org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.xml.transformer.XsltPayloadTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XsltPayloadTransformerParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String xslResource = element.getAttribute("xsl-resource");
|
||||
String xslTemplates = element.getAttribute("xsl-templates");
|
||||
|
||||
boolean bothHaveText = StringUtils.hasText(xslResource) && StringUtils.hasText(xslTemplates);
|
||||
boolean oneHasText = StringUtils.hasText(xslResource) || StringUtils.hasText(xslTemplates);
|
||||
|
||||
Assert.state(!bothHaveText && oneHasText,
|
||||
"Exaclty one of xsl-resource or xsl-templates should be specified");
|
||||
|
||||
builder.getBeanDefinition().setBeanClass(XsltPayloadTransformer.class);
|
||||
|
||||
|
||||
if(StringUtils.hasText(xslResource)){
|
||||
builder.getBeanDefinition().getConstructorArgumentValues()
|
||||
.addGenericArgumentValue(new ValueHolder(xslResource));
|
||||
} else if (StringUtils.hasText(xslTemplates)){
|
||||
builder.getBeanDefinition().getConstructorArgumentValues()
|
||||
.addGenericArgumentValue(new RuntimeBeanReference(xslTemplates));
|
||||
}
|
||||
|
||||
String sourceFactory = element.getAttribute("source-factory");
|
||||
if(StringUtils.hasText(sourceFactory)){
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("sourceFactory", new RuntimeBeanReference(sourceFactory));
|
||||
}
|
||||
|
||||
String resultFactory = element.getAttribute("result-factory");
|
||||
if(StringUtils.hasText(resultFactory)){
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resultFactory", new RuntimeBeanReference(resultFactory));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -52,5 +52,21 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
<xsd:element name="xslt-transformer">
|
||||
<xsd:complexType>
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a XML unmarshalling transformer.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="xsl-resource" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="xsl-templates" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="source-factory" type="xsd:string" use="optional" />
|
||||
<xsd:attribute name="result-factory" type="xsd:string" use="optional" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
</xsd:schema>
|
||||
@@ -64,7 +64,7 @@ public class XmlPayloadMarshallingTransformer implements MessageTransformer {
|
||||
transformedPayload = result;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessageHandlingException(message, "failed to marshal payload", e);
|
||||
throw new MessageHandlingException(message, "failed to marshall payload", e);
|
||||
}
|
||||
|
||||
if (transformedPayload == null) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.xml.transformer;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Templates;
|
||||
import javax.xml.transform.TransformerException;
|
||||
@@ -26,8 +27,10 @@ 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.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
import org.springframework.integration.xml.source.DomSourceFactory;
|
||||
import org.springframework.integration.xml.source.SourceFactory;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
@@ -40,6 +43,10 @@ public class XsltPayloadTransformer implements MessageTransformer {
|
||||
|
||||
private final Templates templates;
|
||||
|
||||
private SourceFactory sourceFactory = new DomSourceFactory();
|
||||
|
||||
private ResultFactory resultFactory = new DomResultFactory();
|
||||
|
||||
public XsltPayloadTransformer(Templates templates) {
|
||||
this.templates = templates;
|
||||
}
|
||||
@@ -52,12 +59,11 @@ public class XsltPayloadTransformer implements MessageTransformer {
|
||||
public void transform(Message message) {
|
||||
try {
|
||||
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
this.transformSource(message);
|
||||
transformSource(message, (Source) message.getPayload());
|
||||
}
|
||||
else {
|
||||
throw new MessagingException(message,
|
||||
"Unsupported payload type for transformation expected javax.xml.transform.Source but got : "
|
||||
+ message.getPayload().getClass().getName());
|
||||
Source source = sourceFactory.getSourceForMessage(message);
|
||||
transformSource(message, source);
|
||||
}
|
||||
}
|
||||
catch (TransformerException e) {
|
||||
@@ -66,10 +72,18 @@ public class XsltPayloadTransformer implements MessageTransformer {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void transformSource(Message message) throws TransformerException {
|
||||
StringResult result = new StringResult();
|
||||
this.templates.newTransformer().transform((Source) message.getPayload(), result);
|
||||
message.setPayload(new StringSource(result.toString()));
|
||||
protected void transformSource(Message message, Source source) throws TransformerException {
|
||||
Result result = resultFactory.getNewResult(message);
|
||||
this.templates.newTransformer().transform(source, result);
|
||||
message.setPayload(result);
|
||||
}
|
||||
|
||||
public void setSourceFactory(SourceFactory sourceFactory) {
|
||||
this.sourceFactory = sourceFactory;
|
||||
}
|
||||
|
||||
public void setResultFactory(ResultFactory resultFactory) {
|
||||
this.resultFactory = resultFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user