Skip to content

Instantly share code, notes, and snippets.

@yamanyar
Created June 12, 2012 08:05
Show Gist options
  • Save yamanyar/2916071 to your computer and use it in GitHub Desktop.
Save yamanyar/2916071 to your computer and use it in GitHub Desktop.
XsltPayloadTransformer with a custom xslt factory.
package com.yamanyar.esb.transformers;
import net.sf.saxon.TransformerFactoryImpl;
import org.springframework.core.io.Resource;
import org.springframework.integration.xml.transformer.ResultTransformer;
import org.springframework.integration.xml.transformer.XsltPayloadTransformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: U068169
* Date: 6/12/12
* Time: 10:41 AM
* To change this template use File | Settings | File Templates.
*/
public class SaxonXsltTransformer extends XsltPayloadTransformer {
public SaxonXsltTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception {
super(TransformerFactoryImpl.newInstance().newTemplates(
createStreamSourceOnResource(xslResource)), resultTransformer);
}
/**
* Compensate for the fact that a Resource <i>may</i> 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 xslResource) throws IOException {
try {
String systemId = xslResource.getURI().toString();
return new StreamSource(xslResource.getInputStream(), systemId);
} catch (IOException e) {
return new StreamSource(xslResource.getInputStream());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment