Uploaded image for project: 'Commons JXPath'
  1. Commons JXPath
  2. JXPATH-54

[JXPATH] Pointer invalid after iterating over Pointers referencing atomic values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.2 Final
    • None
    • None
    • Operating System: Windows XP
      Platform: All

    • 39397

    Description

      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(); }

      }
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            andreas.leitel@iclip.ch Andreas Leitel
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: