From ec6eeb7eb05deb5c6d26a69d37005ddca9da7c0d Mon Sep 17 00:00:00 2001 From: Jukka Matilainen Date: Tue, 8 Aug 2023 20:04:34 +0300 Subject: [PATCH 1/4] Compile to Java 9 instead of Java 8 The method of registering URLStreamHandlers is a bit different in Java 9+ as compared to Java 8. Here we are going to demonstrate the newer method, so we'll change the compilation target to Java 9. --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 6c3304e..7eb3bcb 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,8 @@ UTF-8 UTF-8 - 1.8 - 1.8 + 9 + 9 From 362ffe64d310961067ca7f381c8a17d11280f012 Mon Sep 17 00:00:00 2001 From: Jukka Matilainen Date: Tue, 8 Aug 2023 20:12:28 +0300 Subject: [PATCH 2/4] Register a URLStreamHandler for 'cp:' URLs Introduce a URLStreamHandler for dereferencing 'cp:/' URLs, allowing to retrieve the contents from classpath resources. This implementation supports an optional leading slash in the URL paths, which allows the URLs to be treated as hiererchical, supporting the use of relative URL references. --- .../ClasspathURLStreamHandlerProvider.java | 38 +++++++++++++++++++ .../java.net.spi.URLStreamHandlerProvider | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/main/java/com/stackoverflow/q76848364/ClasspathURLStreamHandlerProvider.java create mode 100644 src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider diff --git a/src/main/java/com/stackoverflow/q76848364/ClasspathURLStreamHandlerProvider.java b/src/main/java/com/stackoverflow/q76848364/ClasspathURLStreamHandlerProvider.java new file mode 100644 index 0000000..6fdaca4 --- /dev/null +++ b/src/main/java/com/stackoverflow/q76848364/ClasspathURLStreamHandlerProvider.java @@ -0,0 +1,38 @@ +package com.stackoverflow.q76848364; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.spi.URLStreamHandlerProvider; + +/** + * URL stream handler for "cp:/" URLs for accessing resources in the classpath. + * Supports a leading slash in the the path so that the scheme is treated as a + * hierarchical scheme for resolving relative URL references. + * + *

+ * Register this provider by putting the fully qualified name of this class in + * the configuration file + * META-INF/services/java.net.spi.URLStreamHandlerProvider. + */ +public class ClasspathURLStreamHandlerProvider extends URLStreamHandlerProvider { + + private static final String PROTOCOL = "cp"; + + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + if (PROTOCOL.equals(protocol)) { + return new URLStreamHandler() { + @Override + protected URLConnection openConnection(URL url) throws IOException { + String urlPath = url.getPath(); + String resourcePath = urlPath.startsWith("/") ? urlPath.substring(1) : urlPath; + return ClassLoader.getSystemClassLoader().getResource(resourcePath).openConnection(); + } + }; + } + return null; + } + +} \ No newline at end of file diff --git a/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider b/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider new file mode 100644 index 0000000..0033638 --- /dev/null +++ b/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider @@ -0,0 +1 @@ +com.stackoverflow.q76848364.ClasspathURLStreamHandlerProvider \ No newline at end of file From 0c394e3e3b47934e11a6329758e2e17979dd5d1e Mon Sep 17 00:00:00 2001 From: Jukka Matilainen Date: Tue, 8 Aug 2023 20:13:43 +0300 Subject: [PATCH 3/4] Change the test to use the newly enabled cp:/ URLs Change the test to use the cp:/ URLs to refer to the stylesheets directly from the classpath. This should now make the test pass. --- src/test/java/TransformTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/TransformTest.java b/src/test/java/TransformTest.java index 5bed144..d1150fa 100644 --- a/src/test/java/TransformTest.java +++ b/src/test/java/TransformTest.java @@ -54,8 +54,7 @@ public void transformTest() { private Document transformDocument(Document document) throws TransformerException, FileNotFoundException { DOMResult result = new DOMResult(); TransformerFactory factory = TransformerFactory.newInstance(); - InputStream is = TransformTest.class.getResourceAsStream("/xsl/driver.xsl"); - Source source = new StreamSource(is, "file:/xsl/driver.xsl"); + Source source = new StreamSource("cp:/xsl/driver.xsl"); Transformer transformer = factory.newTransformer(source); transformer.transform(new DOMSource(document), result); return (Document) result.getNode(); From b7f6c895d3e8b9c14dcb8fccfec65f31df33925b Mon Sep 17 00:00:00 2001 From: Jukka Matilainen Date: Tue, 8 Aug 2023 20:14:43 +0300 Subject: [PATCH 4/4] Remove the unneeded XML catalog file Remove the XML catalog file, which is not needed in this solution. (Note that the previous code did not actually seem to utilize it either.) --- .../resources/org/xmlresolver/catalog.xml | 2555 ----------------- 1 file changed, 2555 deletions(-) delete mode 100644 src/main/resources/org/xmlresolver/catalog.xml diff --git a/src/main/resources/org/xmlresolver/catalog.xml b/src/main/resources/org/xmlresolver/catalog.xml deleted file mode 100644 index c269b71..0000000 --- a/src/main/resources/org/xmlresolver/catalog.xml +++ /dev/null @@ -1,2555 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -