KickJava   Java API By Example, From Geeks To Geeks.

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


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.xpath.Expr;
32 import com.caucho.xpath.ExprEnvironment;
33 import com.caucho.xpath.XPathException;
34
35 import org.w3c.dom.Node JavaDoc;
36
37 abstract public class AbstractNumberExpr extends Expr {
38   public boolean isNumber()
39   {
40     return true;
41   }
42
43   /**
44    * Evaluates to a variable.
45    *
46    * @param node the node to evaluate and use as a context.
47    * @param env the variable environment.
48    *
49    * @return a variable containing the value.
50    */

51   public Var evalVar(Node JavaDoc node, ExprEnvironment env)
52     throws XPathException
53   {
54     double value = evalNumber(node, env);
55
56     return NumberVar.create(value);
57   }
58
59   /**
60    * Evaluates the expression as a number.
61    *
62    * @param node the node to evaluate and use as a context.
63    * @param env the variable environment.
64    *
65    * @return the numeric value
66    */

67   abstract public double evalNumber(Node JavaDoc node, ExprEnvironment env)
68     throws XPathException;
69
70   /**
71    * Evaluates the expression as a boolean.
72    *
73    * @param node the current node
74    * @param env the variable environment.
75    *
76    * @return the boolean representation of the number.
77    */

78   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
79     throws XPathException
80   {
81     double value = evalNumber(node, env);
82
83     return value != 0.0 && ! Double.isNaN(value);
84   }
85
86   /**
87    * Evaluates the expression as a string.
88    *
89    * @param node the current node
90    * @param env the variable environment.
91    *
92    * @return the string representation of the number.
93    */

94   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
95     throws XPathException
96   {
97     double value = evalNumber(node, env);
98
99     if ((int) value == value)
100       return String.valueOf((int) value);
101     else
102       return String.valueOf(value);
103   }
104
105   /**
106    * Evaluates the expression as an object.
107    *
108    * @param node the current node
109    * @param env the variable environment.
110    *
111    * @return the Double representation of the number.
112    */

113   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
114     throws XPathException
115   {
116     return new Double JavaDoc(evalNumber(node, env));
117   }
118 }
119
Popular Tags