从JAR文件加载XSLT文件会加载JAR文件本身而不是XSLT

时间:2010-11-23 16:53:04

标签: classloader transform inputstream xslt

我在 JAR 文件中的类路径上有一个 XSLT 文件。我试图使用InputStream加载XSLT文件。调试后,InputStream包含JAR文件而不是XSLT文件。

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}

我已经仔细检查过XSLT文件的路径是否正确,并且JAR文件中包含了一个物理文件。有什么想法吗?

3 个答案:

答案 0 :(得分:4)

创建自定义解析程序以从类路径解析 设置为Transfromer 测试我在eclipse项目的classpath中设置了一个jar文件

所有代码如下 -----运行示例------------- `

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();
             }

    }

}

`


-----------自定义解析器代码--------------- `

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

`

答案 1 :(得分:1)

试试这个

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

然后您可以使用IOUtils(apache)或其中一个建议here将InputStream转换为String或只使用接受输入流的javax.xml ... StreamSource构造函数。

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();
  }

答案 2 :(得分:0)

  

InputStream包含jar文件   xslt文件

是什么让你这么说的?您是否尝试将InputStream的内容打印为文本?在创建InputStream in和使用它之间,您是否正在使用它(...部分)?

如果提供给getResourceAsStream的路径指向XSLT,并且在通话后is不是null,则is应包含代表的InputStream XSLT资源。如何粘贴整个堆栈跟踪?