commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Perez Jorge <ape...@ignos.com>
Subject [JXPath] Why array range check exists in isCollectionElement() ?
Date Thu, 02 Dec 2004 17:33:49 GMT
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: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message