The relative file path is used from the project directory instead of the source directory

My program reads in a document from a location that is not the root directory of the project. The document contains a relative path. When a program applies this path, it starts from the project root directory. How can I apply the path to the original location of the document?

Here are the details. The view is long but rather simple.

I have a Java project in Eclipse located in

C:\one\two\three\four\five 

The program performs an XSL transform that takes a Schematron schema as input and creates a new XSLT stylesheet as output. The circuit is in

 C:\one\two\three\four\five\six\S\P\schema.sch 

It contains this line, and a few more:

 <sch:let name="foo" select="document('../../C/P/bar.xml')"/> 

If you start with the location of the circuit and apply this relative path, you will get

 C:\one\two\three\four\five\six\C\P\bar.xml 

which is the correct location of bar.xml . However, when I run my program, I get a series of errors that seem similar or related to this:

 Recoverable error on line 1262 FODC0002: I/O error reported by XML parser processing file:/C:/one/two/three/C/P/bar.xml: C:\one\two\three\C\P\bar.xml (The system cannot find the path specified) 

FODC0002 is the error code for "Error retrieving resource." This makes sense because this is the wrong location for bar.xml . The relative path seems to apply to the project root directory. This is the corresponding code:

 void compileToXslt(byte[] schema) throws Exception { XsltCompiler comp = Runtime.getSaxonProcessor().newXsltCompiler(); comp.setURIResolver(resolver); Source source = resolver.resolve("iso_svrl_for_xslt2.xsl", null); XsltExecutable executable = comp.compile(source); XsltTransformer transformer = executable.load(); transformer.setSource(new StreamSource(new ByteArrayInputStream(schema))); Serializer serializer = new Serializer(); serializer.setOutputStream(new ByteArrayOutputStream()); transformer.setDestination(serializer); transformer.transform(); // Errors appear in logs during this line // ... 

Source - javax.xml.transform.Source . XSL related classes are owned by SAXON ( Javadoc ).

What can I do to fix this? Moving bar.xml to the place where the program is looking for it, and editing style.xsl are not parameters for me, since both files belong to a third-party library.

UPDATE:
Further research led me to the fact that I need to set the StreamSource system StreamSource . I tried replacing the string transformer.setSource(... as follows:

 StreamSource strSrc = new StreamSource(new ByteArrayInputStream(schema)); strSrc.setSystemId(new File("C:\\one\\two\\three\\four\\five\\six\\S\\P\\schema.sch").toURI() .toURL().toExternalForm()); transformer.setSource(strSrc); 

but I get the same results. Am I using setSystemId() incorrectly? Am I going the wrong way entirely?

+4
source share
1 answer

I do not have java, but I assume that you need to change the resolver to find the path you need.

You do not show how you will receive it. Of course, you can make it quick and dirty and just change the working directory in the debugging settings under the arguments tab. But I suppose you don't want to do this

0
source

All Articles