Adrian,
 
I wouldn't really want to rely on the ArrayIndexOutOfBoundsException.  There is 
no guarantee that the bean will properly throw this exception and keep the 
state of the bean intact.  There is no way of knowing that passing an invalid 
index won't leave some kind of horrible side-effect.
 
However, you don't need to convert the collection to an array. JXPath can 
handle all kinds of Collection objects.
 
As to your second question, the explanation has to do with the fact that XPath 
is designed to work with XML, which is missing an explicit notion of a 
collection.
 
Let's start with an XML file that looks like this:
 
<a>
  <b>first</b>
  <b>second</b>
</a>
 
The xpath /a/b[1] will return "first" and /a/b[2] will return "second", just as 
expected.
 
Now, let's say the file contains only one <b> element:
 
<a>
  <b>bee</b>
</a>
 
The xpath /a/b[1] will return "bee", but so will "/a/b".
 
As you can see, in XML a collection containing exactly one element can be 
treated either as a collection or a scalar.
 
In order to conform to the XPath standard at least in some sense, JXPath makes 
the same to be true for java objects: a collection of exactly one element can 
be treated either as a collection or as a scalar.
 
I hope this helps.
 
- Dmitri

Adrian Perez Jorge <[EMAIL PROTECTED]> wrote:
Hi,

In 
org.apache.commons.jxpath.ri.axes.SimplePathInterpreter.isCollectionElement(), 
why is there a range check? Why is it necessary?

My problem is this. Suppose I have developed a Bean wich has an 
indexed property, and the real storage is, let say, an ArrayList:

public class MyBean {

ArrayList storage = new ArrayList();

public String[] getStorage() {
return storage.toArray(new String[] {} );
}

public String getStorage(int i) {
this.storage.get(i);
}

public void setStorage(String[] storage) {

this.storage.clear();
for (int i = 0; i < storage.length; ++i) {
this.storage.add(storage[i]);
}
}

public void setStorage(int i, String str) {

this.storage.set(i, str);
}
}

When I call JXPathContext.getValue("storage[3]") it will do range 
check, so the ArrayList must be converted to an array each time I access 
to any of the elements.

Is it possible to avoid range check and just catch and manage the 
ArrayIndexOutOfBoundsException?

BTW, why range check is done for (xpath) indexes > 1? If I do 
JXPathContext.getValue("storage[1]") it does not do range check, but for 
JXPathContext.getValue("storage[2]") it does not. Why 1 is different? 
Think about an empty array.

Thanks in advance,

Adrian P.J.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to