Apache FOP:从1.1升级到2.1

时间:2016-11-15 12:53:38

标签: upgrade apache-fop

我关注的是migration guide,但我似乎无法做到正确。

在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));
        fopFactory = FopFactory.newInstance();
        fopFactory.setURIResolver(new ServletContextURIResolver(context));
        fopFactory.setUserConfig(cfg);
    }
}

我改编了以上代码以坚持FOP 2.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无法加载,因为它在创建时失败。

编辑:

+ "///"上下文之后添加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

1 个答案:

答案 0 :(得分:0)

经过几天的调查,迁移工作终于成功完成了。问题来自URI解析器,修复此问题会产生新问题,我后来解决了这个问题。

https://xmlgraphics.apache.org/fop/2.1/upgrading.html指南的帮助相对有限。

问题的核心是URI解析器。您现在必须定义自定义解析程序,但不是在以下示例中提供的示例: 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()));
 }
};

正确的做法是:

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(),正确的语法是uri.getPath()

此外,我们必须删除所有&#34; servlet-context:&#34;字体URI中的标记(在fop-config.xml中)和图像URI(在任何XSL转换文件或模板中)。

最后,我遇到连字符问题:FOP再也找不到.hyp个文件了,因为出于某种原因,baseUri被用来代替自定义上下文解析器(我不得不深入研究) FOP的源文件找出来)。所以,我不得不修改自定义解析器的getResource方法。我知道这是一个黑客,但是它起作用,对我来说已经足够了,因为我已经花了三天时间解决这个问题):

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

请注意,我还必须手动创建none.hyp文件,因为OFFO提供的.hyp文件中不存在该文件。我刚刚复制了en.hyp并将其重命名为none.hyp。这解决了我的最后一个问题。

我希望这能节省一些人的工作;)