Stephen Stephen - 5 months ago 31
Java Question

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

I'm trying to package up resources into a jar, but I'm having trouble getting Flying Saucer to find the css on the classpath - I can't construct a URL easily to be able to resolve this seamlessly.

Does Flying saucer have a way of specifying resource packages on the classpath to resolve items and images?

Note: I'm running this in a webstart application that does not have file system writing permissions, so jar expansion is not really an option.

Answer

You should implement a UserAgentCallback that you feed 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());
Comments