KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > expr > ObjectJavaExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.expr;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.util.L10N;
33 import com.caucho.xpath.Expr;
34 import com.caucho.xpath.ExprEnvironment;
35 import com.caucho.xpath.XPathException;
36
37 import org.w3c.dom.Node JavaDoc;
38
39 import java.lang.reflect.Method JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * Implements the object java extension functions.
44  */

45 public class ObjectJavaExpr extends Expr {
46   private static L10N L = new L10N(ObjectJavaExpr.class);
47   
48   private static final int J_BOOLEAN = 1;
49   private static final int J_BYTE = J_BOOLEAN + 1;
50   private static final int J_SHORT = J_BYTE + 1;
51   private static final int J_INT = J_SHORT + 1;
52   private static final int J_LONG = J_INT + 1;
53   private static final int J_FLOAT = J_LONG + 1;
54   private static final int J_DOUBLE = J_FLOAT + 1;
55   private static final int J_STRING = J_DOUBLE + 1;
56   private static final int J_OBJECT = J_STRING + 1;
57   
58   // Code of the expression defined in Expr
59
private Method JavaDoc method;
60
61   private Expr objArg;
62   private ArrayList JavaDoc args;
63   private int []argTypes;
64
65   private int retType;
66
67   /**
68    * Create a StringExpression with three arguments.
69    *
70    * @param method Java method
71    * @param args the arguments
72    */

73   public ObjectJavaExpr(Method JavaDoc method, Expr objArg, ArrayList JavaDoc args)
74   {
75     this.method = method;
76     this.objArg = objArg;
77     this.args = args;
78
79     argTypes = new int[args.size()];
80     Class JavaDoc []paramClasses = method.getParameterTypes();
81     for (int i = 0; i < paramClasses.length; i++)
82       argTypes[i] = classToType(paramClasses[i]);
83     
84     retType = classToType(method.getReturnType());
85   }
86
87   private int classToType(Class JavaDoc cl)
88   {
89     if (boolean.class.equals(cl) || Boolean JavaDoc.class.equals(cl))
90       return J_BOOLEAN;
91     else if (byte.class.equals(cl) || Byte JavaDoc.class.equals(cl))
92       return J_BYTE;
93     else if (short.class.equals(cl) || Short JavaDoc.class.equals(cl))
94       return J_SHORT;
95     else if (int.class.equals(cl) || Integer JavaDoc.class.equals(cl))
96       return J_INT;
97     else if (long.class.equals(cl) || Long JavaDoc.class.equals(cl))
98       return J_LONG;
99     else if (float.class.equals(cl) || Float JavaDoc.class.equals(cl))
100       return J_FLOAT;
101     else if (double.class.equals(cl) || Double JavaDoc.class.equals(cl))
102       return J_DOUBLE;
103     else if (String JavaDoc.class.equals(cl))
104       return J_STRING;
105     else
106       return J_OBJECT;
107   }
108
109   /**
110    * True if it returns a string.
111    */

112   public boolean isString()
113   {
114     return retType == J_STRING;
115   }
116
117   /**
118    * True if this returns a boolean.
119    */

120   public boolean isBoolean()
121   {
122     return retType == J_BOOLEAN;
123   }
124
125   /**
126    * True if this returns a boolean.
127    */

128   public boolean isNumber()
129   {
130     return retType >= J_BYTE && retType <= J_DOUBLE;
131   }
132
133   /**
134    * Evaluates the expression as an string.
135    *
136    * @param node the current node
137    * @param env the variable environment.
138    *
139    * @return the string representation of the expression.
140    */

141   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
142     throws XPathException
143   {
144     Object JavaDoc value = evalObject(node, env);
145
146     return String.valueOf(value);
147   }
148
149   /**
150    * Evaluate the expression as a boolean, i.e. evaluate it as a string
151    * and then convert it to a boolean.
152    *
153    * @param node the current node
154    * @param env the variable environment.
155    *
156    * @return the boolean representation of the expression.
157    */

158   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
159     throws XPathException
160   {
161     return toBoolean(evalObject(node, env));
162   }
163
164   /**
165    * Evaluate the expression as a double, i.e. evaluate it as a string
166    * and then convert it to a double.
167    *
168    * @param node the current node
169    * @param env the variable environment.
170    *
171    * @return the numeric representation of the expression.
172    */

173   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
174     throws XPathException
175   {
176     return toDouble(evalObject(node, env));
177   }
178
179   /**
180    * Evaluate the expression as an object, i.e. return the string value.
181    *
182    * @param node the current node
183    * @param env the variable environment.
184    *
185    * @return the boolean representation of the expression.
186    */

187   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
188     throws XPathException
189   {
190     Object JavaDoc []argArray = new Object JavaDoc[args.size()];
191
192     Object JavaDoc obj = objArg.evalObject(node, env);
193
194     if (obj == null ||
195         ! (method.getDeclaringClass().isAssignableFrom(obj.getClass())))
196       throw new XPathException(L.l("Can't call method `{0}' on {1}.",
197                                    method.getName(), obj));
198
199     for (int i = 0; i < argArray.length; i++) {
200       Expr expr = (Expr) args.get(i);
201
202       switch (argTypes[i]) {
203       case J_BOOLEAN:
204         argArray[i] = new Boolean JavaDoc(expr.evalBoolean(node, env));
205         break;
206       case J_BYTE:
207         argArray[i] = new Byte JavaDoc((byte) expr.evalNumber(node, env));
208         break;
209       case J_SHORT:
210         argArray[i] = new Short JavaDoc((short) expr.evalNumber(node, env));
211         break;
212       case J_INT:
213         argArray[i] = new Integer JavaDoc((int) expr.evalNumber(node, env));
214         break;
215       case J_LONG:
216         argArray[i] = new Long JavaDoc((long) expr.evalNumber(node, env));
217         break;
218       case J_FLOAT:
219         argArray[i] = new Float JavaDoc((float) expr.evalNumber(node, env));
220         break;
221       case J_DOUBLE:
222         argArray[i] = new Double JavaDoc(expr.evalNumber(node, env));
223         break;
224       case J_STRING:
225         argArray[i] = expr.evalString(node, env);
226         break;
227       default:
228         argArray[i] = expr.evalObject(node, env);
229         break;
230       }
231     }
232
233     try {
234       return method.invoke(obj, argArray);
235     } catch (Exception JavaDoc e) {
236       throw new XPathException(e);
237     }
238   }
239
240   /**
241    * Return the expression as a string. toString() returns a valid
242    * XPath expression. This lets applications like XSLT use toString()
243    * to print the string in the generated Java.
244    */

245   public String JavaDoc toString()
246   {
247     CharBuffer cb = CharBuffer.allocate();
248     cb.append("java:");
249     cb.append(method.getDeclaringClass().getName());
250     cb.append(".");
251     cb.append(method.getName());
252
253     cb.append("(");
254     cb.append(objArg);
255     for (int i = 0; i < args.size(); i++) {
256       cb.append(",");
257       cb.append(args.get(i));
258     }
259     cb.append(")");
260
261     return cb.close();
262   }
263 }
264
Popular Tags