Announcement Announcement Module
Collapse
No announcement yet.
Example How to load xslt files from classpath Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Example How to load xslt files from classpath

    Loading of xslt files is done by URI using xslt-transformer. The URI resolution is used by xslt file itself and by xsl:import, xsl:include. Loading the xslt file and xsl:import/include have been changed to use ClassLoader.

    Using ClassLoader to load xslt file and define the Resolver for xsl:import/include:

    <bean id="myResolver" class="MyResolver"/>
    <bean id="transformerFactory" class="javax.xml.transform.TransformerFactory" factory-method="newInstance">
    <property name="URIResolver" ref="myResolver"/>
    </bean>
    <bean id="classLoader" class="java.lang.ClassLoader" factory-method="getSystemClassLoader"/>
    <bean id="fpmlStream" class="java.io.InputStream" factory-bean="classLoader" factory-method="getResourceAsStream">
    <constructor-arg type="java.lang.String" value="fpml-2-target-v2-0.xslt"/>
    </bean>
    <bean id="fpmlSource" class="javax.xml.transform.stream.StreamSource">
    <constructor-arg type="java.io.InputStream" ref="fpmlStream"/>
    </bean>
    <bean id="fpmlTemplates" factory-bean="transformerFactory" factory-method="newTemplates">
    <constructor-arg ref="fpmlSource"/>
    </bean>
    <spi-xml:xslt-transformer input-channel="fpmlChannel" output-channel="outputChannel" xsl-templates="fpmlTemplates"/>

    The Resolver for xsl:import/include tries to find the xslt file inside classpath:

    import java.io.InputStream;

    import javax.xml.transform.Source;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.URIResolver;
    import javax.xml.transform.stream.StreamSource;

    import org.apache.log4j.Logger;


    public class MyResolver implements URIResolver
    {
    private static Logger log = Logger.getLogger(MyResolver.class);


    @Override
    public Source resolve(String href, String base) throws TransformerException
    {
    log.debug("href=" + href + " : base=" + base);

    String fileName = null;

    try {
    int k = href.lastIndexOf('/') + 1;
    if (k > 0) {
    fileName = href.substring(k);
    } else {
    fileName = href;
    }

    InputStream input = ClassLoader.getSystemResourceAsStream(fileName);
    if (input != null) {
    return (new StreamSource(input));
    }
    } catch (Exception ie) {
    log.error("File " + fileName + " not found!", ie);
    }

    // instruct the caller to use the default lookup
    return (null);
    }
    }

  • #2
    I've seen a bug that seems related to this (https://jira.springsource.org/browse/INT-812), but I'm not getting your point. Are you just stating what happens, or did you expect something else, or did you patch this bug?

    Comment


    • #3
      I was just stating what happens.

      Apps like Xerces, Xalan, Saxon, etc. do not support classpath as an URI scheme. Therefore I filed an example how to do it.

      Having a second thought:
      Should a classpath scheme be added to Entity- and URIResolver?
      If yes, should this support be added to Spring or Xerces, Xalan and Saxon?

      Comment

      Working...
      X