0

I've an XSLT file on the classpath inside a JAR file. I've tried to load the XSLT file using an InputStream. After debugging, the InputStream contains the JAR file instead of the XSLT file.

String xslPath = "/com/japi/application/templates/foo.xslt";
InputStream is = getClass().getResourceAsStream(xslPath);
...
Source xslt = new StreamSource(is);
trans = factory.newTransformer(xsltSource); //Fatal error. Error parsing XSLT {0}

I have double checked that the path to the XSLT file is correct and a physical file is included in the JAR file. Any ideas?

Rene Knop
  • 1,563
  • 3
  • 12
  • 24
Jarno Lahtinen
  • 1,529
  • 14
  • 25

3 Answers3

4

Create a Custom Resolver to resolve from class path set that to Transfromer to test i had a jar file set in classpath in eclipse project

all code is here below ----- run example ------------- `

public class RunTransform {

    public static void main(String[] args) {

        //  SimpleTransform.transform("xsl/SentAdapter.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");

        SimpleTransform.transform("xslt/ibanvalidation/accuity-ibanvalidationresponse.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");
    }

}

-----------Sample transfomring example ----------------

package com;

import java.io.File;
import java.io.FileOutputStream;


import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


public class SimpleTransform {

    public static void transform(String xslName,String xmlName) {
        try {  
            ResourceResolver resloader = new ResourceResolver();
              TransformerFactory tFactory = TransformerFactory.newInstance();
              tFactory.setURIResolver(resloader);

              StreamSource xsltSRC = new StreamSource(resloader.resolve(xslName));

              Transformer transformer = tFactory.newTransformer(xsltSRC);

              StreamSource xmlSSRC = new StreamSource(xmlName);
              System.out.println("Streamm sources created .....");

              System.out.println("XSLT SET ....");
              transformer.transform(xmlSSRC, new StreamResult(new FileOutputStream(new File("C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/result.xml"))));
              System.out.println("Finished transofrmation ..........");
              System.out.println("************* The result is out in respoinse *************");


             } catch (Throwable t) {
                      t.printStackTrace();
             }

    }

}

`


-----------Code for Custom resolver --------------- `

package com;

import javax.xml.transform.URIResolver;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;

public class ResourceResolver implements URIResolver {

    /* (non-Javadoc)
     * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
     */
    public Source resolve(String href, String base) throws TransformerException {

        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return new StreamSource(is, href);
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    } // resolve

    /**
     * @param href
     * @return
     * @throws TransformerException
     */
    public InputStream resolve(String href) throws TransformerException {
        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return is;
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    }
} // ResourceResolver

`

Amin
  • 41
  • 2
1

Try this

String pathWithinJar = "com/example/xslt/dummy.xslt";
InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(pathWithinJar);

Then you can us IOUtils (apache) or one of the suggestions here to convert the InputStream into a String or just use the javax.xml...StreamSource constructor that accepts an input stream.

public static void transform(InputStream xslFileStream, File xmlSource, File xmlResult)
      throws TransformerException, IOException {

    // unknown if the factory is thread safe, always create new instance
    TransformerFactory factory = TransformerFactory.newInstance();
    StreamSource xslStreamSource = new StreamSource(xslFileStream);
    Transformer transformer = factory.newTransformer(xslStreamSource);

    StreamSource sourceDocument = new StreamSource(xmlSource);
    StreamResult resultDocument = new StreamResult(xmlResult);
    transformer.transform(sourceDocument, resultDocument);

    resultDocument.getOutputStream().flush();
    resultDocument.getOutputStream().close();
  }
Community
  • 1
  • 1
cyber-monk
  • 5,260
  • 6
  • 29
  • 41
0

InputStream contains jar file instead xslt file

What makes you say that? Have you tried printing out the contents of the InputStream as text? In between creating the InputStream in and using it, are you doing something else with it(the ... part)?

If the path provided to the getResourceAsStream points to an XSLT and if is is not null after the call, is should contain the InputStream representing the XSLT resource. How about pasting the entire stack trace here?

Sanjay T. Sharma
  • 21,620
  • 4
  • 54
  • 70
  • 1
    I changed the way implementing this. Now using ServletContext in WebTier to load xslt file. Xslt file can be managed nicely when it's not inside jar. In webtier it worked without problems. – Jarno Lahtinen Nov 24 '10 at 16:27