1 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 ; 37 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 41 44 public class StaticJavaExpr 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 private Method _method; 57 private ArrayList args; 59 private int []argTypes; 60 61 private int retType; 62 63 69 public StaticJavaExpr(Method method, ArrayList args) 70 { 71 _method = method; 72 this.args = args; 73 74 argTypes = new int[args.size()]; 75 Class []paramClasses = _method.getParameterTypes(); 76 for (int i = 0; i < paramClasses.length; i++) 77 argTypes[i] = classToType(paramClasses[i]); 78 79 retType = classToType(_method.getReturnType()); 80 } 81 82 private int classToType(Class cl) 83 { 84 if (boolean.class.equals(cl) || Boolean .class.equals(cl)) 85 return J_BOOLEAN; 86 else if (byte.class.equals(cl) || Byte .class.equals(cl)) 87 return J_BYTE; 88 else if (short.class.equals(cl) || Short .class.equals(cl)) 89 return J_SHORT; 90 else if (int.class.equals(cl) || Integer .class.equals(cl)) 91 return J_INT; 92 else if (long.class.equals(cl) || Long .class.equals(cl)) 93 return J_LONG; 94 else if (float.class.equals(cl) || Float .class.equals(cl)) 95 return J_FLOAT; 96 else if (double.class.equals(cl) || Double .class.equals(cl)) 97 return J_DOUBLE; 98 else if (String .class.equals(cl)) 99 return J_STRING; 100 else 101 return J_OBJECT; 102 } 103 104 107 public boolean isString() 108 { 109 return retType == J_STRING; 110 } 111 112 115 public boolean isBoolean() 116 { 117 return retType == J_BOOLEAN; 118 } 119 120 123 public boolean isNumber() 124 { 125 return retType >= J_BYTE && retType <= J_DOUBLE; 126 } 127 128 136 public String evalString(Node node, ExprEnvironment env) 137 throws XPathException 138 { 139 Object value = evalObject(node, env); 140 141 return String.valueOf(value); 142 } 143 144 153 public boolean evalBoolean(Node node, ExprEnvironment env) 154 throws XPathException 155 { 156 return toBoolean(evalObject(node, env)); 157 } 158 159 168 public double evalNumber(Node node, ExprEnvironment env) 169 throws XPathException 170 { 171 return toDouble(evalObject(node, env)); 172 } 173 174 182 public Object evalObject(Node node, ExprEnvironment env) 183 throws XPathException 184 { 185 Object []argArray = new Object [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 (expr.evalBoolean(node, env)); 193 break; 194 case J_BYTE: 195 argArray[i] = new Byte ((byte) expr.evalNumber(node, env)); 196 break; 197 case J_SHORT: 198 argArray[i] = new Short ((short) expr.evalNumber(node, env)); 199 break; 200 case J_INT: 201 argArray[i] = new Integer ((int) expr.evalNumber(node, env)); 202 break; 203 case J_LONG: 204 argArray[i] = new Long ((long) expr.evalNumber(node, env)); 205 break; 206 case J_FLOAT: 207 argArray[i] = new Float ((float) expr.evalNumber(node, env)); 208 break; 209 case J_DOUBLE: 210 argArray[i] = new Double (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 _method.invoke(null, argArray); 223 } catch (Exception e) { 224 throw new XPathException(e); 225 } 226 } 227 228 233 public String toString() 234 { 235 CharBuffer cb = CharBuffer.allocate(); 236 cb.append("java:"); 237 cb.append(_method.getDeclaringClass().getName()); 238 cb.append("."); 239 cb.append(_method.getName()); 240 241 cb.append("("); 242 for (int i = 0; i < args.size(); i++) { 243 if (i != 0) 244 cb.append(","); 245 cb.append(args.get(i)); 246 } 247 cb.append(")"); 248 249 return cb.close(); 250 } 251 } 252 253 | Popular Tags |