commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmi...@apache.org
Subject cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri EvalContext.java
Date Wed, 29 May 2002 00:41:33 GMT
dmitri      02/05/28 17:41:33

  Modified:    jxpath/src/java/org/apache/commons/jxpath/ri/axes
                        AncestorContext.java ParentContext.java
                        PrecedingOrFollowingContext.java UnionContext.java
               jxpath/src/java/org/apache/commons/jxpath/ri
                        EvalContext.java
  Log:
  Fix problems with document order
  
  Revision  Changes    Path
  1.8       +8 -4      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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AncestorContext.java	28 Apr 2002 04:35:48 -0000	1.7
  +++ AncestorContext.java	29 May 2002 00:41:32 -0000	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/AncestorContext.java,v
1.7 2002/04/28 04:35:48 dmitri Exp $
  - * $Revision: 1.7 $
  - * $Date: 2002/04/28 04:35:48 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/AncestorContext.java,v
1.8 2002/05/29 00:41:32 dmitri Exp $
  + * $Revision: 1.8 $
  + * $Date: 2002/05/29 00:41:32 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -71,7 +71,7 @@
    * EvalContext that walks the "ancestor::" and "ancestor-or-self::" axes.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.7 $ $Date: 2002/04/28 04:35:48 $
  + * @version $Revision: 1.8 $ $Date: 2002/05/29 00:41:32 $
    */
   public class AncestorContext extends EvalContext {
       private NodeTest nodeTest;
  @@ -93,6 +93,10 @@
   
       public NodePointer getCurrentNodePointer(){
           return currentNodePointer;
  +    }
  +
  +    public int getDocumentOrder(){
  +        return -1;
       }
   
       public void reset(){
  
  
  
  1.7       +8 -4      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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ParentContext.java	28 Apr 2002 04:35:48 -0000	1.6
  +++ ParentContext.java	29 May 2002 00:41:32 -0000	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ParentContext.java,v
1.6 2002/04/28 04:35:48 dmitri Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/04/28 04:35:48 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ParentContext.java,v
1.7 2002/05/29 00:41:32 dmitri Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/05/29 00:41:32 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -71,7 +71,7 @@
    * EvalContext that walks the "parent::" axis.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.6 $ $Date: 2002/04/28 04:35:48 $
  + * @version $Revision: 1.7 $ $Date: 2002/05/29 00:41:32 $
    */
   public class ParentContext extends EvalContext {
       private NodeTest nodeTest;
  @@ -90,6 +90,10 @@
   
       public int getCurrentPosition(){
           return 1;
  +    }
  +
  +    public int getDocumentOrder(){
  +        return -1;
       }
   
       public void reset(){
  
  
  
  1.8       +8 -4      jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/PrecedingOrFollowingContext.java
  
  Index: PrecedingOrFollowingContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/PrecedingOrFollowingContext.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PrecedingOrFollowingContext.java	28 Apr 2002 04:35:48 -0000	1.7
  +++ PrecedingOrFollowingContext.java	29 May 2002 00:41:32 -0000	1.8
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/PrecedingOrFollowingContext.java,v
1.7 2002/04/28 04:35:48 dmitri Exp $
  - * $Revision: 1.7 $
  - * $Date: 2002/04/28 04:35:48 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/PrecedingOrFollowingContext.java,v
1.8 2002/05/29 00:41:32 dmitri Exp $
  + * $Revision: 1.8 $
  + * $Date: 2002/05/29 00:41:32 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -73,7 +73,7 @@
    * EvalContext that walks the "preceding::" and "following::" axes.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.7 $ $Date: 2002/04/28 04:35:48 $
  + * @version $Revision: 1.8 $ $Date: 2002/05/29 00:41:32 $
    */
   public class PrecedingOrFollowingContext extends EvalContext {
       private NodeTest nodeTest;
  @@ -95,6 +95,10 @@
   
       public NodePointer getCurrentNodePointer(){
           return currentNodePointer;
  +    }
  +
  +    public int getDocumentOrder(){
  +        return reverse ? -1 : 1;
       }
   
       public void reset(){
  
  
  
  1.7       +11 -4     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/UnionContext.java
  
  Index: UnionContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/UnionContext.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- UnionContext.java	28 Apr 2002 04:35:48 -0000	1.6
  +++ UnionContext.java	29 May 2002 00:41:32 -0000	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/UnionContext.java,v
1.6 2002/04/28 04:35:48 dmitri Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/04/28 04:35:48 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/UnionContext.java,v
1.7 2002/05/29 00:41:32 dmitri Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/05/29 00:41:32 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -73,7 +73,7 @@
    * of a union operation like (a | b)
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.6 $ $Date: 2002/04/28 04:35:48 $
  + * @version $Revision: 1.7 $ $Date: 2002/05/29 00:41:32 $
    */
   public class UnionContext extends EvalContext {
       private boolean startedSet = false;
  @@ -83,6 +83,13 @@
       public UnionContext(EvalContext parentContext, EvalContext contexts[]){
           super(parentContext);
           this.contexts = contexts;
  +    }
  +
  +    public int getDocumentOrder(){
  +        if (contexts.length > 1){
  +            return 1;
  +        }
  +        return super.getDocumentOrder();
       }
   
       public NodePointer getCurrentNodePointer(){
  
  
  
  1.15      +93 -22    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.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- EvalContext.java	8 May 2002 00:40:00 -0000	1.14
  +++ EvalContext.java	29 May 2002 00:41:33 -0000	1.15
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v
1.14 2002/05/08 00:40:00 dmitri Exp $
  - * $Revision: 1.14 $
  - * $Date: 2002/05/08 00:40:00 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v
1.15 2002/05/29 00:41:33 dmitri Exp $
  + * $Revision: 1.15 $
  + * $Date: 2002/05/29 00:41:33 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -79,7 +79,7 @@
    * implement behavior of various XPath axes: "child::", "parent::" etc.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.14 $ $Date: 2002/05/08 00:40:00 $
  + * @version $Revision: 1.15 $ $Date: 2002/05/29 00:41:33 $
    */
   public abstract class EvalContext implements ExpressionContext, Iterator {
       protected EvalContext parentContext;
  @@ -87,6 +87,15 @@
       protected int position = 0;
       private boolean startedSetIteration = false;
       private boolean done = false;
  +    private Iterator pointerIterator;
  +
  +    // Sorts in the reverse order to the one defined by the Comparable
  +    // interface.
  +    private static final Comparator REVERSE_COMPARATOR = new Comparator(){
  +        public int compare(Object o1, Object o2){
  +            return ((Comparable)o2).compareTo(o1);
  +        }
  +    };
   
       public EvalContext(EvalContext parentContext){
           this.parentContext = parentContext;
  @@ -105,41 +114,77 @@
       }
   
       /**
  +     * Determines the document order for this context.
  +     *
  +     * @return 1 ascending order, -1 descending order,
  +     *  0 - does not require ordering
  +     */
  +    public int getDocumentOrder(){
  +        // Default behavior: if the parent needs to be ordered,
  +        // this one needs to be ordered too
  +        if (parentContext != null && parentContext.getDocumentOrder() != 0){
  +            return 1;
  +        }
  +        return 0;
  +    }
  +
  +    /**
        * Returns true if there are mode nodes matching the context's constraints.
        */
       public boolean hasNext(){
  -        if (done){
  -            return false;
  +        if (pointerIterator != null){
  +            return pointerIterator.hasNext();
  +        }
  +
  +        if (getDocumentOrder() != 0){
  +            return constructIterator();
           }
  -        if (position == 0){
  -            while (nextSet()){
  -                if (nextNode()){
  -                    return true;
  +        else {
  +            if (done){
  +                return false;
  +            }
  +            if (position == 0){
  +                while (nextSet()){
  +                    if (nextNode()){
  +                        return true;
  +                    }
                   }
  +                return false;
               }
  -            return false;
  +            return true;
           }
  -        return true;
       }
   
       /**
        * Returns the next node pointer in the context
        */
       public Object next(){
  -        if (done || (position == 0 && !hasNext())){
  -            throw new NoSuchElementException();
  +        if (pointerIterator != null){
  +            return pointerIterator.next();
  +        }
  +
  +        if (getDocumentOrder() != 0){
  +            if (!constructIterator()){
  +                throw new NoSuchElementException();
  +            }
  +            return pointerIterator.next();
           }
  -        NodePointer pointer = getCurrentNodePointer();
  -        if (!nextNode()){
  -            done = true;
  -            while (nextSet()){
  -                if (nextNode()){
  -                    done = false;
  -                    break;
  +        else {
  +            if (done || (position == 0 && !hasNext())){
  +                throw new NoSuchElementException();
  +            }
  +            NodePointer pointer = getCurrentNodePointer();
  +            if (!nextNode()){
  +                done = true;
  +                while (nextSet()){
  +                    if (nextNode()){
  +                        done = false;
  +                        break;
  +                    }
                   }
               }
  +            return pointer;
           }
  -        return pointer;
       }
   
       /**
  @@ -148,6 +193,32 @@
       public void remove(){
           throw new UnsupportedOperationException(
               "JXPath iterators cannot remove nodes");
  +    }
  +
  +    private boolean constructIterator(){
  +        HashSet set = new HashSet();
  +        ArrayList list = new ArrayList();
  +        while (nextSet()){
  +            while (nextNode()){
  +                NodePointer pointer = getCurrentNodePointer();
  +                if (!set.contains(pointer)){
  +                    set.add(pointer);
  +                    list.add(pointer);
  +                }
  +            }
  +        }
  +        if (list.isEmpty()){
  +            return false;
  +        }
  +
  +        if (getDocumentOrder() == 1){
  +            Collections.sort(list);
  +        }
  +        else {
  +            Collections.sort(list, REVERSE_COMPARATOR);
  +        }
  +        pointerIterator = list.iterator();
  +        return true;
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message