Project

Profile

Help

Support #5864

closed

Using XSLT packages in a jar file

Added by Mark Dunn about 1 year ago. Updated 10 months ago.

Status:
Closed
Priority:
Low
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2023-01-31
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:
Platforms:

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

jar-resources.gif (10.6 KB) jar-resources.gif Mark Dunn, 2023-01-31 13:56
Actions #1

Updated by Michael Kay about 1 year 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.

Actions #2

Updated by Mark Dunn about 1 year 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.

Actions #3

Updated by Michael Kay about 1 year 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.

Actions #4

Updated by Mark Dunn about 1 year 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);
Actions #5

Updated by Michael Kay about 1 year ago

Great, good to know.

As a matter of interest, what does the result of url.toExternalForm() look like?

Actions #6

Updated by Mark Dunn about 1 year 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.)

Actions #7

Updated by Michael Kay 10 months ago

  • Status changed from New to Closed

Please register to edit this issue

Also available in: Atom PDF