是否可以使用 Flying Saucer (XHTML-Renderer) 将 css 解析为类路径资源?

发布于 2024-08-12 01:49:50 字数 286 浏览 2 评论 0原文

我正在尝试将资源打包到 jar 中,但我无法让 Flying Saucer 在类路径上找到 css - 我无法轻松构造 URL 来无缝解决此问题

飞碟是否有办法在类路径上指定资源包来解析项目和图像?

注意:我在一个没有文件系统写入权限的 webstart 应用程序中运行它,因此 jar 扩展并不是真正的选择。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-08-19 01:49:50

您应该实现一个 UserAgentCallback 并将其提供给 XHTMLPanel,如下所示:

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());

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());
浮生面具三千个 2024-08-19 01:49:50

我的解决方案是

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;
    }
}

调用:

renderer.getSharedContext()
                .setUserAgentCallback(new UserAgentCallback(renderer.getOutputDevice(), renderer.getSharedContext()));

My solution is

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 invocation:

renderer.getSharedContext()
                .setUserAgentCallback(new UserAgentCallback(renderer.getOutputDevice(), renderer.getSharedContext()));
岁月打碎记忆 2024-08-19 01:49:50

飞碟似乎没有办法在类路径上指定资源,因此我通过制作 classpath: 链接问题的协议 url 处理程序

实施后的发现

这个问题的一些前提似乎是无效的。在编写自己的类路径 URL 加载器后,我发现需要在 jnlp 中请求 才能使用 URL.setURLStreamHandlerFactory() 。事实上,您需要请求所有权限才能执行任何奇特的操作(即使您只是修改自己的沙箱)。请参阅此处的完整列表

简而言之,这意味着我能够将文件提取到操作系统。但现在有一个类路径加载器真是太好了......

It would seem that flying saucer does not have a way of specifying resources on the classpath, so I work around by making a classpath: protocol url handler at the linked question

Post Implementation Findings

It would seem that the some of the premises of this question are invalid. After writing my own classpath URL loader, I found that you need to request <all-permissions/> in the jnlp to be able to use URL.setURLStreamHandlerFactory(). In fact, you need to request all permissions to do just about anything fancy (even though you're only modifying your own sand-box). See the full list here.

In short, this means that I am able to extract files to the operating system. But it's nice having a classpath loader now...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文