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 import com.caucho.xpath.XPathFun; 36 import com.caucho.xpath.pattern.AbstractPattern; 37 38 import org.w3c.dom.Node ; 39 40 import java.util.ArrayList ; 41 42 45 public class FunExpr extends Expr { 46 private String _name; 47 private AbstractPattern _pattern; 48 private ArrayList <Expr> _args; 49 50 public FunExpr(String name, AbstractPattern pattern, ArrayList <Expr> args) 51 { 52 _name = name; 53 _pattern = pattern; 54 _args = args; 55 } 56 57 public boolean evalBoolean(Node node, ExprEnvironment env) 58 throws XPathException 59 { 60 return toBoolean(evalObject(node, env)); 61 } 62 63 public double evalNumber(Node node, ExprEnvironment env) 64 throws XPathException 65 { 66 return toDouble(evalObject(node, env)); 67 } 68 69 public String evalString(Node node, ExprEnvironment env) 70 throws XPathException 71 { 72 return toString(evalObject(node, env)); 73 } 74 75 public Object evalObject(Node node, ExprEnvironment env) 76 throws XPathException 77 { 78 XPathFun fun = env.getFunction(_name); 79 80 if (fun == null) 82 throw new RuntimeException ("unknown function: " + _name); 83 84 ArrayList <Object > values = new ArrayList <Object >(); 85 for (int i = 0; i < _args.size(); i++) { 86 Expr expr = _args.get(i); 87 values.add(expr.evalObject(node, env)); 88 } 89 90 return fun.eval(node, env, _pattern, values); 91 } 92 93 public String toString() 94 { 95 CharBuffer cb = new CharBuffer(); 96 cb.append(_name); 97 cb.append("("); 98 for (int i = 0; i < _args.size(); i++) { 99 if (i != 0) 100 cb.append(", "); 101 cb.append(_args.get(i)); 102 } 103 cb.append(")"); 104 105 return cb.toString(); 106 } 107 } 108 | Popular Tags |