2016-11-15 9 views
0

私はmigration guideに従っていますが、私はそれを正しく理解していないようです。 ​​ Apache FOP:1.1から2.1へのアップグレード

は私がFOP 2.1に固執する上記のコードを適応:

はFOP 1.1で、私はこの作業コードを持って

public class XsltFactory { 
    private static final String FO_CONFIG_FILE = "/path/to/fop-config.xml"; 

    private static FopFactory fopFactory; 

    private static synchronized void initFopFactory(final ServletContext context) throws Exception { 
     Configuration cfg = new DefaultConfigurationBuilder().build(XsltFactory.class.getResourceAsStream(FO_CONFIG_FILE)); 

     FopFactoryBuilder fopFactoryBuilder = new FopFactoryBuilder(
      new URI(ServletContextURIResolver.SERVLET_CONTEXT_PROTOCOL), 
      new URIResolverAdapter(new ServletContextURIResolver(context)) 
     ); 

     fopFactoryBuilder.setConfiguration(cfg); 
     fopFactory = fopFactoryBuilder.build(); 
    } 
} 

しかし、私は次のエラーを取得:

java.lang.Exception: Fail to create PDF 
    at ....web.controller.PrintPdfController.renderPdf(PrintPdfController.java:181) 
    [...] 
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:263) 
Caused by: java.net.URISyntaxException: Expected scheme-specific part at index 16: servlet-context: 
    at java.net.URI$Parser.fail(URI.java:2829) 
    at java.net.URI$Parser.failExpecting(URI.java:2835) 
    at java.net.URI$Parser.parse(URI.java:3038) 
    at java.net.URI.<init>(URI.java:595) 
    [...] 
    ... 42 common frames omitted 

PDFの作成に失敗したため、PDFの読み込みに失敗しました。

EDIT:

SERVLET_CONTEXT_PROTOCOLコンテキスト後+ "///"を追加した後、私は今取得:調査の数日後

Caused by: java.net.MalformedURLException: unknown protocol: servlet-context 
    at java.net.URL.<init>(URL.java:592) 
    at java.net.URL.<init>(URL.java:482) 
    at java.net.URL.<init>(URL.java:431) 
    at java.net.URI.toURL(URI.java:1096) 
    at org.apache.fop.fonts.FontDetectorFactory$DefaultFontDetector.detect(FontDetectorFactory.java:94) 
    ... 59 common frames omitted 
+0

これはドキュメントから正しく見えます。 1.0から2.1になったときに設定が変更されたようです。それはあなたのURIか解決者かもしれません。あなたが生成しているURIの具体例は何ですか? –

+0

生成されたbaseUriは "servlet-context:///"です。私は春のアプリケーションで作業しており、絶対的なファイルシステムのパスをbaseUriとして提供することはできません。 –

答えて

0

、移行が最終的に成功しました。問題はURIリゾルバから来ていましたが、この問題を修正することで新たな問題が発生しました。

https://xmlgraphics.apache.org/fop/2.1/upgrading.htmlのガイドは、比較的限定されたヘルプです。

問題の核心はURIリゾルバです。正しい構文は、

ResourceResolver resolver = new ResourceResolver() { 
public OutputStream getOutputStream(URI uri) throws IOException { 
    URL url = context.getResource(uri.getPath()); 
    return url.openConnection().getOutputStream(); 
} 
public Resource getResource(URI uri) throws IOException { 
    return new Resource(context.getResourceAsStream(uri.getPath())); 
} 
}; 

代わりのuri.toASCIIString()https://xmlgraphics.apache.org/fop/2.0/servlets.html

ResourceResolver resolver = new ResourceResolver() { 
public OutputStream getOutputStream(URI uri) throws IOException { 
    URL url = getServletContext().getResource(uri.toASCIIString()); 
    return url.openConnection().getOutputStream(); 
} 

public Resource getResource(URI uri) throws IOException { 
    return new Resource(getServletContext().getResourceAsStream(uri.toASCIIString())); 
} 
}; 

それを行うための正しい方法は次のとおりです。あなたは今で提供されるカスタムリゾルバを定義する必要はなく、例のようにuri.getPath()

さらに、fop-config.xmlのフォントURIですべての "servlet-context:"マークアップを削除し、(XSL変換ファイルまたはテンプレートの)URIをイメージングする必要がありました。

最後に、何らかの理由でbaseUriがカスタムコンテキストリゾルバの代わりに使用されていたため、FOPは.hypファイルをもう見つけられませんでした(私はFOPのソースファイルを調べて見つけなければなりませんでした) 。だから、自分のカスタムリゾルバのgetResourceメソッドを修正しなければならなかった。私は、これはハックですけど、それが動作し、私はすでにこの問題に3日間)を過ごしたとして、それは私のために十分である:私もそれをしないので、手動でnone.hypファイルを作成する必要がありました

public OutputStream getOutputStream(URI uri) throws IOException { 
    URL url = context.getResource(uri.getPath()); 
    return url.openConnection().getOutputStream(); 
} 
public Resource getResource(URI uri) throws IOException { 
    InputStream stream = null; 
    /* 
    * For some reason, in FOP 2.x, the hyphenator does not use the 
    * classpath fop-hyph.jar. 
    * 
    * This causes trouble as FOP tries to find "none.hyp" in the 
    * war directory. Setting 
    * <hyphenation-base>/WEB-INF/hyph</hyphenation-base> in the 
    * fop-config.xml file does not solve the issue. The only 
    * solution I could find is to programmatically detect when a 
    * .hyp file is trying to be loaded. When this occurs, I modify 
    * the path so that the resolver gets the right resource. 
    * 
    * This is a hack, but after spending three days on it, I just 
    * went straight to the point and got a workaround. 
    */ 
    if (uri.getPath().endsWith('.hyp')) { 
    String relUri = uri.getPath().substring(uri.getPath().indexOf(baseUri.getPath()) + baseUri.getPath().length()); 
    stream = context.getResourceAsStream(FopManager.HYPH_DIR + relUri); 
    } else { 
    stream = context.getResourceAsStream(uri.getPath()); 
    } 
    Resource res = new Resource(stream); 
    return res; 
} 
}; 

注意をOFFOによって提供されるファイル.hypに存在します。私はちょうどen.hypをコピーし、none.hypと名前を変更しました。これは私の最後の問題を解決しました。

私はこれで数日の作業を節約したいと思っています;)

関連する問題