From c6571da09f50136618bdfef9fe07db0067e5dfdd Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Thu, 28 Dec 2023 23:32:58 +0200 Subject: [PATCH] #216 implement loading resources from classpath now it's possible to use "classpath:" prefix in URLs of included resources, e.g. ``` ``` --- .../src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java | 4 ++++ .../org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java | 1 + flying-saucer-examples/src/test/resources/sample.html | 4 ++++ flying-saucer-examples/src/test/resources/styles/sample.css | 3 +++ 4 files changed, 12 insertions(+) create mode 100644 flying-saucer-examples/src/test/resources/styles/sample.css diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java index a8b1ea884..71c5573c2 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/swing/NaiveUserAgent.java @@ -389,6 +389,10 @@ public String resolveURI(String uri) { try { URI result = new URI(uri); if (result.isAbsolute()) { + if (result.getScheme().equals("classpath")) { + URL resource = Thread.currentThread().getContextClassLoader().getResource(uri.substring("classpath".length() + 1)); + return resource.toString(); + } return result.toString(); } XRLog.load(uri + " is not a URL; may be relative. Testing using parent URL " + _baseURL); diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java index 0717bb136..55f3919a9 100644 --- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java +++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java @@ -67,6 +67,7 @@ private void verifyPdf(byte[] pdfBytes) { assertThat(pdf).containsText("Bill To:", "John doe", "john.do@mail.com"); assertThat(pdf).containsText("Invoice #:", "INV-666"); assertThat(pdf).containsText("Invoice Date:", "Sep 27, 2023"); + assertThat(pdf).doesNotContainText("Password", "Secret"); } private byte[] generatePdf(String htmlPath) { diff --git a/flying-saucer-examples/src/test/resources/sample.html b/flying-saucer-examples/src/test/resources/sample.html index 032397dd2..094103eb5 100644 --- a/flying-saucer-examples/src/test/resources/sample.html +++ b/flying-saucer-examples/src/test/resources/sample.html @@ -5,6 +5,8 @@ + Sample bill +