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.pattern.NodeIterator;
036: import com.caucho.xpath.pattern.SingleNodeIterator;
037:
038: import org.w3c.dom.Node;
039:
040: public class VarExpr extends Expr {
041: private String name;
042:
043: public VarExpr(String name) {
044: this .name = name.intern();
045: }
046:
047: /**
048: * Returns the value of the variable as a boolean.
049: *
050: * @param node the current node
051: * @param env the XPath envivonment
052: *
053: * @return the boolean value
054: */
055: public boolean evalBoolean(Node node, ExprEnvironment env)
056: throws XPathException {
057: Var var = (Var) env.getVar(name);
058:
059: return var == null ? false : var.getBoolean();
060: }
061:
062: /**
063: * Returns the value of the variable as a double.
064: *
065: * @param node the current node
066: * @param env the XPath envivonment
067: *
068: * @return the double value
069: */
070: public double evalNumber(Node node, ExprEnvironment env)
071: throws XPathException {
072: Var var = env.getVar(name);
073:
074: return var == null ? Double.NaN : var.getDouble();
075: }
076:
077: /**
078: * Returns the value of the variable as a string
079: *
080: * @param cb the buffer to append the value
081: * @param node the current node
082: * @param env the XPath envivonment
083: *
084: * @return the string value
085: */
086: public void evalString(CharBuffer cb, Node node, ExprEnvironment env)
087: throws XPathException {
088: Var var = env.getVar(name);
089:
090: if (var != null)
091: var.getString(cb);
092: }
093:
094: /**
095: * Returns the value of the variable as a string
096: *
097: * @param env the XPath envivonment
098: * @param node the current node
099: *
100: * @return the string value
101: */
102: public String evalString(Node node, ExprEnvironment env)
103: throws XPathException {
104: Var var = env.getVar(name);
105:
106: return var == null ? "" : var.getString();
107: }
108:
109: /**
110: * Returns the value of the variable as an object
111: *
112: * @param env the XPath envivonment
113: * @param node the current node
114: *
115: * @return the value
116: */
117: public Object evalObject(Node node, ExprEnvironment env)
118: throws XPathException {
119: Var var = env.getVar(name);
120:
121: return var == null ? null : var.getObject();
122: }
123:
124: /**
125: * Returns the value of the variable as an variable
126: *
127: * @param node the current node
128: * @param env the XPath envivonment
129: *
130: * @return the value
131: */
132: public Var evalVar(Node node, ExprEnvironment env)
133: throws XPathException {
134: Var var = env.getVar(name);
135:
136: return var;
137: }
138:
139: /**
140: * Returns the value of the variable as a node set.
141: *
142: * @param node the current node
143: * @param env the variable envivonment
144: *
145: * @return the value
146: */
147: public NodeIterator evalNodeSet(Node node, ExprEnvironment env)
148: throws XPathException {
149: Var var = env.getVar(name);
150:
151: if (var == null)
152: return new SingleNodeIterator(env, null);
153: else
154: return var.getNodeSet(env);
155: }
156:
157: public String toString() {
158: return "$" + name;
159: }
160: }
|