Coverage Report - org.jaxen.expr.DefaultFunctionCallExpr
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultFunctionCallExpr
88%
44/50
83%
5/6
1.7
 
 1  
 /*
 2  
  * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/DefaultFunctionCallExpr.java,v 1.17 2006/11/30 18:49:27 elharo Exp $
 3  
  * $Revision: 1.17 $
 4  
  * $Date: 2006/11/30 18:49:27 $
 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: DefaultFunctionCallExpr.java,v 1.17 2006/11/30 18:49:27 elharo Exp $
 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  2644
     {
 75  2644
         this.prefix = prefix;
 76  2644
         this.functionName = functionName;
 77  2644
         this.parameters = new ArrayList();
 78  2644
     }
 79  
 
 80  
     public void addParameter(Expr parameter)
 81  
     {
 82  3022
         this.parameters.add(parameter);
 83  3022
     }
 84  
 
 85  
 
 86  
     public List getParameters()
 87  
     {
 88  10090
         return this.parameters;
 89  
     }
 90  
 
 91  
     public String getPrefix()
 92  
     {
 93  11708
         return this.prefix;
 94  
     }
 95  
 
 96  
     public String getFunctionName()
 97  
     {
 98  7468
         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  
                 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  2628
         List paramExprs = getParameters();
 138  2628
         int paramSize = paramExprs.size();
 139  
 
 140  2628
         List newParams = new ArrayList(paramSize);
 141  
 
 142  5642
         for (int i = 0; i < paramSize; ++i)
 143  
         {
 144  3014
             Expr eachParam = (Expr) paramExprs.get(i);
 145  
 
 146  3014
             newParams.add(eachParam.simplify());
 147  
         }
 148  
 
 149  2628
         this.parameters = newParams;
 150  
 
 151  2628
         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  4240
         String namespaceURI =
 170  
                 context.translateNamespacePrefixToUri(getPrefix());
 171  
 
 172  4240
         Function func = context.getFunction(namespaceURI,
 173  
                 getPrefix(),
 174  
                 getFunctionName());
 175  4234
         List paramValues = evaluateParams(context);
 176  
 
 177  4234
         return func.call(context, paramValues);
 178  
     }
 179  
 
 180  
     public List evaluateParams(Context context) throws JaxenException
 181  
     {
 182  4234
         List paramExprs = getParameters();
 183  4234
         int paramSize = paramExprs.size();
 184  
 
 185  4234
         List paramValues = new ArrayList(paramSize);
 186  
 
 187  6682
         for (int i = 0; i < paramSize; ++i)
 188  
         {
 189  2448
             Expr eachParam = (Expr) paramExprs.get(i);
 190  
 
 191  2448
             Object eachValue = eachParam.evaluate(context);
 192  
 
 193  2448
             paramValues.add(eachValue);
 194  
         }
 195  4234
         return paramValues;
 196  
     }
 197  
 
 198  
 }
 199