KickJava   Java API By Example, From Geeks To Geeks.

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


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.xpath.Expr;
33 import com.caucho.xpath.ExprEnvironment;
34 import com.caucho.xpath.XPathException;
35
36 import org.w3c.dom.Node JavaDoc;
37
38 import java.lang.reflect.Constructor JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * Implements the static java extension functions.
43  */

44 public class NewJavaExpr extends Expr {
45   private static final int J_BOOLEAN = 1;
46   private static final int J_BYTE = J_BOOLEAN + 1;
47   private static final int J_SHORT = J_BYTE + 1;
48   private static final int J_INT = J_SHORT + 1;
49   private static final int J_LONG = J_INT + 1;
50   private static final int J_FLOAT = J_LONG + 1;
51   private static final int J_DOUBLE = J_FLOAT + 1;
52   private static final int J_STRING = J_DOUBLE + 1;
53   private static final int J_OBJECT = J_STRING + 1;
54   
55   // Code of the expression defined in Expr
56
private Constructor JavaDoc constructor;
57   // Arguments for more than 3.
58
private ArrayList JavaDoc args;
59   private int []argTypes;
60
61   private int retType;
62
63   /**
64    * Create a StringExpression with three arguments.
65    *
66    * @param method Java method
67    * @param args the arguments
68    */

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

107   public boolean isString()
108   {
109     return retType == J_STRING;
110   }
111
112   /**
113    * True if this returns a boolean.
114    */

115   public boolean isBoolean()
116   {
117     return retType == J_BOOLEAN;
118   }
119
120   /**
121    * True if this returns a boolean.
122    */

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

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

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

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

182   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
183     throws XPathException
184   {
185     Object JavaDoc []argArray = new Object JavaDoc[args.size()];
186
187     for (int i = 0; i < argArray.length; i++) {
188       Expr expr = (Expr) args.get(i);
189
190       switch (argTypes[i]) {
191       case J_BOOLEAN:
192         argArray[i] = new Boolean JavaDoc(expr.evalBoolean(node, env));
193         break;
194       case J_BYTE:
195         argArray[i] = new Byte JavaDoc((byte) expr.evalNumber(node, env));
196         break;
197       case J_SHORT:
198         argArray[i] = new Short JavaDoc((short) expr.evalNumber(node, env));
199         break;
200       case J_INT:
201         argArray[i] = new Integer JavaDoc((int) expr.evalNumber(node, env));
202         break;
203       case J_LONG:
204         argArray[i] = new Long JavaDoc((long) expr.evalNumber(node, env));
205         break;
206       case J_FLOAT:
207         argArray[i] = new Float JavaDoc((float) expr.evalNumber(node, env));
208         break;
209       case J_DOUBLE:
210         argArray[i] = new Double JavaDoc(expr.evalNumber(node, env));
211         break;
212       case J_STRING:
213         argArray[i] = expr.evalString(node, env);
214         break;
215       default:
216         argArray[i] = expr.evalObject(node, env);
217         break;
218       }
219     }
220
221     try {
222       return constructor.newInstance(argArray);
223     } catch (Exception JavaDoc e) {
224       throw new XPathException(e);
225     }
226   }
227
228   /**
229    * Return the expression as a string. toString() returns a valid
230    * XPath expression. This lets applications like XSLT use toString()
231    * to print the string in the generated Java.
232    */

233   public String JavaDoc toString()
234   {
235     CharBuffer cb = CharBuffer.allocate();
236     cb.append("java:");
237     cb.append(constructor.getDeclaringClass().getName());
238     cb.append(".new");
239
240     cb.append("(");
241     for (int i = 0; i < args.size(); i++) {
242       if (i != 0)
243         cb.append(",");
244       cb.append(args.get(i));
245     }
246     cb.append(")");
247
248     return cb.close();
249   }
250 }
251
Popular Tags