DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUGĀ·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=39397>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED ANDĀ·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=39397

           Summary: [JXPATH] Pointer invalid after iterating over Pointers
                    referencing atomic values
           Product: Commons
           Version: unspecified
          Platform: All
        OS/Version: Windows XP
            Status: NEW
          Severity: major
          Priority: P2
         Component: JXPath
        AssignedTo: [email protected]
        ReportedBy: [EMAIL PROTECTED]


We encountered a problem with Pointers when iterating over an array or a 
collection that holds atomic values such as Boolean. 

Actions:
- Create a bean exposing an array of Boolean objects
  getBoolArray()
- Obtain a Pointer iterator by calling:
  context.iteratePointers("somepath/boolArray");
- Iterate over all Pointers and store them in a new Collection
- Iterate over the new Collection
- For each Pointer, try to update the Boolean value.
--> The result is that only the last Boolean value of the original 
    array is updated.
    Apparently the Pointers do not keep the index of the referenced
    element withing the array. 
    The problem does also exist for Collections.
    
    We are using java5.

    We consider this to be an error since nowhere is stated that updating
    Pointer values is not possible after iterating over a list pointers.

    The only work around is to wrap the values (see Example)

Kind regards
Andreas

Example Code:

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;

public class JXPathTest {

  /**
   * @param args
   */
  @SuppressWarnings({"unchecked","unchecked"})
  public static void main(String[] args) {
    
    ArrayList arrayPointers = new ArrayList();
    ArrayList listPointers  = new ArrayList();
    ArrayList listWrappedPointers  = new ArrayList();
    
    
    AModel model = new AModel();
    System.out.println("start with:");
    model.printIt();
    // get pointers
    JXPathContext context = JXPathContext.newContext(model);    
    // get the array pointers
    Iterator resultPointerIterator = context.iteratePointers
("/booleans/arrBool");
    while (resultPointerIterator.hasNext()){
      Pointer ptr = (Pointer)resultPointerIterator.next();
      arrayPointers.add(ptr);
      
    }        
    // get list pointers
    resultPointerIterator = context.iteratePointers("/booleans/listBool");
    while (resultPointerIterator.hasNext()){
      Pointer ptr = (Pointer)resultPointerIterator.next();      
      listPointers.add(ptr);
    }
    // get wrapped boolean pointers
    resultPointerIterator = context.iteratePointers
("/booleans/listWrappedBool/booleanValue");
    while (resultPointerIterator.hasNext()){
      Pointer ptr = (Pointer)resultPointerIterator.next();      
      listWrappedPointers.add(ptr);
    }
    
    
    // modify pointers: We just invert every values
    // modify array content via iterated pointer (case Boolean[])
    Iterator it = arrayPointers.iterator();
    while (it.hasNext()){
      Pointer ptr = (Pointer)it.next();
      ptr.setValue(Boolean.TRUE.equals(ptr.getValue())?
Boolean.FALSE:Boolean.TRUE);
    }
    
    // modify collection content via iterated pointer (case Boolean List)
    it = listPointers.iterator();
    while (it.hasNext()){
      Pointer ptr = (Pointer)it.next();
      ptr.setValue(Boolean.TRUE.equals(ptr.getValue())?
Boolean.FALSE:Boolean.TRUE);
    }
    
    // modify wrapped boolean collection content via iterated pointer (case 
Wrapped Boolean List)
    it = listWrappedPointers.iterator();
    while (it.hasNext()){
      Pointer ptr = (Pointer)it.next();
      ptr.setValue(Boolean.TRUE.equals(ptr.getValue())?
Boolean.FALSE:Boolean.TRUE);
    }
    
    System.out.println("after invert --> not ok, only the boolean values of 
the wrapped boolean list are inverted");
    System.out.println("                 In the Boolean Array and the 
ArrayList only the last value is inverted.");
    
    model.printIt();
    
  }

  
  public static class AModel{
    SomeBooleans booleans = new SomeBooleans();
    
    public SomeBooleans getBooleans(){
      return booleans;
    }
    
    public void printIt(){
      booleans.printArrBool();
      booleans.printListBool();
      booleans.printListWrappedBool();
    }
  }
  
  public static class SomeBooleans{
    private Boolean[] arrBool = new Boolean[]{
        Boolean.TRUE,
        Boolean.FALSE,
        Boolean.TRUE,
        Boolean.FALSE,
    };
    
    ArrayList listBool = new ArrayList();
    
    ArrayList listWrappedBool = new ArrayList();
    
    public SomeBooleans(){
      // populate list with boolean Object references
      listBool.add(Boolean.TRUE);
      listBool.add(Boolean.FALSE);
      listBool.add(Boolean.TRUE);
      listBool.add(Boolean.FALSE);
      
      // populate list with boolean wrapper objects
      listWrappedBool.add(new BooleanWrapper(Boolean.TRUE));
      listWrappedBool.add(new BooleanWrapper(Boolean.FALSE));
      listWrappedBool.add(new BooleanWrapper(Boolean.TRUE));
      listWrappedBool.add(new BooleanWrapper(Boolean.FALSE));
    }

    /**
     * @return Returns the arrBool.
     */
    public Boolean[] getArrBool() {      
      return arrBool;
    }

    /**
     * @param arrBool The arrBool to set.
     */
    public void setArrBool(Boolean[] arrBool) {
      this.arrBool = arrBool;
    }

    /**
     * @return Returns the listBool.
     */
    public ArrayList getListBool() {
      return listBool;
    }

    /**
     * @param listBool The listBool to set.
     */
    public void setListBool(ArrayList listBool) {
      this.listBool = listBool;
    }
        
    /**
     * @return Returns the listWrappedBool.
     */
    public ArrayList getListWrappedBool() {
      return listWrappedBool;
    }

    /**
     * @param listWrappedBool The listWrappedBool to set.
     */
    public void setListWrappedBool(ArrayList listWrappedBool) {
      this.listWrappedBool = listWrappedBool;
    }

    public void printArrBool(){
      System.out.print("printArrBool:          [");
      for (int i= 0; i< arrBool.length; ++i){        
        System.out.print(i>0?", "+arrBool[i]:arrBool[i]);
      }
      System.out.println("]");
    }
    
    public void printListBool(){
      System.out.println("printListBool:         " + listBool);
    }   
    
    public void printListWrappedBool(){
      System.out.print("printListWrappedBool:  [");
      for (int i= 0; i< listWrappedBool.size(); ++i){        
        System.out.print(i>0?", "+listWrappedBool.get(i):listWrappedBool.get
(i));
      }
      System.out.println("]");
    }
  }
  
  public static class BooleanWrapper{
    Boolean booleanValue;

    public BooleanWrapper(Boolean booleanValue){
      this.booleanValue = booleanValue;
    }
    
    /**
     * @return Returns the booleanValue.
     */
    public Boolean getBooleanValue() {
      return booleanValue;
    }

    /**
     * @param booleanValue The booleanValue to set.
     */
    public void setBooleanValue(Boolean booleanValue) {
      this.booleanValue = booleanValue;
    }
    
    public String toString(){
      return booleanValue.toString();
    }
  }
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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

Reply via email to