View Javadoc

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      {
75          this.prefix = prefix;
76          this.functionName = functionName;
77          this.parameters = new ArrayList();
78      }
79  
80      public void addParameter(Expr parameter)
81      {
82          this.parameters.add(parameter);
83      }
84  
85  
86      public List getParameters()
87      {
88          return this.parameters;
89      }
90  
91      public String getPrefix()
92      {
93          return this.prefix;
94      }
95  
96      public String getFunctionName()
97      {
98          return this.functionName;
99      }
100 
101 
102     public String getText()
103     {
104         StringBuffer buf = new StringBuffer();
105         String prefix = getPrefix();
106 
107         if (prefix != null &&
108                 prefix.length() > 0)
109         {
110             buf.append(prefix);
111             buf.append(":");
112         }
113 
114         buf.append(getFunctionName());
115         buf.append("(");
116 
117         Iterator paramIter = getParameters().iterator();
118 
119         while (paramIter.hasNext()) {
120             Expr eachParam = (Expr) paramIter.next();
121 
122             buf.append(eachParam.getText());
123 
124             if (paramIter.hasNext())
125             {
126                 buf.append(", ");
127             }
128         }
129 
130         buf.append(")");
131 
132         return buf.toString();
133     }
134 
135     public Expr simplify()
136     {
137         List paramExprs = getParameters();
138         int paramSize = paramExprs.size();
139 
140         List newParams = new ArrayList(paramSize);
141 
142         for (int i = 0; i < paramSize; ++i)
143         {
144             Expr eachParam = (Expr) paramExprs.get(i);
145 
146             newParams.add(eachParam.simplify());
147         }
148 
149         this.parameters = newParams;
150 
151         return this;
152     }
153 
154 
155     public String toString()
156     {
157         String prefix = getPrefix();
158 
159         if (prefix == null)
160         {
161             return "[(DefaultFunctionCallExpr): " + getFunctionName() + "(" + getParameters() + ") ]";
162         }
163 
164         return "[(DefaultFunctionCallExpr): " + getPrefix() + ":" + getFunctionName() + "(" + getParameters() + ") ]";
165     }
166 
167     public Object evaluate(Context context) throws JaxenException
168     {
169         String prefix = getPrefix();
170         String namespaceURI = null;
171         // default namespace is not used within XPath expressions
172         if (prefix != null && !"".equals(prefix)) {
173             namespaceURI = context.translateNamespacePrefixToUri(prefix);
174         }
175 
176         Function func = context.getFunction(namespaceURI,
177                 prefix,
178                 getFunctionName());
179         List paramValues = evaluateParams(context);
180 
181         return func.call(context, paramValues);
182     }
183 
184     public List evaluateParams(Context context) throws JaxenException
185     {
186         List paramExprs = getParameters();
187         int paramSize = paramExprs.size();
188 
189         List paramValues = new ArrayList(paramSize);
190 
191         for (int i = 0; i < paramSize; ++i)
192         {
193             Expr eachParam = (Expr) paramExprs.get(i);
194 
195             Object eachValue = eachParam.evaluate(context);
196 
197             paramValues.add(eachValue);
198         }
199         return paramValues;
200     }
201 
202 }
203