Java Source:
package cnde.module;
import org.basex.query.QueryException;
import org.basex.query.QueryModule;
import org.basex.query.iter.Iter;
import org.basex.query.value.Value;
import org.basex.query.value.item.Item;
import org.basex.query.value.item.Uri;
import org.basex.query.value.node.ANode;
import org.basex.query.value.type.NodeType;
public class DbXml extends QueryModule {
@Requires(Permission.NONE)
@ContextDependent
@FocusDependent
public String metadata(String qname) {
try {
Value val = context.value;
if(val == null) {
return null;
}
Item it = null;
if(val.isItem()) {
it = (Item)val;
} else {
throw new QueryException(val + " not an item");
}
if(!(it instanceof ANode)) throw new QueryException(it + " not a node");
ANode node = (ANode)it;
if(node.type != NodeType.DOC) return "node is not a document node";
final byte[] uri = node.baseURI();
String s = uri.length == 0 ? null : Uri.uri(uri, false).toString();
return s == null? "no uri for node" : s.replace("/", "/hdl:okwffu/");
} catch (QueryException e) {
System.err.println(e);
return e.getMessage();
}
}
}
wrapper.xq:
module namespace dbxml='http://www.sleepycat.com/2002/dbxml';
declare namespace p="java:cnde.module.DbXml";
declare function dbxml:metadata($key as xs:string) as xs:string* {
let $meta := p:new()
return p:metadata($meta, $key)
};
expath-pkg.xml:
<package xmlns="http://expath.org/ns/pkg" name="http://www.sleepycat.com/2002/dbxml" abbrev="dbxml" version="1.0.0" spec="1.0">
<title>dbxml metadata</title>
<dependency processor="basex"/>
<xquery>
<namespace>http://www.sleepycat.com/2002/dbxml</namespace>
<file>wrapper.xq</file>
</xquery>
</package>
Dear Jeff,
Afair, UserFunc should never be evaluated when calling a Java
function. Do you have an sscce for demonstrating your use case?
By extending the QueryModule class, you are getting access to all
internal query process information, and the current context is one of
them. As you already guessed, it can be retrieved via "context.value".
Another alternative is to pass on the current context as argument..
Q{java:.....}func(.)
Best,
Christian
___________________________
On Wed, Jan 30, 2013 at 6:40 PM, Lansing, Jeff J CIV
SPAWARSYSCEN-PACIFIC, 56250 <jeff.lansing@navy.mil> wrote:
> It seems that there is no way for a Java user function installed in the
> Repository to get access to the current context node.
> Yes the context is available because of extending abstract QueryModule, but
> it seems the value of the current context node has been wiped out by the
> call from UserFunc.value() [at UserFunc:line 170] before it gets to the
> actual user function [during line 175].
> Is there some way for a user function to recover that value, or is this just
> a limitation of the user function mechanism?
> Thanks.
>
> _______________________________________________
> BaseX-Talk mailing list
> BaseX-Talk@mailman.uni-konstanz.de
> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
>