Set basePIFopFactoryBuilder in jar classpath

I am updating the Apache FOP 1.0 project to Apache FOP 2.1. In this project, all the necessary files are packed into a jar file.

I added a new FopFactoryBuilder to create a FopFactory

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

but all my resources are loaded from a relative path in my file system, not from a jar. How to set baseURI in jar class path?

thanks

+3
source share
2 answers

We also used FOP 2.1 and want to ensure that images inside the jars-classpath are found. Our tested and used solution:

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. Create a FOPBuilderFactory with your Resolver
 FopFactoryBuilder fopBuilder = new FopFactoryBuilder(new File(".").toURI(), new ClasspathResolverURIAdapter()); 
  1. Finally, turn to your image.
 <fo:external-graphic src="classpath:com/mypackage/image.jpg" /> 

Since you use our own Resolver, you can perform any search you want.

+2
source

By specifying the URL as the class URL, for example:

 <fo:external-graphic src="classpath:fop/images/myimage.jpg"/> 

In this example, the file is a resource in the fop.images resource fop.images , but the actual file is later packaged in a completely different place inside the JAR, which is, however, part of the class path, so searching as described above works.

0
source

Source: https://habr.com/ru/post/1259213/


All Articles