问题
I have a camel route which is pretty simple, except for the used xquery which is why I extracted it into a separate file. In my camel route I call the xquery with the Transformer EIP:
.transform().xquery("resource:classpath:xquery/myCoolXQuery.xquery",String.class)
The XQuery itself works fine, but because of some functional changes I now want to import another XQuery module which I want to use as a "library" with some helping functions, because I have lots of XQuery-Files which could use them.
So what I did in my XQuery is:
xquery version "3.0" encoding "UTF-8";
declare namespace fn = 'http://www.w3.org/2005/xpath-functions';
(: Library of really helpful functions :)
import module namespace lib = "http://handofnod.com/xquery-lib" at "classpath:xquery/lib.xquery";
Question:
How can I configure Apache Camel that the StandardModuleURIResolver
is able to load the imported module from the classpath?
So far investigations/ the problem
Unfortunately apache camel now throws a NullpointerException
in the StandardModuleURIResolver
at:
if ("classpath".equals(absoluteURI.getScheme())) {
String path = absoluteURI.getPath();
is = this.config.getDynamicLoader().getResourceAsStream(path);
That's because the absoluteURI
(which is "classpath:xquery/lib.xquery"
) cannot be converted correctly and the variable path
is NULL
(that's pretty much code smell but not my business now).
The absoluteURI
cannot be converted because the net.sf.saxon.query.XQueryParser
has no value set in the this.env.getStaticBaseURI()
.
So I tried during debugging to set the staticBaseURI
value to classpath
but then the StandardModuleURIResolver
also returns an absoluteURI
which has a NULL
value in the getPath()
thus resulting in the same NullPointerException
.
So now I don't have any clue how to use the Transformer-EIP with an XQuery which uses a module import for some helper functions.
Another possible solution
Another possible solution would be to not use the Transformer-EIP implemented in Apache Camel but instead use the ToDefinition
because there it is possible to define a custom Saxon Configuration over an URL parameter which can use an overwritten ModuleURIResolver
which then hasn't the problem.
Also, sure I could use a custom Processor
implementation to achieve what I want, but that's not the point in using Camel which provides implementation for EIPs.
It would be great if I could use the Transformer-EIP with module import in my XQuery.
Project setup is:
- Spring-Boot 2.1.0.RELEASE
- Apache Camel 2.22.2
- Build with Maven
Final question
Can anyone help me pls?
Edit 1:
I downloaded the sources from Camel and Saxon to be able to debug with more understanding what happens and tried some things:
First I tried to use an absolute path:
.transform().xquery("resource:classpath:/xquery/myCoolXQuery.xquery",String.class)
This way I don't get the NullPointerException
anymore because in the StandardModuleURIResolver
at getQuerySource
the path is not NULL
anymore:
Unfortunately the file cannot be loaded by
config.getDynamicLoader().getResourceAsStream(path);
and returns NULL
.
So I thought I move the xquery files from the directory directly under src\main\resoures
but that also didn't help and I had the same result.
回答1:
Since it seems there is no answer to my question I debugged further and further and it really just seems impossible to configure the Transformer-EIP with XQuery in any way. Also I was not able to find any way to load the imported xquery module from the classpath so I changed my code and now I call my xquery via the XQueryEndpoint which provides support for custom configuration:
.to("xquery:/xquery/myCoolXQuery.xquery?configuration=#saxonConfig&resultsFormat=String")
This way my route works. But it would have been nice to be able to use the transform()
because the code would be easier to understand.
来源:https://stackoverflow.com/questions/56462217/import-module-into-xquery-with-apache-camel-transformer-eip