You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dm...@apache.org on 2002/10/20 05:43:39 UTC

cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri EvalContext.java

dmitri      2002/10/19 20:43:39

  Modified:    jxpath/src/java/org/apache/commons/jxpath/ri/axes
                        ParentContext.java DescendantContext.java
                        AncestorContext.java
               jxpath/src/java/org/apache/commons/jxpath/ri
                        EvalContext.java
  Log:
  Fixed a problem with iteration - calling hasNext() multiple times would mess up the iteration
  
  Revision  Changes    Path
  1.8       +10 -22    jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ParentContext.java
  
  Index: ParentContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ParentContext.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ParentContext.java	29 May 2002 00:41:32 -0000	1.7
  +++ ParentContext.java	20 Oct 2002 03:43:38 -0000	1.8
  @@ -77,7 +77,6 @@
       private NodeTest nodeTest;
       private boolean setStarted = false;
       private NodePointer currentNodePointer;
  -    private HashSet visitedNodes = new HashSet();
   
       public ParentContext(EvalContext parentContext, NodeTest nodeTest){
           super(parentContext);
  @@ -107,22 +106,6 @@
       }
   
       public boolean nextNode(){
  -        while (nextIgnoreDuplicates()){
  -            NodePointer location = getCurrentNodePointer();
  -            if (!visitedNodes.contains(location)){
  -                visitedNodes.add(location.clone());
  -                position++;
  -                return true;
  -            }
  -        }
  -        return false;
  -    }
  -
  -    /**
  -     * Returns true if there is another object in the current set, even
  -     * if that object has already been encountered in the same iteration.
  -     */
  -    private boolean nextIgnoreDuplicates(){
           // Each set contains exactly one node: the parent
           if (setStarted){
               return false;
  @@ -133,6 +116,11 @@
           while (currentNodePointer != null && !currentNodePointer.isNode()){
               currentNodePointer = currentNodePointer.getParent();
           }
  -        return currentNodePointer != null && currentNodePointer.testNode(nodeTest);
  +        if (currentNodePointer != null &&
  +                currentNodePointer.testNode(nodeTest)){
  +            position++;
  +            return true;
  +        }
  +        return false;
       }
   }
  
  
  
  1.9       +5 -5      jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/DescendantContext.java
  
  Index: DescendantContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/DescendantContext.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DescendantContext.java	10 Aug 2002 01:32:38 -0000	1.8
  +++ DescendantContext.java	20 Oct 2002 03:43:38 -0000	1.9
  @@ -83,7 +83,7 @@
       private Stack stack;
       private NodePointer currentNodePointer;
       private boolean includeSelf;
  -    private final static NodeTest elementNodeTest = 
  +    private final static NodeTest elementNodeTest =
               new NodeNameTest(new QName(null, "*"));
   
       public DescendantContext(EvalContext parentContext, boolean includeSelf, NodeTest nodeTest){
  
  
  
  1.9       +6 -21     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/AncestorContext.java
  
  Index: AncestorContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/AncestorContext.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- AncestorContext.java	29 May 2002 00:41:32 -0000	1.8
  +++ AncestorContext.java	20 Oct 2002 03:43:39 -0000	1.9
  @@ -78,7 +78,6 @@
       private boolean setStarted = false;
       private NodePointer currentNodePointer;
       private boolean includeSelf;
  -    private HashSet visitedNodes = new HashSet();
   
       /**
        * @param parentContext represents the previous step on the path
  @@ -118,27 +117,12 @@
       }
   
       public boolean nextNode(){
  -        while (nextIgnoreDuplicates()){
  -            NodePointer location = getCurrentNodePointer();
  -            if (!visitedNodes.contains(location)){
  -                visitedNodes.add(location.clone());
  -                position++;
  -                return true;
  -            }
  -        }
  -        return false;
  -    }
  -
  -    /**
  -     * Returns true if there is another object in the current set, even
  -     * if that object has already been encountered in the same iteration.
  -     */
  -    private boolean nextIgnoreDuplicates(){
           if (!setStarted){
               setStarted = true;
               currentNodePointer = parentContext.getCurrentNodePointer();
               if (includeSelf){
                   if (currentNodePointer.testNode(nodeTest)){
  +                    position++;
                       return true;
                   }
               }
  @@ -152,6 +136,7 @@
               }
   
               if (currentNodePointer.testNode(nodeTest)){
  +                position++;
                   return true;
               }
           }
  
  
  
  1.18      +34 -27    jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java
  
  Index: EvalContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- EvalContext.java	3 Jul 2002 21:12:36 -0000	1.17
  +++ EvalContext.java	20 Oct 2002 03:43:39 -0000	1.18
  @@ -87,6 +87,7 @@
       protected int position = 0;
       private boolean startedSetIteration = false;
       private boolean done = false;
  +    private boolean hasPerformedIteratorStep = false;
       private Iterator pointerIterator;
   
       // Sorts in the reverse order to the one defined by the Comparable
  @@ -140,18 +141,10 @@
               return constructIterator();
           }
           else {
  -            if (done){
  -                return false;
  -            }
  -            if (position == 0){
  -                while (nextSet()){
  -                    if (nextNode()){
  -                        return true;
  -                    }
  -                }
  -                return false;
  +            if (!done && !hasPerformedIteratorStep){
  +                performIteratorStep();
               }
  -            return true;
  +            return !done;
           }
       }
   
  @@ -170,21 +163,34 @@
               return pointerIterator.next();
           }
           else {
  -            if (done || (position == 0 && !hasNext())){
  +            if (!done && !hasPerformedIteratorStep){
  +                performIteratorStep();
  +            }
  +            if (done){
                   throw new NoSuchElementException();
               }
  -            NodePointer pointer = (NodePointer)getCurrentNodePointer().clone();
  -            if (!nextNode()){
  -                done = true;
  -                while (nextSet()){
  -                    if (nextNode()){
  -                        done = false;
  -                        break;
  -                    }
  +            hasPerformedIteratorStep = false;
  +            return (NodePointer)getCurrentNodePointer().clone();
  +        }
  +    }
  +
  +    /**
  +     * Moves the iterator forward by one position
  +     */
  +    private void performIteratorStep(){
  +        done = true;
  +        if (position != 0 && nextNode()){
  +            done = false;
  +        }
  +        else {
  +            while (nextSet()){
  +                if (nextNode()){
  +                    done = false;
  +                    break;
                   }
               }
  -            return pointer;
           }
  +        hasPerformedIteratorStep = true;
       }
   
       /**
  @@ -202,8 +208,9 @@
               while (nextNode()){
                   NodePointer pointer = getCurrentNodePointer();
                   if (!set.contains(pointer)){
  -                    set.add(pointer);
  -                    list.add(pointer);
  +                    Pointer cln = (Pointer)pointer.clone();
  +                    set.add(cln);
  +                    list.add(cln);
                   }
               }
           }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>