001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.expr;
030:
031: import com.caucho.util.CharBuffer;
032: import com.caucho.xpath.Expr;
033: import com.caucho.xpath.ExprEnvironment;
034: import com.caucho.xpath.XPathException;
035:
036: import org.w3c.dom.Node;
037:
038: import java.lang.reflect.Method;
039: import java.util.ArrayList;
040:
041: /**
042: * Implements the static java extension functions.
043: */
044: public class StaticJavaExpr extends Expr {
045: private static final int J_BOOLEAN = 1;
046: private static final int J_BYTE = J_BOOLEAN + 1;
047: private static final int J_SHORT = J_BYTE + 1;
048: private static final int J_INT = J_SHORT + 1;
049: private static final int J_LONG = J_INT + 1;
050: private static final int J_FLOAT = J_LONG + 1;
051: private static final int J_DOUBLE = J_FLOAT + 1;
052: private static final int J_STRING = J_DOUBLE + 1;
053: private static final int J_OBJECT = J_STRING + 1;
054:
055: // Code of the expression defined in Expr
056: private Method _method;
057: // Arguments for more than 3.
058: private ArrayList args;
059: private int[] argTypes;
060:
061: private int retType;
062:
063: /**
064: * Create a StringExpression with three arguments.
065: *
066: * @param method Java method
067: * @param args the arguments
068: */
069: public StaticJavaExpr(Method method, ArrayList args) {
070: _method = method;
071: this .args = args;
072:
073: argTypes = new int[args.size()];
074: Class[] paramClasses = _method.getParameterTypes();
075: for (int i = 0; i < paramClasses.length; i++)
076: argTypes[i] = classToType(paramClasses[i]);
077:
078: retType = classToType(_method.getReturnType());
079: }
080:
081: private int classToType(Class cl) {
082: if (boolean.class.equals(cl) || Boolean.class.equals(cl))
083: return J_BOOLEAN;
084: else if (byte.class.equals(cl) || Byte.class.equals(cl))
085: return J_BYTE;
086: else if (short.class.equals(cl) || Short.class.equals(cl))
087: return J_SHORT;
088: else if (int.class.equals(cl) || Integer.class.equals(cl))
089: return J_INT;
090: else if (long.class.equals(cl) || Long.class.equals(cl))
091: return J_LONG;
092: else if (float.class.equals(cl) || Float.class.equals(cl))
093: return J_FLOAT;
094: else if (double.class.equals(cl) || Double.class.equals(cl))
095: return J_DOUBLE;
096: else if (String.class.equals(cl))
097: return J_STRING;
098: else
099: return J_OBJECT;
100: }
101:
102: /**
103: * True if it returns a string.
104: */
105: public boolean isString() {
106: return retType == J_STRING;
107: }
108:
109: /**
110: * True if this returns a boolean.
111: */
112: public boolean isBoolean() {
113: return retType == J_BOOLEAN;
114: }
115:
116: /**
117: * True if this returns a boolean.
118: */
119: public boolean isNumber() {
120: return retType >= J_BYTE && retType <= J_DOUBLE;
121: }
122:
123: /**
124: * Evaluates the expression as an string.
125: *
126: * @param node the current node
127: * @param env the variable environment.
128: *
129: * @return the string representation of the expression.
130: */
131: public String evalString(Node node, ExprEnvironment env)
132: throws XPathException {
133: Object value = evalObject(node, env);
134:
135: return String.valueOf(value);
136: }
137:
138: /**
139: * Evaluate the expression as a boolean, i.e. evaluate it as a string
140: * and then convert it to a boolean.
141: *
142: * @param node the current node
143: * @param env the variable environment.
144: *
145: * @return the boolean representation of the expression.
146: */
147: public boolean evalBoolean(Node node, ExprEnvironment env)
148: throws XPathException {
149: return toBoolean(evalObject(node, env));
150: }
151:
152: /**
153: * Evaluate the expression as a double, i.e. evaluate it as a string
154: * and then convert it to a double.
155: *
156: * @param node the current node
157: * @param env the variable environment.
158: *
159: * @return the numeric representation of the expression.
160: */
161: public double evalNumber(Node node, ExprEnvironment env)
162: throws XPathException {
163: return toDouble(evalObject(node, env));
164: }
165:
166: /**
167: * Evaluate the expression as an object, i.e. return the string value.
168: *
169: * @param node the current node
170: * @param env the variable environment.
171: *
172: * @return the boolean representation of the expression.
173: */
174: public Object evalObject(Node node, ExprEnvironment env)
175: throws XPathException {
176: Object[] argArray = new Object[args.size()];
177:
178: for (int i = 0; i < argArray.length; i++) {
179: Expr expr = (Expr) args.get(i);
180:
181: switch (argTypes[i]) {
182: case J_BOOLEAN:
183: argArray[i] = new Boolean(expr.evalBoolean(node, env));
184: break;
185: case J_BYTE:
186: argArray[i] = new Byte((byte) expr
187: .evalNumber(node, env));
188: break;
189: case J_SHORT:
190: argArray[i] = new Short((short) expr.evalNumber(node,
191: env));
192: break;
193: case J_INT:
194: argArray[i] = new Integer((int) expr.evalNumber(node,
195: env));
196: break;
197: case J_LONG:
198: argArray[i] = new Long((long) expr
199: .evalNumber(node, env));
200: break;
201: case J_FLOAT:
202: argArray[i] = new Float((float) expr.evalNumber(node,
203: env));
204: break;
205: case J_DOUBLE:
206: argArray[i] = new Double(expr.evalNumber(node, env));
207: break;
208: case J_STRING:
209: argArray[i] = expr.evalString(node, env);
210: break;
211: default:
212: argArray[i] = expr.evalObject(node, env);
213: break;
214: }
215: }
216:
217: try {
218: return _method.invoke(null, argArray);
219: } catch (Exception e) {
220: throw new XPathException(e);
221: }
222: }
223:
224: /**
225: * Return the expression as a string. toString() returns a valid
226: * XPath expression. This lets applications like XSLT use toString()
227: * to print the string in the generated Java.
228: */
229: public String toString() {
230: CharBuffer cb = CharBuffer.allocate();
231: cb.append("java:");
232: cb.append(_method.getDeclaringClass().getName());
233: cb.append(".");
234: cb.append(_method.getName());
235:
236: cb.append("(");
237: for (int i = 0; i < args.size(); i++) {
238: if (i != 0)
239: cb.append(",");
240: cb.append(args.get(i));
241: }
242: cb.append(")");
243:
244: return cb.close();
245: }
246: }
|