Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2014 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 java.util.Map;
 
 
 
Thread safe XSLT transformer implementation which returns a transformed javax.xml.transform.Source, org.w3c.dom.Document, or java.lang.String. If alwaysUseSourceResultFactories is false (default) the following logic occurs

java.lang.String payload in results in java.lang.String payload out

org.w3c.dom.Document payload in results in org.w3c.dom.Document payload out

javax.xml.transform.Source payload in results in javax.xml.transform.Result payload out, type will be determined by the org.springframework.integration.xml.result.ResultFactory, org.springframework.integration.xml.result.DomResultFactory by default. If an instance of ResultTransformer is registered this will be used to convert the result.

If alwaysUseSourceResultFactories is true then the ResultFactory and org.springframework.integration.xml.source.SourceFactory will be used to create the javax.xml.transform.Source from the payload and the javax.xml.transform.Result to pass into the transformer. An instance of ResultTransformer can also be provided to convert the Result prior to returning.

Author(s):
Jonas Partner
Mark Fisher
Oleg Zhurakousky
Artem Bilan
Mike Bazos
Gary Russell
 
 public class XsltPayloadTransformer extends AbstractTransformer implements BeanClassLoaderAware {
 
 
 	private volatile Resource xslResource;
 
 	private volatile Templates templates;
 
 
 
 
 	private volatile SourceFactory sourceFactory = new DomSourceFactory();
 
 	private volatile ResultFactory resultFactory = new DomResultFactory();
 
 	private volatile boolean resultFactoryExplicitlySet;
 
	private volatile boolean alwaysUseSourceFactory = false;
	private volatile boolean alwaysUseResultFactory = false;
	private volatile String[] xsltParamHeaders;
	public XsltPayloadTransformer(Templates templates) {
		this(templatesnull);
	}
	public XsltPayloadTransformer(Resource xslResource) {
		this(xslResourcenullnull);
	}
	public XsltPayloadTransformer(Resource xslResourceResultTransformer resultTransformer) {
		this(xslResourceresultTransformernull);
	}
	public XsltPayloadTransformer(Resource xslResourceString transformerFactoryClassName) {
		Assert.notNull(xslResource"'xslResource' must not be null.");
		Assert.hasText(transformerFactoryClassName"'transformerFactoryClassName' must not be empty String.");
		this. = xslResource;
		this. = transformerFactoryClassName;
		this. = null;
	}
	public XsltPayloadTransformer(Resource xslResourceResultTransformer resultTransformerString transformerFactoryClassName) {
		Assert.notNull(xslResource"'xslResource' must not be null.");
		this. = xslResource;
		this. = resultTransformer;
		this. = transformerFactoryClassName;
	}
	public XsltPayloadTransformer(Templates templatesResultTransformer resultTransformer) {
		Assert.notNull(templates"'templates' must not be null.");
		this. = templates;
		this. = resultTransformer;
	}

Sets the SourceFactory.

Parameters:
sourceFactory The source factory.
	public void setSourceFactory(SourceFactory sourceFactory) {
		Assert.notNull(sourceFactory"SourceFactory must not be null");
		this. = sourceFactory;
	}

Sets the ResultFactory.

Parameters:
resultFactory The result factory.
	public void setResultFactory(ResultFactory resultFactory) {
		Assert.notNull("ResultFactory must not be null");
		this. = resultFactory;
	}

Specify whether to always use source factory even for directly supported payload types.

Parameters:
alwaysUseSourceFactory true to always use the source factory.
	public void setAlwaysUseSourceFactory(boolean alwaysUseSourceFactory) {
		this. = alwaysUseSourceFactory;
	}

Specify whether to always use result factory even for directly supported payload types

Parameters:
alwaysUseResultFactory true to always use the result factory.
	public void setAlwaysUseResultFactory(boolean alwaysUseResultFactory) {
		this. = alwaysUseResultFactory;
	}
	public void setXslParameterMappings(Map<StringExpressionxslParameterMappings) {
		this. = xslParameterMappings;
	}
	public void setXsltParamHeaders(String[] xsltParamHeaders) {
		this. = xsltParamHeaders;
	}
	public void setBeanClassLoader(ClassLoader classLoader) {
		Assert.notNull(classLoader"'beanClassLoader' must not be null.");
		this. = classLoader;
	}
		return "xml:xslt-transformer";
	}
	protected void onInit() throws Exception {
		super.onInit();
		if (this. == null) {
			TransformerFactory transformerFactory;
			if (this. != null) {
				transformerFactory = TransformerFactory.newInstance(this.this.);
			}
			else {
				transformerFactory = TransformerFactory.newInstance();
			}
		}
	}
	protected Object doTransform(Message<?> messagethrows Exception {
		Transformer transformer = buildTransformer(message);
		Object payload;
			payload = .createSource(message.getPayload());
		}
		else {
			payload = message.getPayload();
		}
		Object transformedPayload = null;
			transformedPayload = transformUsingResultFactory(payloadtransformer);
		}
		else if (payload instanceof String) {
			transformedPayload = transformString((Stringpayloadtransformer);
		}
		else if (payload instanceof Document) {
			transformedPayload = transformDocument((Documentpayloadtransformer);
		}
		else if (payload instanceof Source) {
			transformedPayload = transformSource((Sourcepayloadpayloadtransformer);
		}
		else {
			// fall back to trying factories
			transformedPayload = transformUsingResultFactory(payloadtransformer);
		}
		return transformedPayload;
	}
	private Object transformUsingResultFactory(Object payloadTransformer transformerthrows TransformerException {
		Source source;
			source = this..createSource(payload);
		}
		else if (payload instanceof String) {
			source = new StringSource((Stringpayload);
		}
		else if (payload instanceof Document) {
			source = new DOMSource((Documentpayload);
		}
		else if (payload instanceof Source) {
			source = (Sourcepayload;
		}
		else {
			source = this..createSource(payload);
		}
		return transformSource(sourcepayloadtransformer);
	}
	private Object transformSource(Source sourceObject payloadTransformer transformerthrows TransformerException {
		Result result;
		if (!this. && "text".equals(transformer.getOutputProperties().getProperty("method"))) {
			result = new StringResult();
		}
		else {
			result = this..createResult(payload);
		}
		transformer.transform(sourceresult);
		if (this. != null) {
			return this..transformResult(result);
		}
		return result;
	}
	private String transformString(String stringPayloadTransformer transformerthrows TransformerException {
		StringResult result = new StringResult();
		Source source;
			source = this..createSource(stringPayload);
		}
		else {
			source = new StringSource(stringPayload);
		}
		transformer.transform(sourceresult);
		return result.toString();
	}
	private Document transformDocument(Document documentPayloadTransformer transformerthrows TransformerException {
		Source source;
			source = this..createSource(documentPayload);
		}
		else {
			source = new DOMSource(documentPayload);
		}
		Result result = this..createResult(documentPayload);
		if (!DOMResult.class.isAssignableFrom(result.getClass())) {
			throw new MessagingException(
					"Document to Document conversion requires a DOMResult-producing ResultFactory implementation.");
		}
		DOMResult domResult = (DOMResultresult;
		transformer.transform(sourcedomResult);
		return (DocumentdomResult.getNode();
	}
	private Transformer buildTransformer(Message<?> messagethrows TransformerException {
		// process individual mappings
		Transformer transformer = this..newTransformer();
		if (this. != null) {
			for (String parameterName : this..keySet()) {
				Expression expression = this..get(parameterName);
				try {
					Object value = expression.getValue(this.message);
					transformer.setParameter(parameterNamevalue);
				}
				catch (Exception e) {
						.warn("Evaluation of header expression '"
expression.getExpressionString()
"' failed. The XSLT parameter '"
parameterName + "' will be skipped.");
					}
				}
			}
		}
		// process xslt-parameter-headers
		MessageHeaders headers = message.getHeaders();
		if (!ObjectUtils.isEmpty(this.)) {
			for (String headerName : headers.keySet()) {
				if (PatternMatchUtils.simpleMatch(this.headerName)) {
					transformer.setParameter(headerNameheaders.get(headerName));
				}
			}
		}
		return transformer;
	}

Compensate for the fact that a Resource may not be a File or even addressable through a URI. If it is, we want the created StreamSource to read other resources relative to the provided one. If it isn't, it loads from the default path.
	private static StreamSource createStreamSourceOnResource(Resource xslResourcethrows IOException {
		try {
			String systemId = xslResource.getURI().toString();
			return new StreamSource(xslResource.getInputStream(), systemId);
		}
		catch (IOException e) {
			return new StreamSource(xslResource.getInputStream());
		}
	}
New to GrepCode? Check out our FAQ X