2010-11-23 107 views
0

我有一個xslt文件在classpath裏面的jar文件。我曾嘗試使用InputStream加載xslt文件。一些如何在調試後,InputStream代替xslt文件包含jar文件。從jar加載xslt文件加載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中。任何想法的人?

回答

0

InputStream中包含的jar文件,而不是 XSLT文件

是什麼讓你這麼說?您是否嘗試將文本內容InputStream打印出來?在創建InputStream in並使用它之間,你是否正在做其他事情(......部分)?

如果提供給getResourceAsStream點到XSLT,如果is路徑是呼叫後不nullis應該包含代表XSLT資源InputStream。如何粘貼整個堆棧跟蹤?

+1

我改變了實現這個的方式。現在在WebTier中使用ServletContext加載xslt文件。當Xslt文件不在jar中時,它可以很好地管理。在webtier中它沒有問題。 – 2010-11-24 16:27:58

1

試試這個

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

那你能給我們IOUtils(阿帕奇)或建議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(); 
    } 
4

創建自定義解析從類路徑來解決 組到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 

`