Coverage Report - org.jaxen.expr.DefaultFunctionCallExpr
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultFunctionCallExpr
89%
49/55
72%
13/18
2
 
 1  
 /*
 2  
  * $Header$
 3  
  * $Revision$
 4  
  * $Date$
 5  
  *
 6  
  * ====================================================================
 7  
  *
 8  
  * Copyright 2000-2004 bob mcwhirter & James Strachan.
 9  
  * All rights reserved.
 10  
  *
 11  
  *
 12  
  * Redistribution and use in source and binary forms, with or without
 13  
  * modification, are permitted provided that the following conditions are
 14  
  * met:
 15  
  * 
 16  
  *   * Redistributions of source code must retain the above copyright
 17  
  *     notice, this list of conditions and the following disclaimer.
 18  
  * 
 19  
  *   * Redistributions in binary form must reproduce the above copyright
 20  
  *     notice, this list of conditions and the following disclaimer in the
 21  
  *     documentation and/or other materials provided with the distribution.
 22  
  * 
 23  
  *   * Neither the name of the Jaxen Project nor the names of its
 24  
  *     contributors may be used to endorse or promote products derived 
 25  
  *     from this software without specific prior written permission.
 26  
  * 
 27  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 28  
  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 29  
  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 30  
  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 31  
  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 32  
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 33  
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 34  
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 35  
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 36  
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 37  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 38  
  *
 39  
  * ====================================================================
 40  
  * This software consists of voluntary contributions made by many
 41  
  * individuals on behalf of the Jaxen Project and was originally
 42  
  * created by bob mcwhirter <bob@werken.com> and
 43  
  * James Strachan <jstrachan@apache.org>.  For more information on the
 44  
  * Jaxen Project, please see <http://www.jaxen.org/>.
 45  
  *
 46  
  * $Id$
 47  
  */
 48  
 
 49  
 package org.jaxen.expr;
 50  
 
 51  
 import java.util.ArrayList;
 52  
 import java.util.Iterator;
 53  
 import java.util.List;
 54  
 
 55  
 import org.jaxen.Context;
 56  
 import org.jaxen.Function;
 57  
 import org.jaxen.JaxenException;
 58  
 
 59  
 /**
 60  
  * @deprecated this class will become non-public in the future;
 61  
  *     use the interface instead
 62  
  */
 63  
 public class DefaultFunctionCallExpr extends DefaultExpr implements FunctionCallExpr
 64  
 {
 65  
     /**
 66  
      * 
 67  
      */
 68  
     private static final long serialVersionUID = -4747789292572193708L;
 69  
     private String prefix;
 70  
     private String functionName;
 71  
     private List parameters;
 72  
 
 73  
     public DefaultFunctionCallExpr(String prefix, String functionName)
 74  2784
     {
 75  2784
         this.prefix = prefix;
 76  2784
         this.functionName = functionName;
 77  2784
         this.parameters = new ArrayList();
 78  2784
     }
 79  
 
 80  
     public void addParameter(Expr parameter)
 81  
     {
 82  3272
         this.parameters.add(parameter);
 83  3272
     }
 84  
 
 85  
 
 86  
     public List getParameters()
 87  
     {
 88  10316
         return this.parameters;
 89  
     }
 90  
 
 91  
     public String getPrefix()
 92  
     {
 93  7554
         return this.prefix;
 94  
     }
 95  
 
 96  
     public String getFunctionName()
 97  
     {
 98  7554
         return this.functionName;
 99  
     }
 100  
 
 101  
 
 102  
     public String getText()
 103  
     {
 104  1084
         StringBuffer buf = new StringBuffer();
 105  1084
         String prefix = getPrefix();
 106  
 
 107  1084
         if (prefix != null &&
 108  1084
                 prefix.length() > 0)
 109  
         {
 110  0
             buf.append(prefix);
 111  0
             buf.append(":");
 112  
         }
 113  
 
 114  1084
         buf.append(getFunctionName());
 115  1084
         buf.append("(");
 116  
 
 117  1084
         Iterator paramIter = getParameters().iterator();
 118  
 
 119  2282
         while (paramIter.hasNext()) {
 120  1198
             Expr eachParam = (Expr) paramIter.next();
 121  
 
 122  1198
             buf.append(eachParam.getText());
 123  
 
 124  1198
             if (paramIter.hasNext())
 125  
             {
 126  392
                 buf.append(", ");
 127  
             }
 128  1198
         }
 129  
 
 130  1084
         buf.append(")");
 131  
 
 132  1084
         return buf.toString();
 133  
     }
 134  
 
 135  
     public Expr simplify()
 136  
     {
 137  2768
         List paramExprs = getParameters();
 138  2768
         int paramSize = paramExprs.size();
 139  
 
 140  2768
         List newParams = new ArrayList(paramSize);
 141  
 
 142  6032
         for (int i = 0; i < paramSize; ++i)
 143  
         {
 144  3264
             Expr eachParam = (Expr) paramExprs.get(i);
 145  
 
 146  3264
             newParams.add(eachParam.simplify());
 147  
         }
 148  
 
 149  2768
         this.parameters = newParams;
 150  
 
 151  2768
         return this;
 152  
     }
 153  
 
 154  
 
 155  
     public String toString()
 156  
     {
 157  0
         String prefix = getPrefix();
 158  
 
 159  0
         if (prefix == null)
 160  
         {
 161  0
             return "[(DefaultFunctionCallExpr): " + getFunctionName() + "(" + getParameters() + ") ]";
 162  
         }
 163  
 
 164  0
         return "[(DefaultFunctionCallExpr): " + getPrefix() + ":" + getFunctionName() + "(" + getParameters() + ") ]";
 165  
     }
 166  
 
 167  
     public Object evaluate(Context context) throws JaxenException
 168  
     {
 169  4326
         String prefix = getPrefix();
 170  4326
         String namespaceURI = null;
 171  
         // default namespace is not used within XPath expressions
 172  4326
         if (prefix != null && !"".equals(prefix)) {
 173  2
             namespaceURI = context.translateNamespacePrefixToUri(prefix);
 174  
         }
 175  
 
 176  8652
         Function func = context.getFunction(namespaceURI,
 177  
                 prefix,
 178  4326
                 getFunctionName());
 179  4320
         List paramValues = evaluateParams(context);
 180  
 
 181  4320
         return func.call(context, paramValues);
 182  
     }
 183  
 
 184  
     public List evaluateParams(Context context) throws JaxenException
 185  
     {
 186  4320
         List paramExprs = getParameters();
 187  4320
         int paramSize = paramExprs.size();
 188  
 
 189  4320
         List paramValues = new ArrayList(paramSize);
 190  
 
 191  6786
         for (int i = 0; i < paramSize; ++i)
 192  
         {
 193  2466
             Expr eachParam = (Expr) paramExprs.get(i);
 194  
 
 195  2466
             Object eachValue = eachParam.evaluate(context);
 196  
 
 197  2466
             paramValues.add(eachValue);
 198  
         }
 199  4320
         return paramValues;
 200  
     }
 201  
 
 202  
 }
 203