commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Perez Jorge <>
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  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 

---    Fri May  7 10:05:37 2004
+++    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);
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, 


---- ends here

--- -- starts here

package test;

public class MyExtensionFunctions {

    static public String sayhello(String salutation) {

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

--- end here

--- -- starts here
package test;

public class MyDummyExtensionFunctions {

--- ends here


Adrian P.J.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message