Support #5864
closedUsing XSLT packages in a jar file
0%
Description
For the project I am working on my entire code needs to be bundled in a jar file.
The Saxon configuration file is in the resources folder in the jar. (See attached screenshot jar-resources.gif.)
The packages are in a subfolder "packages".
The config file references the packages with a relative path, e.g.
<package name="http://www.oup.com/xsltPackages/logger" version="1.0" sourceLocation="packages/logger.xsl"/>
When I run the application I get an error saying that the package could not be located:
I/O error reported by XML parser processing file:/C:/SVN/software/Atlas/BITS2MARC/MARC_creator/packages/logger.xsl: C:\SVN\software\Atlas\BITS2MARC\MARC_creator\packages\logger.xsl (The system cannot find the path specified)
The application is looking outside the jar for the packages even though the config file is being read from within the jar.
Is there a way of writing the element so that the packages are picked up from the resources folder in the jar?
Files
Updated by Michael Kay almost 2 years ago
How do you supply details of the configuration file to Saxon? The first thing to establish is that Saxon knows the base URI.
Although JAR file URIs are non-standard, if we have a base URI that's a file within a JAR, we should be able in principle to use it to locate other files in the same JAR using relative URIs; the most likely explanation for the observed effect is that we don't know the base URI.
Updated by Mark Dunn almost 2 years ago
I'm getting the file from the resources folder as a stream:
InputStream saxonConfig = getClass().getClassLoader().getResourceAsStream("config-saxon.xml");
this.saxon = new Processor(new StreamSource(saxonConfig));
Is the base URI getting lost in this process?
I'm wondering if I might have better results by supplying an instance of the Configuration class instead of the file. But I've not looked into that.
Updated by Michael Kay almost 2 years ago
Well, there's no systemId property on the StreamSource, so Saxon doesn't have a base URI to work with.
And it's not easy to suggest what you should supply. You could try "classpath:config-saxon.xml" - I've no idea if that would work.
Updated by Mark Dunn almost 2 years ago
Perfect, thanks!
A method that works:
ClassLoader cl = getClass().getClassLoader();
String systemID = "config-saxon.xml";
InputStream saxonConfig = cl.getResourceAsStream(systemID);
URL url = cl.getResource(systemID);
Source source = new StreamSource(saxonConfig);
source.setSystemId(url.toExternalForm());
this.saxon = new Processor(source);
Updated by Michael Kay almost 2 years ago
Great, good to know.
As a matter of interest, what does the result of url.toExternalForm()
look like?
Updated by Mark Dunn almost 2 years ago
When I run the application within NetBeans the value is
file:/C:/SVN/software/Atlas/BITS2MARC/MARC_creator/target/classes/config-saxon.xml
When I run the application as a jar the value is
jar:file:/C:/mjd/data-samples/scripts/BITS2MARC/MARC_creator.jar!/config-saxon.xml
(Maven has placed the config-saxon.xml file at the top level of the jar when building the jar.)
Please register to edit this issue