Yes, you are seeing the output of the second XQuery, which calls document-uri() for comparison.
But the first XQuery returned nothing (expected to see it return: "test/hdl:okwffu/12345").
Why?
It can only be because context.value was null (since every other path throws an Exception).
 
If you look in a debugger, you see that context.value had a DBNode value until just before UserFunc.value() at line 170, which was just before the metadata() function was called.


From: Christian Grün
Sent: Fri 2/1/2013 9:56 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

Your code outputs "test/12345" on my machine, so I’m not sure what’s
the problem, or what you were expecting to get.
___________________________

On Fri, Feb 1, 2013 at 6:47 PM, Lansing, Jeff J CIV
SPAWARSYSCEN-PACIFIC, 56250 <jeff.lansing@navy.mil> wrote:
> Sorry, wrong basex.xml. Here's the correct one:
>
> <package xmlns="http://www.basex.org/modules/pkg">
>   <jar>dbxml-module.jar</jar>
>   <class>cnde.module.DbXml</class>
> </package>
>
> Ant build.xml:
> <project default="module" name="dbxml-module" basedir=".">
>   <property name="base.name" value="dbxml-module"/>
>   <property name="jar.name" value="${base.name}.jar"/>
>   <path id="class.path">
>     <fileset dir="C:/xquery/basex">
>       <include name="BaseX.jar"/>
>     </fileset>
>   </path>
>   <target name="compile">
>     <mkdir dir="build"/>
>     <mkdir dir="build/classes"/>
>     <javac destdir="./build/classes" classpathref="class.path" debug="true">
>       <src path="src"/>
>     </javac>
>   </target>
>   <target name="jar" depends="compile">
>     <jar destfile="${jar.name}">
>       <fileset dir="./build/classes">
>         <include name="**/*.class"/>
>       </fileset>
>       <manifest>
>         <attribute name="Main-Class" value="cnde.module.DbXml"/>
>       </manifest>
>     </jar>
>     <delete dir="build"/>
>   </target>
>   <target name="module" depends="jar">
>     <zip destfile="${base.name}-1.0.0.xar">
>       <zipfileset dir=".">
>         <include name="expath-pkg.xml"/>
>         <include name="basex.xml"/>
>       </zipfileset>
>       <zipfileset dir="." prefix="dbxml">
>         <include name="wrapper.xq"/>
>         <include name="${jar.name}"/>
>       </zipfileset>
>     </zip>
>     <copy
> todir="C:\xquery\basex\repo\http-www.sleepycat.com-2002-dbxml-1.0.0\dbxml"
> file="${jar.name}"/>
>   </target>
> </project>
>
> ________________________________
> From: Christian Grün
> Sent: Fri 2/1/2013 9:30 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
>
> Hm, doesn’t look too self-contained (e.g., I can’t find
> GraphContainment anywhere). For testing purposes, it may be easier to
> build a simple BaseX package and do EXPath in a next step (see [1] for
> more details).
>
> Thanks,
> Christian
>
> [1] http://docs.basex.org/wiki/Repository
> ___________________________
>
> On Fri, Feb 1, 2013 at 6:22 PM, Lansing, Jeff J CIV
> SPAWARSYSCEN-PACIFIC, 56250 <jeff.lansing@navy.mil> wrote:
>> 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();
>>   }
>>  }
>> }
>>
>>
>>
>> ________________________________
>> From: Christian Grün
>> Sent: Fri 2/1/2013 9:15 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
>>
>> 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
>>>>