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: import com.caucho.xpath.XPathFun;
036: import com.caucho.xpath.pattern.AbstractPattern;
037:
038: import org.w3c.dom.Node;
039:
040: import java.util.ArrayList;
041:
042: /**
043: * Expressions based on custom library extensions.
044: */
045: public class FunExpr extends Expr {
046: private String _name;
047: private AbstractPattern _pattern;
048: private ArrayList<Expr> _args;
049:
050: public FunExpr(String name, AbstractPattern pattern,
051: ArrayList<Expr> args) {
052: _name = name;
053: _pattern = pattern;
054: _args = args;
055: }
056:
057: public boolean evalBoolean(Node node, ExprEnvironment env)
058: throws XPathException {
059: return toBoolean(evalObject(node, env));
060: }
061:
062: public double evalNumber(Node node, ExprEnvironment env)
063: throws XPathException {
064: return toDouble(evalObject(node, env));
065: }
066:
067: public String evalString(Node node, ExprEnvironment env)
068: throws XPathException {
069: return toString(evalObject(node, env));
070: }
071:
072: public Object evalObject(Node node, ExprEnvironment env)
073: throws XPathException {
074: XPathFun fun = env.getFunction(_name);
075:
076: // XXX: need to propagate the exception
077: if (fun == null)
078: throw new RuntimeException("unknown function: " + _name);
079:
080: ArrayList<Object> values = new ArrayList<Object>();
081: for (int i = 0; i < _args.size(); i++) {
082: Expr expr = _args.get(i);
083: values.add(expr.evalObject(node, env));
084: }
085:
086: return fun.eval(node, env, _pattern, values);
087: }
088:
089: public String toString() {
090: CharBuffer cb = new CharBuffer();
091: cb.append(_name);
092: cb.append("(");
093: for (int i = 0; i < _args.size(); i++) {
094: if (i != 0)
095: cb.append(", ");
096: cb.append(_args.get(i));
097: }
098: cb.append(")");
099:
100: return cb.toString();
101: }
102: }
|