Yes, of course:
package cnde.module;
import org.basex.core.Context;
import org.basex.core.cmd.Add;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.DropDB;
import org.basex.core.cmd.XQuery;
import org.junit.Test;
public class DbXmlTest {
@Test
public void test() {
try {
Context context = new Context();
// String result1 = new XQuery("repo:install('C:\\Users\\jlansing\\workspaceTEST\\dbxml-module\\dbxml-module-1.0.0.xar')").execute(context);
// System.out.println(result1);
new CreateDB("test").execute(context);
new Add("12345", "<doc><guid>12345</guid></doc>").execute(context);
String result = new XQuery("import module namespace dbxml='http://www.sleepycat.com/2002/dbxml'; ((collection('test'))[1])/dbxml:metadata('dbxml:name')").execute(context);
String result2 = new XQuery("((collection('test'))[1])/document-uri()").execute(context);
System.out.println(result + " " + result2);
new DropDB("test").execute(context);
context.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
What’s the query you are trying to run?
___________________________
On Fri, Feb 1, 2013 at 6:07 PM, Lansing, Jeff J CIV
SPAWARSYSCEN-PACIFIC, 56250 <jeff.lansing@navy.mil> wrote:
> 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)
> };
>
> basex.xml:
> <package xmlns="http://www.basex.org/modules/pkg">
> <jar>containment.jar</jar>
> <class>cnde.service.operator.GraphContainment</class>
> </package>
>
> 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>
>
> ________________________________
> From: Christian Grün
> Sent: Fri 2/1/2013 8:39 AM
> To: Lansing, Jeff J CIV SPAWARSYSCEN-PACIFIC, 56250
> Cc: basex-talk@mailman.uni-konstanz.de
> Subject: Re: [basex-talk] Java User Function and Context Awareness
>
> 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
>>