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 Fri, 07 Feb 2003 00:51:41 GMT
dmitri      2003/02/06 16:51:41

  Modified:    jxpath/src/java/org/apache/commons/jxpath/util
                        ValueUtils.java BasicTypeConverter.java
               jxpath/xdocs users-guide.xml
               jxpath/src/test/org/apache/commons/jxpath/ri/compiler
                        TestFunctions.java ExtensionFunctionTest.java
               jxpath/xdocs/stylesheets project.xml
               jxpath/src/java/org/apache/commons/jxpath
                        PackageFunctions.java
               jxpath/src/java/org/apache/commons/jxpath/functions
                        MethodFunction.java ConstructorFunction.java
               jxpath/src/java/org/apache/commons/jxpath/ri/compiler
                        ExtensionFunction.java
               jxpath/src/java/org/apache/commons/jxpath/ri
                        EvalContext.java
  Added:       jxpath/src/java/org/apache/commons/jxpath NodeSet.java
  Log:
  Improved handling of collections as arguments of extension functions
  
  Revision  Changes    Path
  1.14      +5 -5      jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/ValueUtils.java
  
  Index: ValueUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/ValueUtils.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ValueUtils.java	17 Jan 2003 02:02:57 -0000	1.13
  +++ ValueUtils.java	7 Feb 2003 00:51:40 -0000	1.14
  @@ -531,7 +531,7 @@
        *
        * @param method The method that we wish to call
        */
  -    private static Method getAccessibleMethod(Method method) {
  +    public static Method getAccessibleMethod(Method method) {
   
           // Make sure we have a method to check
           if (method == null) {
  
  
  
  1.5       +99 -7     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/BasicTypeConverter.java
  
  Index: BasicTypeConverter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/util/BasicTypeConverter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BasicTypeConverter.java	29 Jan 2003 17:55:01 -0000	1.4
  +++ BasicTypeConverter.java	7 Feb 2003 00:51:40 -0000	1.5
  @@ -63,10 +63,17 @@
   
   import java.lang.reflect.Array;
   import java.lang.reflect.Modifier;
  -import java.util.*;
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Collections;
  +import java.util.HashSet;
  +import java.util.Iterator;
  +import java.util.List;
  +import java.util.Set;
   
   import org.apache.commons.jxpath.JXPathException;
   import org.apache.commons.jxpath.Pointer;
  +import org.apache.commons.jxpath.NodeSet;
   
   /**
    * The default implementation of TypeConverter.
  @@ -183,6 +190,9 @@
                   return canConvert(value, toType);
               }
           }
  +        else if (object instanceof NodeSet) {
  +            return canConvert(((NodeSet) object).getValues(), toType);
  +        }
           else if (object instanceof Pointer) {
               return canConvert(((Pointer) object).getValue(), toType);
           }
  @@ -259,7 +269,7 @@
                   for (int i = 0; i < length; i++) {
                       collection.add(Array.get(object, i));
                   }
  -                return collection;
  +                return unmodifiableCollection(collection);
               }
               else if (length == 1) {
                   Object value = Array.get(object, 0);
  @@ -281,7 +291,7 @@
               else if (Collection.class.isAssignableFrom(toType)) {
                   Collection collection = allocateCollection(toType);
                   collection.addAll((Collection) object);
  -                return collection;
  +                return unmodifiableCollection(collection);
               }
               else if (length == 1) {
                   Object value;
  @@ -303,6 +313,9 @@
                           + " elements");
               }
           }
  +        else if (object instanceof NodeSet) {
  +            return convert(((NodeSet) object).getValues(), toType);
  +        }
           else if (object instanceof Pointer) {
               return convert(((Pointer) object).getValue(), toType);
           }
  @@ -425,5 +438,84 @@
               return new HashSet();
           }
           throw new RuntimeException("Cannot create collection of type: " + type);
  +    }
  +    
  +    private Collection unmodifiableCollection(Collection collection) {
  +        if (collection instanceof List) {
  +            return Collections.unmodifiableList((List) collection);
  +        }
  +        else if (collection instanceof Set) {
  +            return Collections.unmodifiableSet((Set) collection);
  +        }
  +        // Cannot wrap it into a proper unmodifiable collection, 
  +        // so we just return the original collection itself
  +        return collection;
  +    }
  +    
  +    static class ValueNodeSet implements NodeSet {
  +        private List values;
  +        private List pointers;
  +
  +        public ValueNodeSet(List values) {
  +           this.values = values;
  +        }
  +        
  +        public List getValues() {
  +            return Collections.unmodifiableList(values);
  +        }
  +        
  +        public List getNodes() {
  +            return Collections.unmodifiableList(values);
  +        }
  +        
  +        public List getPointers() {
  +            if (pointers == null) {
  +                pointers = new ArrayList();
  +                for (int i = 0; i < values.size(); i++) {
  +                    pointers.add(new ValuePointer(values.get(i)));
  +                }
  +                pointers = Collections.unmodifiableList(pointers);
  +            }
  +            return pointers;
  +        }
  +    }
  +    
  +    static final class ValuePointer implements Pointer {
  +        private Object bean;
  +
  +        public ValuePointer(Object object) {
  +            this.bean = object;
  +        }
  +        
  +        public Object getValue() {
  +            return bean;
  +        }
  +        
  +        public Object getNode() {
  +            return bean;
  +        }
  +        public void setValue(Object value) {
  +            throw new UnsupportedOperationException();
  +        }
  +        
  +        public String asPath() {
  +            if (bean == null) {
  +                return "null()";
  +            }
  +            else if (bean instanceof Number) {
  +                String string = bean.toString();
  +                if (string.endsWith(".0")) {
  +                    string = string.substring(0, string.length() - 2);
  +                }
  +                return string;
  +            }
  +            else if (bean instanceof Boolean) {
  +                return ((Boolean) bean).booleanValue() ? "true()" : "false()";
  +            }
  +            else if (bean instanceof String) {
  +                return "'" + bean + "'";
  +            }
  +            return "{object of type " + bean.getClass().getName() + "}";
  +        }
       }
   }
  
  
  
  1.10      +26 -12    jakarta-commons/jxpath/xdocs/users-guide.xml
  
  Index: users-guide.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/xdocs/users-guide.xml,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- users-guide.xml	25 Jan 2003 01:50:37 -0000	1.9
  +++ users-guide.xml	7 Feb 2003 00:51:40 -0000	1.10
  @@ -112,6 +112,8 @@
               </li>
               <li><a href="#Expression Context">Expression Context</a>
               </li>
  +            <li><a href="#Collections as Arguments">Collections as Arguments</a>
  +            </li>
             </ul>
           </li>
           <li><a href="#Type Conversions">Type Conversions</a>
  @@ -1142,26 +1144,38 @@
             This expression will find all nodes of the graph that are
             dates.
           </p>
  -        <p>
  -          The current context is passed to an extension function by
  -          default. Any other context can be passed as an explicit
  -          argument declared as ExpressionContext. Note, that if the
  -          first argument is ExpressionContext, it is always passed
  -          the current context. Therefore, you may need to declare two
  -          ExpressionContext arguments- one for the current and one
  -          for the parameter context. For example,
  -        </p>
  +      </subsection>
  +      
  +      <subsection name="Collections as Arguments">
  +         There are two ways a collection can be passed to an extension function:
  +         as a <a href="apidocs/org/apache/commons/jxpath/NodeSet.html"><code>NodeSet</code></a>
  +         or as a Collection proper.  If the argument type is declared
  +         as NodeSet, JXPath will pass a NodeSet object, otherwise it will take values
  +         out of the node set and pass those to the function as a regular collection.
  +         Note that a collection is always passed to an extension function by value -
  +         it cannot be modified.
   
   <!--============================ + SOURCE + ============================-->
   <source>
                                                                          <b/>
   public class MyExtenstionFunctions {
      ...
  -   public static boolean contains(ExpressionContext current,
  -                            ExpressionContext context, Object value){
  -      Iterator iter = context.getContextNodeList().iterator();
  +   public static boolean contains(NodeSet nodeSet, Object value){
  +      Iterator iter = nodeSet.getPointers().iterator();
         while (iter.hasNext()) {
             Pointer item = (Pointer)iter.next();
  +          if (item.getValue().equals(value)){
  +            return true;
  +          }
  +      }
  +      return false;
  +   }
  +   
  +   // Alternative implementation
  +   public static boolean contains(List list, Object value){
  +      Iterator iter = list.iterator();
  +      while (iter.hasNext()) {
  +          Object item = iter.next();
             if (item.getValue().equals(value)){
               return true;
             }
  
  
  
  1.4       +16 -4     jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java
  
  Index: TestFunctions.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/TestFunctions.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestFunctions.java	20 Jan 2003 00:00:27 -0000	1.3
  +++ TestFunctions.java	7 Feb 2003 00:51:40 -0000	1.4
  @@ -62,10 +62,12 @@
   package org.apache.commons.jxpath.ri.compiler;
   
   import java.util.Collection;
  +import java.util.Iterator;
   import java.util.Map;
   
   import org.apache.commons.jxpath.ExpressionContext;
   import org.apache.commons.jxpath.Pointer;
  +import org.apache.commons.jxpath.NodeSet;
   
   /**
    * @author Dmitri Plotnikov
  @@ -147,6 +149,16 @@
        * the first argument.
        */
       public static int count(ExpressionContext context, Collection col) {
  +        for (Iterator iter = col.iterator(); iter.hasNext();) {
  +            Object element = iter.next();
  +            if (!(element instanceof String)) {
  +                throw new RuntimeException("Invalid argument");
  +            }
  +        };
           return col.size();
  +    }
  +    
  +    public static int countPointers(NodeSet nodeSet) {
  +        return nodeSet.getPointers().size();
       }
   }
  
  
  
  1.6       +45 -7     jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java
  
  Index: ExtensionFunctionTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/test/org/apache/commons/jxpath/ri/compiler/ExtensionFunctionTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ExtensionFunctionTest.java	29 Jan 2003 17:55:00 -0000	1.5
  +++ ExtensionFunctionTest.java	7 Feb 2003 00:51:40 -0000	1.6
  @@ -62,6 +62,7 @@
   
   package org.apache.commons.jxpath.ri.compiler;
   
  +import java.util.ArrayList;
   import java.util.List;
   import java.util.Locale;
   
  @@ -238,6 +239,34 @@
               "string(test:setFooAndBar($test, 7, 'biz'))",
               "foo=7; bar=biz");
       }
  +    
  +    public void testCollectionMethodCall() {
  +        
  +        List list = new ArrayList();
  +        list.add("foo");
  +        context.getVariables().declareVariable("myList", list);
  +
  +        assertXPathValue(
  +            context, 
  +            "size($myList)", 
  +            new Integer(1));
  +    
  +        assertXPathValue(
  +            context, 
  +            "size(beans)", 
  +            new Integer(2));
  +            
  +        boolean exception = false;
  +        try {
  +            assertXPathValue(context, "add($myList, 'hello')", Boolean.TRUE);
  +        }
  +        catch (Exception ex) {
  +            exception = true;
  +        }
  +        assertTrue(
  +            "Exception trying to add to an unmodifiable list",
  +            exception);
  +    }
   
       public void testStaticMethodCall() {
   
  @@ -273,19 +302,28 @@
               "//.[test:isMap()]/Key1", 
               "Value 1");
   
  -        // The function uses ExpressionContext to get to all
  -        // nodes in the context that is passed to it.
  +        // The function gets all
  +        // nodes in the context that match the pattern.
           assertXPathValue(
               context,
               "count(//.[test:count(strings) = 3])",
               new Double(7));
   
  -        // Another example of the same            
  +        // The function receives a collection of strings
  +        // and checks their type for testing purposes            
           assertXPathValue(
               context,
               "test:count(//strings)",
               new Integer(21));
   
  +        
  +        // The function receives a collection of pointers
  +        // and checks their type for testing purposes            
  +        assertXPathValue(
  +            context,
  +            "test:countPointers(//strings)",
  +            new Integer(21));
  +            
           // The function uses ExpressionContext to get to the current
           // pointer and returns its path.
           assertXPathValue(
  
  
  
  1.7       +1 -1      jakarta-commons/jxpath/xdocs/stylesheets/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/xdocs/stylesheets/project.xml,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- project.xml	30 Jan 2003 23:41:29 -0000	1.6
  +++ project.xml	7 Feb 2003 00:51:40 -0000	1.7
  @@ -21,7 +21,7 @@
              <item name="Client API&amp;nbsp;Documentation"   href="/apidocs/org/apache/commons/jxpath/package-summary.html"/>
              <item name="Complete API&amp;nbsp;Docs"        href="/apidocs/index.html"/>
              <item name="User's Guide"                  href="/users-guide.html"/>
  -           <item name="Release Notes"                 href="/release-notes-1.1.html"/>
  +           <!-- item name="JXPath Design"                 href="/design.html"/ -->
          </menu>
          <menu name="Project Files">
              <item name="Status"                        href="http://cvs.apache.org/viewcvs/~checkout~/jakarta-commons/jxpath/STATUS.html?content-type=text/html"/>
  
  
  
  1.9       +27 -5     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/PackageFunctions.java
  
  Index: PackageFunctions.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/PackageFunctions.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- PackageFunctions.java	29 Jan 2003 17:55:00 -0000	1.8
  +++ PackageFunctions.java	7 Feb 2003 00:51:40 -0000	1.9
  @@ -63,9 +63,9 @@
   
   import java.lang.reflect.Constructor;
   import java.lang.reflect.Method;
  -import java.util.*;
   import java.util.Collection;
   import java.util.Collections;
  +import java.util.Iterator;
   import java.util.Set;
   
   import org.apache.commons.jxpath.functions.ConstructorFunction;
  @@ -167,6 +167,28 @@
           if (parameters.length >= 1) {
               Object target = parameters[0];
               if (target != null) {
  +                Method method =
  +                    MethodLookupUtils.lookupMethod(
  +                        target.getClass(),
  +                        name,
  +                        parameters);
  +                if (method != null) {
  +                    return new MethodFunction(method);
  +                }
  +                    
  +                if (target instanceof NodeSet) {
  +                    target = ((NodeSet) target).getPointers();
  +                }
  +                
  +                method =
  +                    MethodLookupUtils.lookupMethod(
  +                        target.getClass(),
  +                        name,
  +                        parameters);
  +                if (method != null) {
  +                    return new MethodFunction(method);
  +                }
  +                
                   if (target instanceof Collection) {
                       Iterator iter = ((Collection) target).iterator();
                       if (iter.hasNext()) {
  
  
  
  1.1                  jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/NodeSet.java
  
  Index: NodeSet.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.jxpath;
  
  import java.util.List;
  
  /**
   * NodeSet interface can be used as the type of an argument of an extension
   * function.  Alternatively, the function can declare the argument as
   * a Collection (or List or Set), in which case it will be given a collection
   * of <i>values</i> matching the path.
   * 
   * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
   * @version $Id: NodeSet.java,v 1.1 2003/02/07 00:51:40 dmitri Exp $
   */
  public interface NodeSet {
  
      /**
       * Returns a list of nodes.
       */
      List getNodes();
      
      /**
       * Returns a list of pointers for all nodes in the set.
       */
      List getPointers();
      
      /**
       * Returns a list of values of all contained pointers.
       */
      List getValues();
      
  }
  
  
  
  1.8       +16 -7     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/functions/MethodFunction.java
  
  Index: MethodFunction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/functions/MethodFunction.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MethodFunction.java	11 Jan 2003 05:41:22 -0000	1.7
  +++ MethodFunction.java	7 Feb 2003 00:51:41 -0000	1.8
  @@ -61,6 +61,7 @@
    */
   package org.apache.commons.jxpath.functions;
   
  +import java.lang.reflect.InvocationTargetException;
   import java.lang.reflect.Method;
   import java.lang.reflect.Modifier;
   
  @@ -68,6 +69,7 @@
   import org.apache.commons.jxpath.Function;
   import org.apache.commons.jxpath.JXPathException;
   import org.apache.commons.jxpath.util.TypeUtils;
  +import org.apache.commons.jxpath.util.ValueUtils;
   
   /**
    * An XPath extension function implemented as an individual Java method.
  @@ -81,7 +83,7 @@
       private static final Object EMPTY_ARRAY[] = new Object[0];
   
       public MethodFunction(Method method) {
  -        this.method = method;
  +        this.method = ValueUtils.getAccessibleMethod(method);
       }
   
       public Object invoke(ExpressionContext context, Object[] parameters) {
  @@ -131,8 +133,15 @@
   
               return method.invoke(target, args);
           }
  -        catch (Exception exception) {
  -            throw new JXPathException("Cannot invoke " + method, exception);
  +        catch (Throwable ex) {
  +            if (ex instanceof InvocationTargetException) {
  +                ex = ((InvocationTargetException) ex).getTargetException();
  +            }
  +            throw new JXPathException("Cannot invoke " + method, ex);
           }
  +    }
  +    
  +    public String toString() {
  +        return method.toString();
       }
   }
  
  
  
  1.8       +10 -6     jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/functions/ConstructorFunction.java
  
  Index: ConstructorFunction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/functions/ConstructorFunction.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ConstructorFunction.java	11 Jan 2003 05:41:22 -0000	1.7
  +++ ConstructorFunction.java	7 Feb 2003 00:51:41 -0000	1.8
  @@ -62,6 +62,7 @@
   package org.apache.commons.jxpath.functions;
   
   import java.lang.reflect.Constructor;
  +import java.lang.reflect.InvocationTargetException;
   
   import org.apache.commons.jxpath.ExpressionContext;
   import org.apache.commons.jxpath.Function;
  @@ -107,10 +108,13 @@
               }
               return constructor.newInstance(args);
           }
  -        catch (Exception exception) {
  +        catch (Throwable ex) {
  +            if (ex instanceof InvocationTargetException) {
  +                ex = ((InvocationTargetException) ex).getTargetException();
  +            }
               throw new JXPathException(
                   "Cannot invoke constructor " + constructor,
  -                exception);
  +                ex);
           }
       }
   }
  
  
  
  1.9       +10 -13    jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunction.java
  
  Index: ExtensionFunction.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jxpath/src/java/org/apache/commons/jxpath/ri/compiler/ExtensionFunction.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ExtensionFunction.java	29 Jan 2003 17:55:00 -0000	1.8
  +++ ExtensionFunction.java	7 Feb 2003 00:51:41 -0000	1.9
  @@ -61,14 +61,12 @@
    */
   package org.apache.commons.jxpath.ri.compiler;
   
  +import java.util.Arrays;
  +
   import org.apache.commons.jxpath.Function;
   import org.apache.commons.jxpath.JXPathException;
  -import org.apache.commons.jxpath.Pointer;
  -import org.apache.commons.jxpath.ri.QName;
   import org.apache.commons.jxpath.ri.EvalContext;
  -
  -import java.util.Arrays;
  -import java.util.List;
  +import org.apache.commons.jxpath.ri.QName;
   
   /**
    * Represents  an element of the parse tree representing an extension function
  @@ -127,6 +125,7 @@
                   parameters[i] = convert(args[i].compute(context));
               }
           }
  +
           Function function =
               context.getRootContext().getFunction(functionName, parameters);
           if (function == null) {
  @@ -141,10 +140,8 @@
       
       private Object convert(Object object) {
           if (object instanceof EvalContext) {
  -            return ((EvalContext) object).getPointerList();
  +            return ((EvalContext) object).getNodeSet();
           }
           return object;
  -    }
  -    
  -    
  +    }  
   }
  
  
  
  1.22      +54 -13    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.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- EvalContext.java	29 Jan 2003 17:55:00 -0000	1.21
  +++ EvalContext.java	7 Feb 2003 00:51:41 -0000	1.22
  @@ -69,9 +69,10 @@
   import java.util.List;
   import java.util.NoSuchElementException;
   
  -import org.apache.commons.jxpath.*;
   import org.apache.commons.jxpath.ExpressionContext;
   import org.apache.commons.jxpath.JXPathContext;
  +import org.apache.commons.jxpath.JXPathException;
  +import org.apache.commons.jxpath.NodeSet;
   import org.apache.commons.jxpath.Pointer;
   import org.apache.commons.jxpath.ri.axes.RootContext;
   import org.apache.commons.jxpath.ri.model.NodePointer;
  @@ -260,7 +261,7 @@
        * of the parent contexts.  If there was an ongoing iteration over
        * this context, the method should not be called.
        */
  -    public List getPointerList() {
  +    public NodeSet getNodeSet() {
           if (position != 0) {
               throw new JXPathException(
                   "Simultaneous operations: "
  @@ -268,13 +269,7 @@
                       + "iterating over an EvalContext");
           }
           
  -        List list = new ArrayList();
  -        while (nextSet()) {
  -            while (nextNode()) {
  -                list.add(getCurrentNodePointer());
  -            }
  -        }
  -        return list;
  +        return new SimpleNodeSet();
       }
   
       public String toString() {
  @@ -381,4 +376,50 @@
           this.position = position;
           return true;
       }
  +    
  +    class SimpleNodeSet implements NodeSet {
  +        private List pointers;
  +        private List nodes;
  +        private List values;
  +
  +        public SimpleNodeSet() {
  +            pointers = new ArrayList();
  +            while (nextSet()) {
  +                while (nextNode()) {
  +                    pointers.add(getCurrentNodePointer());
  +                }
  +            }
  +        }
  +
  +        public List getPointers() {
  +            return Collections.unmodifiableList(pointers);
  +        }
  +
  +        public List getNodes() {
  +            if (nodes == null) {
  +                List pointers = getPointers();
  +                nodes = new ArrayList();
  +                for (int i = 0; i < pointers.size(); i++) {
  +                    Pointer pointer = (Pointer) pointers.get(i);
  +                    nodes.add(pointer.getValue());
  +                }
  +                nodes = Collections.unmodifiableList(nodes);
  +            }
  +            return nodes;
  +        }
  +
  +        public List getValues() {
  +            if (values == null) {
  +                List pointers = getPointers();
  +                values = new ArrayList();
  +                for (int i = 0; i < pointers.size(); i++) {
  +                    Pointer pointer = (Pointer) pointers.get(i);
  +                    values.add(pointer.getValue());
  +                }
  +                values = Collections.unmodifiableList(values);
  +            }
  +            return values;
  +        }
  +    }
  +    
   }
  
  
  

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


Mime
View raw message