1 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 ; 38 39 import java.lang.reflect.Method ; 40 import java.util.ArrayList ; 41 42 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 private Method method; 60 61 private Expr objArg; 62 private ArrayList args; 63 private int []argTypes; 64 65 private int retType; 66 67 73 public ObjectJavaExpr(Method method, Expr objArg, ArrayList args) 74 { 75 this.method = method; 76 this.objArg = objArg; 77 this.args = args; 78 79 argTypes = new int[args.size()]; 80 Class []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 cl) 88 { 89 if (boolean.class.equals(cl) || Boolean .class.equals(cl)) 90 return J_BOOLEAN; 91 else if (byte.class.equals(cl) || Byte .class.equals(cl)) 92 return J_BYTE; 93 else if (short.class.equals(cl) || Short .class.equals(cl)) 94 return J_SHORT; 95 else if (int.class.equals(cl) || Integer .class.equals(cl)) 96 return J_INT; 97 else if (long.class.equals(cl) || Long .class.equals(cl)) 98 return J_LONG; 99 else if (float.class.equals(cl) || Float .class.equals(cl)) 100 return J_FLOAT; 101 else if (double.class.equals(cl) || Double .class.equals(cl)) 102 return J_DOUBLE; 103 else if (String .class.equals(cl)) 104 return J_STRING; 105 else 106 return J_OBJECT; 107 } 108 109 112 public boolean isString() 113 { 114 return retType == J_STRING; 115 } 116 117 120 public boolean isBoolean() 121 { 122 return retType == J_BOOLEAN; 123 } 124 125 128 public boolean isNumber() 129 { 130 return retType >= J_BYTE && retType <= J_DOUBLE; 131 } 132 133 141 public String evalString(Node node, ExprEnvironment env) 142 throws XPathException 143 { 144 Object value = evalObject(node, env); 145 146 return String.valueOf(value); 147 } 148 149 158 public boolean evalBoolean(Node node, ExprEnvironment env) 159 throws XPathException 160 { 161 return toBoolean(evalObject(node, env)); 162 } 163 164 173 public double evalNumber(Node node, ExprEnvironment env) 174 throws XPathException 175 { 176 return toDouble(evalObject(node, env)); 177 } 178 179 187 public Object evalObject(Node node, ExprEnvironment env) 188 throws XPathException 189 { 190 Object []argArray = new Object [args.size()]; 191 192 Object 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 (expr.evalBoolean(node, env)); 205 break; 206 case J_BYTE: 207 argArray[i] = new Byte ((byte) expr.evalNumber(node, env)); 208 break; 209 case J_SHORT: 210 argArray[i] = new Short ((short) expr.evalNumber(node, env)); 211 break; 212 case J_INT: 213 argArray[i] = new Integer ((int) expr.evalNumber(node, env)); 214 break; 215 case J_LONG: 216 argArray[i] = new Long ((long) expr.evalNumber(node, env)); 217 break; 218 case J_FLOAT: 219 argArray[i] = new Float ((float) expr.evalNumber(node, env)); 220 break; 221 case J_DOUBLE: 222 argArray[i] = new Double (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 e) { 236 throw new XPathException(e); 237 } 238 } 239 240 245 public String 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 |