commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Rouvoy <romain.rou...@lifl.fr>
Subject Re: [jxpath] Navigating across Lists
Date Mon, 16 May 2011 19:16:51 GMT
On 16 mai 2011, at 16:06, Matt Benson wrote:
> On Mon, May 16, 2011 at 4:51 AM, Romain Rouvoy <romain.rouvoy@lifl.fr> wrote:
>> I'm using JXPath to query a graph of beans, which are heavily using lists, e.g.:
>>> public class A {
>>>       List<B> getB() {...}
>>> }
>>> 
>>> public class B {
>>>       String getName() {...}
>>>       List<C> getC() {...}
>>> }
>>> 
>>> public class C {
>>>       String getName() {...}
>>> }
>> 
>> However, when I'm querying this graph of beans, the following request does not match
any elements (while it should return several elements):
>>> org.apache.commons.jxpath.JXPathContext.newContext(new A());
>>> context.getValue("b[@name='foo']/c/name");
>> Any idea? Does JXPath support navigation within lists?
> What should happen is that getValue(expr) returns the first value specified by your expression,
while iterate(expr) would be the way you would retrieve multiple results.  If you can distill
your problem down to a failing JUnit test, please feel free to attach this to a JIRA issue.

Thanks Matt, actually I use Scala to develop my application:
> import scala.reflect.BeanProperty
> 
> case class B(@BeanProperty val name:String)
> 
> object a {
>     @BeanProperty val b = List[B](new B("FOOs"), new B("BARs"))
> }

The annotation @BeanProperty is used to generate getters at compile-time.


> scala> val context = org.apache.commons.jxpath.JXPathContext.newContext(a)
> context: org.apache.commons.jxpath.JXPathContext = org.apache.commons.jxpath.ri.JXPathContextReferenceImpl@132bb617
> 
> scala> context.getValue("b")
> res27: java.lang.Object = List(B(FOOs), B(BARs))

This is an expected behavior...


> scala> context.getValue("b/name")
> org.apache.commons.jxpath.JXPathNotFoundException: No value for xpath: b/name
> 	at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathContextReferenceImpl.java:383)
> 	at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathContextReferenceImpl.java:313)
> 	at .<init>(<console>:29)
> 	at .<clinit>(<console>)
> 	at RequestResult$.<init>(<console>:9)
> 	at RequestResult$.<clinit>(<console>)
> 	at RequestResult$scala_repl_result(<console>)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:597)
> 	at scala.tools.nsc.Interpreter$Request$$a...

According to your answer, I should get a String ("FOOs") and not an exception...
Ideally, I would have expected a collection of String containing "FOOs" and "BARs"... :)


> scala> val i = context.iterate("b/name")
> i: java.util.Iterator[_] = org.apache.commons.jxpath.ri.compiler.Expression$ValueIterator@3b29642c
> 
> scala> i.hasNext
> res30: Boolean = false

And even when using the context.iterate method, the returned iterator is empty...

Is that ok if I submit my Scala code except within JIRA?

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


Mime
View raw message