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/04/24 05:32:48 UTC

cvs commit: jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes ChildContext.java InitialContext.java SelfContext.java

dmitri      02/04/23 20:32:48

  Modified:    jxpath/src/java/org/apache/commons/jxpath/ri
                        EvalContext.java JXPathContextReferenceImpl.java
               jxpath/src/java/org/apache/commons/jxpath/ri/axes
                        ChildContext.java InitialContext.java
                        SelfContext.java
  Log:
  Simplified ExpressionContext implementation
  
  Revision  Changes    Path
  1.9       +56 -86    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- EvalContext.java	21 Apr 2002 21:52:31 -0000	1.8
  +++ EvalContext.java	24 Apr 2002 03:32:48 -0000	1.9
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v 1.8 2002/04/21 21:52:31 dmitri Exp $
  - * $Revision: 1.8 $
  - * $Date: 2002/04/21 21:52:31 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/EvalContext.java,v 1.9 2002/04/24 03:32:48 dmitri Exp $
  + * $Revision: 1.9 $
  + * $Date: 2002/04/24 03:32:48 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -79,72 +79,60 @@
    * implement behavior of various XPath axes: "child::", "parent::" etc.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.8 $ $Date: 2002/04/21 21:52:31 $
  + * @version $Revision: 1.9 $ $Date: 2002/04/24 03:32:48 $
    */
  -public abstract class EvalContext {
  +public abstract class EvalContext implements ExpressionContext {
       protected EvalContext parentContext;
       protected RootContext rootContext;
       protected int position = 0;
       private boolean startedSetIteration = false;
  -    private EvalExpressionContext expressionContext;
   
       public EvalContext(EvalContext parentContext){
           this.parentContext = parentContext;
       }
   
  -    public class EvalExpressionContext implements ExpressionContext {
  -        public Pointer getContextNodePointer(){
  -            return getCurrentNodePointer();
  -        }
  -
  -        public JXPathContext getJXPathContext(){
  -            return getRootContext().getJXPathContext();
  -        }
  -
  -        public int getPosition(){
  -            return position;
  -        }
  +    public Pointer getContextNodePointer(){
  +        return getCurrentNodePointer();
  +    }
   
  -        public List getContextNodeList(){
  -            int pos = position;
  -            if (pos != 0){
  -                reset();
  -            }
  -            List list = new ArrayList();
  -            while(next()){
  -                list.add(getCurrentNodePointer());
  -            }
  -            if (pos != 0){
  -                setPosition(pos);
  -            }
  -            else {
  -                reset();
  -            }
  -            return list;
  -        }
  +    public JXPathContext getJXPathContext(){
  +        return getRootContext().getJXPathContext();
  +    }
   
  -        public String toString(){
  -            Pointer ptr = getContextNodePointer();
  -            if (ptr == null){
  -                return "Empty expression context";
  -            }
  -            else {
  -                return "Expression context [" + getPosition() + "] " + ptr.asPath();
  -            }
  -        }
  +    public int getPosition(){
  +        return position;
       }
   
       /**
  -     * Produces an ExpressionContext that when it needs to pass one to
  -     * an extenstion function.
  +     * Returns the list of all Pointers in this context
        */
  -    public ExpressionContext getExpressionContext(){
  -        if (expressionContext == null){
  -            expressionContext = new EvalExpressionContext();
  +    public List getContextNodeList() {
  +        int pos = position;
  +        if (pos != 0) {
  +            reset();
  +        }
  +        List list = new ArrayList();
  +        while (next()) {
  +            list.add(getCurrentNodePointer());
           }
  -        return expressionContext;
  +        if (pos != 0) {
  +            setPosition(pos);
  +        }
  +        else {
  +            reset();
  +        }
  +        return list;
       }
   
  +    public String toString() {
  +        Pointer ptr = getContextNodePointer();
  +        if (ptr == null) {
  +            return "Empty expression context";
  +        }
  +        else {
  +            return "Expression context [" + getPosition() + "] " + ptr.asPath();
  +        }
  +    }
       /**
        * Returns the root context of the path, which provides easy
        * access to variables and functions.
  @@ -172,7 +160,7 @@
        * returns the first encountered Pointer that matches the current
        * step's criteria.  Otherwise, returns the current pointer.
        */
  -    public Pointer getContextNodePointer(){
  +    public Pointer getSingleNodePointer(){
           reset();
           while(nextSet()){
               if (next()){
  @@ -183,20 +171,6 @@
       }
   
       /**
  -     * Iterates through the current context collecting
  -     * pointers to all elements.
  -     */
  -    public List getContextNodeList(){
  -        List list = new ArrayList();
  -        while(nextSet()){
  -            while(next()){
  -                list.add(getCurrentNodePointer());
  -            }
  -        }
  -        return list;
  -    }
  -
  -    /**
        * Returns the current context node. Undefined before the beginning
        * of the iteration.
        */
  @@ -206,16 +180,16 @@
        * Returns true if there is another sets of objects to interate over.
        * Resets the current position and node.
        */
  -    public boolean nextSet(){
  -        reset();     // Restart iteration within the set
  +    public boolean nextSet() {
  +        reset(); // Restart iteration within the set
   
           // Most of the time you have one set per parent node
           // First time this method is called, we should look for
           // the first parent set that contains at least one node.
  -        if (!startedSetIteration){
  +        if (!startedSetIteration) {
               startedSetIteration = true;
  -            while (parentContext.nextSet()){
  -                if (parentContext.next()){
  +            while (parentContext.nextSet()) {
  +                if (parentContext.next()) {
                       return true;
                   }
               }
  @@ -224,20 +198,19 @@
   
           // In subsequent calls, we see if the parent context
           // has any nodes left in the current set
  -        if (parentContext.next()){
  +        if (parentContext.next()) {
               return true;
           }
   
           // If not, we look for the next set that contains
           // at least one node
  -        while (parentContext.nextSet()){
  -            if (parentContext.next()){
  +        while (parentContext.nextSet()) {
  +            if (parentContext.next()) {
                   return true;
               }
           }
           return false;
       }
  -
       /**
        * Returns true if there is another object in the current set.
        * Switches the current position and node to the next object.
  @@ -533,10 +506,10 @@
           }
   
           if (l instanceof EvalContext){
  -            l = ((EvalContext)l).getContextNodePointer();
  +            l = ((EvalContext)l).getSingleNodePointer();
           }
           if (r instanceof EvalContext){
  -            r = ((EvalContext)r).getContextNodePointer();
  +            r = ((EvalContext)r).getSingleNodePointer();
           }
   
           if (l instanceof Pointer && r instanceof Pointer){
  @@ -629,7 +602,7 @@
           }
           else if (object instanceof EvalContext){
               EvalContext ctx = (EvalContext)object;
  -            Pointer ptr = ctx.getContextNodePointer();
  +            Pointer ptr = ctx.getSingleNodePointer();
               if (ptr != null){
                   return stringValue(ptr);
               }
  @@ -933,7 +906,7 @@
       private int indexFromPredicate(Expression predicate){
           Object value = eval(predicate, true);
           if (value instanceof EvalContext){
  -            value = ((EvalContext)value).getContextNodePointer();
  +            value = ((EvalContext)value).getSingleNodePointer();
           }
           if (value instanceof NodePointer){
               value = ((NodePointer)value).getCanonicalValue();
  @@ -985,7 +958,7 @@
                   path.getEvaluationHint(ExpressionPath.BASIC_PREDICATES_HINT).equals(Boolean.TRUE) &&
                   !(context instanceof UnionContext)){
               EvalContext ctx = context;
  -            NodePointer ptr = (NodePointer)ctx.getContextNodePointer();
  +            NodePointer ptr = (NodePointer)ctx.getSingleNodePointer();
               if (ptr != null &&
                       (ptr.getIndex() == NodePointer.WHOLE_COLLECTION ||
                        predicates == null || predicates.length == 0)){
  @@ -1012,7 +985,7 @@
           if (firstMatch && steps.length != 0){
               boolean basic = path.getEvaluationHint(Path.BASIC_PATH_HINT).equals(Boolean.TRUE);
               if (basic){
  -                NodePointer ptr = (NodePointer)context.getContextNodePointer();
  +                NodePointer ptr = (NodePointer)context.getSingleNodePointer();
                   return interpretBasicPath(ptr, steps);
               }
           }
  @@ -1028,7 +1001,7 @@
           }
   
           if (firstMatch){
  -            Pointer ptr = context.getContextNodePointer();
  +            Pointer ptr = context.getSingleNodePointer();
   //            System.err.println("GETTING CTX POINTER: " + context + " " + ptr);
               return ptr;
           }
  @@ -1082,9 +1055,6 @@
               parameters = new Object[arguments.length];
               for (int i = 0; i < arguments.length; i++){
                   Object param = eval(arguments[i], false);
  -                if (param instanceof EvalContext){
  -                    param = ((EvalContext)param).getExpressionContext();
  -                }
                   parameters[i] = param;
               }
           }
  @@ -1094,7 +1064,7 @@
                    Arrays.asList(parameters));
           }
   
  -        return function.invoke(getExpressionContext(), parameters);
  +        return function.invoke(this, parameters);
       }
   
       /**
  @@ -1195,7 +1165,7 @@
       protected Object functionLang(CoreFunction function){
           assertArgCount(function, 1);
           String lang = stringValue(eval(function.getArg1()));
  -        NodePointer pointer = (NodePointer)getContextNodePointer();
  +        NodePointer pointer = (NodePointer)getSingleNodePointer();
           if (pointer == null){
               return Boolean.FALSE;
           }
  
  
  
  1.10      +15 -14    jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
  
  Index: JXPathContextReferenceImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- JXPathContextReferenceImpl.java	21 Apr 2002 21:52:32 -0000	1.9
  +++ JXPathContextReferenceImpl.java	24 Apr 2002 03:32:48 -0000	1.10
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java,v 1.9 2002/04/21 21:52:32 dmitri Exp $
  - * $Revision: 1.9 $
  - * $Date: 2002/04/21 21:52:32 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java,v 1.10 2002/04/24 03:32:48 dmitri Exp $
  + * $Revision: 1.10 $
  + * $Date: 2002/04/24 03:32:48 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -86,7 +86,7 @@
   
   /**
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.9 $ $Date: 2002/04/21 21:52:32 $
  + * @version $Revision: 1.10 $ $Date: 2002/04/24 03:32:48 $
    */
   public class JXPathContextReferenceImpl extends JXPathContext
   {
  @@ -117,13 +117,14 @@
           }
       }
   
  -    private static void createNodeFactoryArray(){
  -        if (nodeFactoryArray == null){
  -            nodeFactoryArray = (NodePointerFactory[])nodeFactories.toArray(new NodePointerFactory[0]);
  -            Arrays.sort(nodeFactoryArray, new Comparator(){
  -                public int compare(Object a, Object b){
  -                    int orderA = ((NodePointerFactory)a).getOrder();
  -                    int orderB = ((NodePointerFactory)b).getOrder();
  +    private static void createNodeFactoryArray() {
  +        if (nodeFactoryArray == null) {
  +            nodeFactoryArray =
  +                (NodePointerFactory[]) nodeFactories.toArray(new NodePointerFactory[0]);
  +            Arrays.sort(nodeFactoryArray, new Comparator() {
  +                public int compare(Object a, Object b) {
  +                    int orderA = ((NodePointerFactory) a).getOrder();
  +                    int orderB = ((NodePointerFactory) b).getOrder();
                       return orderA - orderB;
                   }
               });
  @@ -200,7 +201,7 @@
   
           if (result instanceof EvalContext){
               EvalContext ctx = (EvalContext)result;
  -            result = ctx.getContextNodePointer();
  +            result = ctx.getSingleNodePointer();
           }
           if (result instanceof NodePointer){
               result = ((NodePointer)result).getCanonicalValue();
  @@ -257,7 +258,7 @@
   //        System.err.println("XPATH: " + xpath);
           Object result = eval(xpath, true);
           if (result instanceof EvalContext){
  -            result = ((EvalContext)result).getContextNodePointer();
  +            result = ((EvalContext)result).getSingleNodePointer();
           }
           if (result instanceof Pointer){
               return (Pointer)result;
  @@ -303,7 +304,7 @@
           }
           else if (result instanceof EvalContext){
               EvalContext ctx = (EvalContext)result;
  -            pointer = ctx.getContextNodePointer();
  +            pointer = ctx.getSingleNodePointer();
           }
           else {
               // This should never happen
  
  
  
  1.6       +5 -5      jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ChildContext.java
  
  Index: ChildContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ChildContext.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ChildContext.java	21 Apr 2002 21:52:32 -0000	1.5
  +++ ChildContext.java	24 Apr 2002 03:32:48 -0000	1.6
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ChildContext.java,v 1.5 2002/04/21 21:52:32 dmitri Exp $
  - * $Revision: 1.5 $
  - * $Date: 2002/04/21 21:52:32 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/ChildContext.java,v 1.6 2002/04/24 03:32:48 dmitri Exp $
  + * $Revision: 1.6 $
  + * $Date: 2002/04/24 03:32:48 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -76,7 +76,7 @@
    * "preceding-sibling::" axes.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.5 $ $Date: 2002/04/21 21:52:32 $
  + * @version $Revision: 1.6 $ $Date: 2002/04/24 03:32:48 $
    */
   public class ChildContext extends EvalContext {
       private NodeTest nodeTest;
  @@ -112,7 +112,7 @@
        * of the collection.  For example, "books" will return the collection
        * of books rather than the first book from that collection.
        */
  -    public Pointer getContextNodePointer(){
  +    public Pointer getSingleNodePointer(){
           if (position == 0){
               while(nextSet()){
                   prepare();
  
  
  
  1.5       +5 -5      jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/InitialContext.java
  
  Index: InitialContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/InitialContext.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- InitialContext.java	21 Apr 2002 21:52:32 -0000	1.4
  +++ InitialContext.java	24 Apr 2002 03:32:48 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/InitialContext.java,v 1.4 2002/04/21 21:52:32 dmitri Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/04/21 21:52:32 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/InitialContext.java,v 1.5 2002/04/24 03:32:48 dmitri Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/04/24 03:32:48 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -74,7 +74,7 @@
    * on to the parent context.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.4 $ $Date: 2002/04/21 21:52:32 $
  + * @version $Revision: 1.5 $ $Date: 2002/04/24 03:32:48 $
    */
   public class InitialContext extends EvalContext {
       private boolean startedSet = false;
  @@ -91,7 +91,7 @@
           }
       }
   
  -    public Pointer getContextNodePointer(){
  +    public Pointer getSingleNodePointer(){
           return nodePointer;
       }
   
  
  
  
  1.5       +6 -10     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/SelfContext.java
  
  Index: SelfContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/SelfContext.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SelfContext.java	21 Apr 2002 21:52:32 -0000	1.4
  +++ SelfContext.java	24 Apr 2002 03:32:48 -0000	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/SelfContext.java,v 1.4 2002/04/21 21:52:32 dmitri Exp $
  - * $Revision: 1.4 $
  - * $Date: 2002/04/21 21:52:32 $
  + * $Header: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/axes/SelfContext.java,v 1.5 2002/04/24 03:32:48 dmitri Exp $
  + * $Revision: 1.5 $
  + * $Date: 2002/04/24 03:32:48 $
    *
    * ====================================================================
    * The Apache Software License, Version 1.1
  @@ -74,7 +74,7 @@
    * EvalContext that returns the current node from the parent context if the test succeeds.
    *
    * @author Dmitri Plotnikov
  - * @version $Revision: 1.4 $ $Date: 2002/04/21 21:52:32 $
  + * @version $Revision: 1.5 $ $Date: 2002/04/24 03:32:48 $
    */
   public class SelfContext extends EvalContext {
       private NodeTest nodeTest;
  @@ -87,12 +87,8 @@
           this.nodeTest = nodeTest;
       }
   
  -    public ExpressionContext getExpressionContext(){
  -        return parentContext.getExpressionContext();
  -    }
  -
  -    public Pointer getContextNodePointer(){
  -        return parentContext.getContextNodePointer();
  +    public Pointer getSingleNodePointer(){
  +        return parentContext.getSingleNodePointer();
       }
   
       public NodePointer getCurrentNodePointer(){
  
  
  

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