KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.xpath.XPathFun;
36 import com.caucho.xpath.pattern.AbstractPattern;
37
38 import org.w3c.dom.Node JavaDoc;
39
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * Expressions based on custom library extensions.
44  */

45 public class FunExpr extends Expr {
46   private String JavaDoc _name;
47   private AbstractPattern _pattern;
48   private ArrayList JavaDoc<Expr> _args;
49
50   public FunExpr(String JavaDoc name, AbstractPattern pattern, ArrayList JavaDoc<Expr> args)
51   {
52     _name = name;
53     _pattern = pattern;
54     _args = args;
55   }
56
57   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
58     throws XPathException
59   {
60     return toBoolean(evalObject(node, env));
61   }
62
63   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
64     throws XPathException
65   {
66     return toDouble(evalObject(node, env));
67   }
68
69   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
70     throws XPathException
71   {
72     return toString(evalObject(node, env));
73   }
74
75   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
76     throws XPathException
77   {
78     XPathFun fun = env.getFunction(_name);
79     
80     // XXX: need to propagate the exception
81
if (fun == null)
82       throw new RuntimeException JavaDoc("unknown function: " + _name);
83
84     ArrayList JavaDoc<Object JavaDoc> values = new ArrayList JavaDoc<Object JavaDoc>();
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 JavaDoc 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