【问题标题】:Loading an XSLT file from a JAR file loads the JAR file itself instead of the XSLT从 JAR 文件加载 XSLT 文件会加载 JAR 文件本身而不是 XSLT
【发布时间】:2010-11-23 16:53:04
【问题描述】:

我在 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 文件中包含一个物理文件。有什么想法吗?

【问题讨论】:

    标签: classloader transform inputstream xslt


    【解决方案1】:

    创建自定义解析器以从类路径解析 将其设置为Transfromer 为了测试我在 Eclipse 项目的类路径中设置了一个 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
    

    `

    【讨论】:

      【解决方案2】:

      试试这个

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

      然后您可以使用 IOUtils (apache) 或 here 的建议之一将 InputStream 转换为字符串,或者只使用接受输入流的 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();
        }
      

      【讨论】:

        【解决方案3】:

        InputStream 包含 jar 文件 xslt 文件

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

        如果提供给getResourceAsStream 的路径指向一个XSLT,并且如果is 在调用后不是null,则is 应该包含代表XSLT 资源的InputStream。在这里粘贴整个堆栈跟踪怎么样?

        【讨论】:

        • 我改变了实现这个的方式。现在使用 WebTier 中的 ServletContext 来加载 xslt 文件。当 Xslt 文件不在 jar 中时,可以很好地管理它。在 webtier 中它可以正常工作。
        猜你喜欢
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 2019-01-02
        • 2014-05-07
        • 2011-07-18
        • 2013-08-26
        相关资源
        最近更新 更多