On 21 Mar 2002, David Peterson wrote:

> I just had a query as to what the recommended way of adding a custom
> function to an XPath is. Reading through the JavaDocs and source code,
> I'm guessing it is something like the following: 
> 
>   XPathFunctionContext ctx = new XPathFunctionContext(); 
>   ctx.registerFunction(null, "custom", new CustomFunction()); 
> 
>   XPath xpath = new XPath("custom(//foo/bar)"); 
>   xpath.setFunctionContext(ctx); 
> 
> This process isn't documented anywhere, and the above is not necessarily
> a conclusion I would jump to - replacing the whole function context
> feels kind of wierd - I feel as though I may be replacing another custom
> context, wiping out it's set of custom functions. What if, for example,
> a couple of the supported OMs added their own implementation of the
> 'id()' function, and created their own subclass of XPathFunctionContext
> containing their IDFunction. The above code will remove the registered
> 'id()' function. Of course, if they read their JavaDocs, that fact would
> probably have been noted. 

That's the way I'd suggest doing it.

Each OM should *not* be creating OM-specific functions.  They *should*
have a function that can wrap behaviour of a Navigator if they need
OM-specific stuff.  ie, for id(), it wraps up Navigator.getElementById(...).

Your OM-specific Navigator may or may not support that method.

You, as a writer of extension functions that aren't included in the
jaxen distribution, though, are free to write them however you like.

> Anyway, I'm not suggesting a change, since I can see the reasons it is
> this way, but could the current 'recommended practice' be put down
> somewhere please? Also perhaps some documentation in
> BaseXPath.createFunctionContext() that it by default returns the
> singleton XPathFunctionContext. That would be a good hint to people like
> myself who are trawling the JavaDocs and don't fully read the class
> intro :)

Will do.

> Also, I was wondering what the benefit of having the singleton inside a
> static inner class is. I've seen the pattern before, but I've never
> understood the benefit of that over just having the singleton
> XPathFunctionContext as a private static final in XPathFunctionContext
> itself.

You know, it seemed like a good idea at the time, but now I'm having
trouble remembering why I did it that way.   ;)

Can anyone else justify my actions?

        -bob


_______________________________________________
Jaxen-interest mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jaxen-interest

Reply via email to