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.xpath.Expr;
032: import com.caucho.xpath.ExprEnvironment;
033: import com.caucho.xpath.XPathException;
034:
035: import org.w3c.dom.Node;
036:
037: abstract public class AbstractNumberExpr extends Expr {
038: public boolean isNumber() {
039: return true;
040: }
041:
042: /**
043: * Evaluates to a variable.
044: *
045: * @param node the node to evaluate and use as a context.
046: * @param env the variable environment.
047: *
048: * @return a variable containing the value.
049: */
050: public Var evalVar(Node node, ExprEnvironment env)
051: throws XPathException {
052: double value = evalNumber(node, env);
053:
054: return NumberVar.create(value);
055: }
056:
057: /**
058: * Evaluates the expression as a number.
059: *
060: * @param node the node to evaluate and use as a context.
061: * @param env the variable environment.
062: *
063: * @return the numeric value
064: */
065: abstract public double evalNumber(Node node, ExprEnvironment env)
066: throws XPathException;
067:
068: /**
069: * Evaluates the expression as a boolean.
070: *
071: * @param node the current node
072: * @param env the variable environment.
073: *
074: * @return the boolean representation of the number.
075: */
076: public boolean evalBoolean(Node node, ExprEnvironment env)
077: throws XPathException {
078: double value = evalNumber(node, env);
079:
080: return value != 0.0 && !Double.isNaN(value);
081: }
082:
083: /**
084: * Evaluates the expression as a string.
085: *
086: * @param node the current node
087: * @param env the variable environment.
088: *
089: * @return the string representation of the number.
090: */
091: public String evalString(Node node, ExprEnvironment env)
092: throws XPathException {
093: double value = evalNumber(node, env);
094:
095: if ((int) value == value)
096: return String.valueOf((int) value);
097: else
098: return String.valueOf(value);
099: }
100:
101: /**
102: * Evaluates the expression as an object.
103: *
104: * @param node the current node
105: * @param env the variable environment.
106: *
107: * @return the Double representation of the number.
108: */
109: public Object evalObject(Node node, ExprEnvironment env)
110: throws XPathException {
111: return new Double(evalNumber(node, env));
112: }
113: }
|