commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we9 <...@o2.pl>
Subject Re: [JXPath] Can JXPath help me with processing collection based tree?
Date Wed, 14 Nov 2012 21:09:50 GMT
Matt Benson, thank you for your answer.
As you wrote, I put IMyNodeAggregate into field myCollection in RootStub 
class. This is OK.

A)
1. public class Rules {
2.    public static String getName(ExpressionContext context){
3.       Pointer pointer = context.getContextNodePointer();
4.        if(pointer == null){
5.            return "";
6.        } else {
7.            if(pointer.getValue() instanceof IMyNode){
8.            System.out.println("POINTER SET ON " + pointer);
9.                IMyNode node = (IMyNode)pointer.getValue();
10.               return node.someService().getName();
11.           }
12.       }
13.       return "";
14.   }
15. }

RootStub is submitted to newContext:
JXPathContext context = JXPathContext.newContext(rootStub);

and

context.getValue("/myCollection");
returns myCollection - OK

context.getValue("//myCollection");
returns myCollection - OK

context.getValue("/myCollection/.[rules:getName() = 'secondName']");
returns the instance of IMyNode which someService().getName() returns 
"secondName" - OK

After line
context.getValue("/myCollection//.[rules:getName() = 'secondName']");
the next lines are NOT executed. Also no exception, and lenient mode is 
not set. I do not understand what is going on.
Line 8. of Rules class prints "POINTER SET ON /myCollection[1]"

After this I have a lot of doubts, if I can use JXPath in my case.

B)
Can I use regex like here?
[rules:getName() matches '.*Name']

Thank you,
Kamil

W dniu 2012-11-02 18:18, Matt Benson pisze:
> As Jun suggests, a custom extension function [1] is probably in order
> to keep your expressions reasonably (!) short.  Assuming you want to
> use JXPath without modifying any of your existing code, I would
> recommend trying to write a custom function that makes the call to
> retrieve the name.  Then your expression might look like
> ".[mynode:getName() = 'nodeName1']/.[mynode:getName() = 'nodeName2']"
> etc.  ISTR JXPath having a bit of trouble processing a collection as
> the root, so you might experiment with making your IMyNodeAggregate be
> a property of your root, or even the value of a variable.
>
> Thinking further, you might get some mileage out of doing some custom
> interface implementations.  The discussion of maps [2] in the user
> guide makes reference to the customization needed to treat any
> arbitrary structure similarly to a map, which arguably your
> IMyNodeAggregate is.
>
> HTH,
> Matt
>
> [1] http://commons.apache.org/jxpath/users-guide.html#Custom_Extension_Functions
> [2] http://commons.apache.org/jxpath/users-guide.html#Map_Element_Access
>
> On Fri, Nov 2, 2012 at 11:59 AM, Jun Ouyang <jouyang3@mail.ccsf.edu> wrote:
>> Sorry I am on transit right now so i cant give u a full and proper.respond.
>> But i think you can create your own jxpath function and invoke it to match
>> your need. In this case the function shall check if names are equal?
>> On Nov 2, 2012 9:47 AM, "we9" <we9@o2.pl> wrote:
>>
>>> I started to write some methods which process tree structure. The queries
>>> are based on XPath. And now, I discovered JXPath. I was reading user-guide,
>>> but I didn't find what I want.
>>> I must be sure that there is no posibility to do this with JXPath.
>>>
>>> I have the following interfaces:
>>> interface IMyNode
>>> interface IMyNodeAggregate<C extends IMyNode> implements Collection<C>,
>>> IMyNode
>>>
>>> I want to pass some root (instance of IMyAggregate) and using XPath
>>> notation (eg. /nodeName1//nodeName2/**nodeName3) get proper IMyNode
>>> instances. Name of IMyNode is avaliable by calling
>>> myNode.someService().getName() so maybe some interface JXPath should be
>>> implemented?
>>>
>>> Please answer me if JXPath can help me?
>>>
>>> Thank you in advance,
>>> Kamil
>>>
>>> ------------------------------**------------------------------**---------
>>> To unsubscribe, e-mail: user-unsubscribe@commons.**apache.org<user-unsubscribe@commons.apache.org>
>>> For additional commands, e-mail: user-help@commons.apache.org
>>>
>>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


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


Mime
View raw message