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

查看:117
本文介绍了将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天全站免登陆