Is it possible to enable css as a classpath resource using Flying Saucer (XHTML-Renderer)?

I'm trying to pack resources in a jar, but I had problems getting Flying Saucer to find css in the classpath - I cannot easily create URLs so that this problem can be easily solved .

Does Flying saucer have a way of specifying resource bundles in the classpath for resolving elements and images?

Note. I run this in a webstart application that does not have write permissions to the file system, so the jar extension is not really an option.

+3
java css flying-saucer xhtmlrenderer
source share
3 answers

You should implement the UserAgentCallback that you pass to the XHTMLPanel, something like this:

private static class UAC extends NaiveUserAgent { @Override public String resolveURI(String uri) { return uri; } @Override protected InputStream resolveAndOpenStream(String uri) { java.io.InputStream is = null; URL url = UAC.class.getResource(uri); if (url == null) { XRLog.load("Didn't find resource [" + uri + "]."); return null; } try { is = url.openStream(); } catch (java.net.MalformedURLException e) { XRLog.exception("bad URL given: " + uri, e); } catch (java.io.FileNotFoundException e) { XRLog.exception("item at URI " + uri + " not found"); } catch (java.io.IOException e) { XRLog.exception("IO problem for " + uri, e); } return is; } } XHTMLPanel panel = new XHTMLPanel(new UAC()); 
+3
source share

My decision

 private static class UserAgentCallback extends ITextUserAgent { public UserAgentCallback(ITextOutputDevice outputDevice, SharedContext sharedContext) { super(outputDevice); setSharedContext(sharedContext); } @Override public String resolveURI(String uri) { return uri; } @Override protected InputStream resolveAndOpenStream(String uri) { java.io.InputStream is = null; URL url = null; try { url = new ClassPathResource("/META-INF/pdfTemplates/" + uri).getURL(); } catch (IOException e) { XRLog.exception("bad URL given: " + uri, e); } if (url == null) { XRLog.load("Didn't find resource [" + uri + "]."); return null; } try { is = url.openStream(); } catch (java.net.MalformedURLException e) { XRLog.exception("bad URL given: " + uri, e); } catch (java.io.FileNotFoundException e) { XRLog.exception("item at URI " + uri + " not found"); } catch (java.io.IOException e) { XRLog.exception("IO problem for " + uri, e); } return is; } } 

and call:

 renderer.getSharedContext() .setUserAgentCallback(new UserAgentCallback(renderer.getOutputDevice(), renderer.getSharedContext())); 
+2
source share

It would seem that a flying saucer has no way of specifying resources on the path to the classes, so I work by creating a classpath: a URL handler on a related issue

Results after implementation

It would seem that some of the premises of this question are not valid. After writing my own class URL loader, I found that you need to request <all-permissions/> in jnlp in order to be able to use URL.setURLStreamHandlerFactory() . In fact, you need to request all permissions to do something interesting (although you are only modifying your own sandbox). See the full list here .

In short, this means that I can extract files to the operating system. But now it’s nice to have a classpath loader ...

0
source share

All Articles