将 FopFactoryBuilder baseURI 设置为 jar 类路径 [英] Set FopFactoryBuilder baseURI to jar classpath

查看:20
本文介绍了将 FopFactoryBuilder baseURI 设置为 jar 类路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Apache FOP 1.0 项目升级到 Apache FOP 2.1.在这个项目中,所有需要的文件都打包在 jar 文件中.

I'm upgrading an Apache FOP 1.0 project to Apache FOP 2.1. In this project, all necessary files are packaged within the jar file.

我添加了新的 FopFactoryBuilder 来生成 FopFactory

I've added the new FopFactoryBuilder to generate a FopFactory

    FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
    builder = builder.setConfiguration(config);
    fopFactory = builder.build();

但是我所有的资源都是从我的文件系统上的相对路径加载的,而不是从 jar 加载的.如何将 baseURI 设置为 jar 的类路径?

but all my resouces are loaded from the relative path on my file system, not from the jar. How can I set the baseURI to the jar's classpath?

谢谢

推荐答案

我们也使用了 FOP 2.1 并且想要实现,jars-classpath 中的图像会被找到.我们经过测试和使用的解决方案如下:

We also used FOP 2.1 and want to achieve, that images inside jars-classpath will be found. Our tested and used solution is the following:

创建自己的ResourceResolver

Create your own ResourceResolver

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;

import org.apache.fop.apps.io.ResourceResolverFactory;
import org.apache.xmlgraphics.io.Resource;
import org.apache.xmlgraphics.io.ResourceResolver;

public class ClasspathResolverURIAdapter implements ResourceResolver {

  private final ResourceResolver wrapped;


  public ClasspathResolverURIAdapter() {
    this.wrapped = ResourceResolverFactory.createDefaultResourceResolver();
  }


  @Override
  public Resource getResource(URI uri) throws IOException {
    if (uri.getScheme().equals("classpath")) {
      URL url = getClass().getClassLoader().getResource(uri.getSchemeSpecificPart());

      return new Resource(url.openStream());
    } else {
      return wrapped.getResource(uri);
    }
  }

  @Override
  public OutputStream getOutputStream(URI uri) throws IOException {
    return wrapped.getOutputStream(uri);
  }

}

  1. 使用解析器创建 FOPBuilderFactory

FopFactoryBuilder fopBuilder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter());

  1. 最后确定您的形象

<fo:external-graphic src="classpath:com/mypackage/image.jpg" />

因为您使用我们自己的解析器,所以可以执行您想要的所有查找.

Because you use our own Resolver it is possible to do every lookup which you want.

这篇关于将 FopFactoryBuilder baseURI 设置为 jar 类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆