commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Perez Jorge <ape...@ignos.com>
Subject Re: JXPath - shared Context
Date Fri, 07 May 2004 09:47:07 GMT
Paul McClure wrote:

> I'm creating my JXPathContext with a parent context arg - but the new 
> context does not seem to see anything that was set in the parent 
> context. Should this work - this would be a very useful feature....
>
> JXPathContext sharedContext = JXPathContext.newContext(null);
> sharedContext.getVariables().declareVariable("title", "Java");

I don't agree.  Accessing variables declared/defined in parent context 
works for me.  Maybe it's a problem of your AbstractFactory?

> sharedContext.setFunctions(new MyExtensionFunctions());

It seems there is a bug in method getFunction(QName, Object[]) at file 
JXPathContextReferenceImpl.java.  The code just looks for the function 
to be called in all Functions objects established for each context, 
starting from the current context and then going on each parent 
context.  But the problem is that when a Function object has not been 
established for any of the context mentioned, the search stops.

Here is a patch that solves that problem (untested; for release 
commons-jxpath-1.1):

--- JXPathContextReferenceImpl.java.orig    Fri May  7 10:05:37 2004
+++ JXPathContextReferenceImpl.java    Fri May  7 10:06:06 2004
@@ -603,9 +603,6 @@
 
                 funcCtx = funcCtx.getParentContext();
             }
-            else {
-                break;
-            }
         }
         func = GENERIC_FUNCTIONS.getFunction(namespace, name, parameters);
         if (func != null) {
@@ -639,4 +636,4 @@
             throw new JXPathException("Cannot allocate " + className, ex);
         }
     }
-}
\ No newline at end of file
+}



> sharedContext.setLocale(Locale.CANADA);

Locale is also inherited at runtime from parent context, so it should work.

> sharedContext.setFactory(new MyFactory());

I haven't included here a test for that feature, but again parent 
context factory works for me in contexts where a parent context exists.  
I can publish a test case if anybody wants it.

>
> JXPathContext context = JXPathContext.newContext(sharedContext, auth);
>
> I follow this example, but my 'context' does not know about anything 
> in sharedContext.
>
> thanks,
> Paul
>

Here is the code I used to test that stuff.  I used a BeanShell script 
and two very simple classes.  To run this use the correct CLASSPATH to 
commons-jxpath.jar and bsh.jar and compile the two java classes:

---- testParentCtx.beanshell -- starts here

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.ClassFunctions;
import test.MyExtensionFunctions;
import test.MyDummyExtensionFunctions;

sharedContext = JXPathContext.newContext(null);
sharedContext.setFunctions(new 
ClassFunctions(MyExtensionFunctions.class, "myfunc"));
sharedContext.getVariables().declareVariable("title", "Java");
sharedContext.setLocale(new Locale("es", "ES"));

context = JXPathContext.newContext(sharedContext, null);
context.setFunctions(new ClassFunctions(MyDummyExtensionFunctions.class, 
"myfunc"));

System.out.println(context.getValue("myfunc:sayhello('hola')"));
System.out.println(context.getValue("$title"));
System.out.println(context.getLocale());

---- ends here

--- MyExtensionFunctions.java -- starts here

package test;

public class MyExtensionFunctions {

    static public String sayhello(String salutation) {

    return new String("I say " + salutation);
    }
}

--- end here

--- MyDummyExtensionFunctions.java -- starts here
package test;

public class MyDummyExtensionFunctions {

}
--- ends here


Cheers,

Adrian P.J.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message