KickJava   Java API By Example, From Geeks To Geeks.

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


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.pattern.NodeIterator;
36 import com.caucho.xpath.pattern.SingleNodeIterator;
37
38 import org.w3c.dom.Node JavaDoc;
39
40 public class VarExpr extends Expr {
41   private String JavaDoc name;
42
43   public VarExpr(String JavaDoc name)
44   {
45     this.name = name.intern();
46   }
47
48   /**
49    * Returns the value of the variable as a boolean.
50    *
51    * @param node the current node
52    * @param env the XPath envivonment
53    *
54    * @return the boolean value
55    */

56   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
57     throws XPathException
58   {
59     Var var = (Var) env.getVar(name);
60
61     return var == null ? false : var.getBoolean();
62   }
63
64   /**
65    * Returns the value of the variable as a double.
66    *
67    * @param node the current node
68    * @param env the XPath envivonment
69    *
70    * @return the double value
71    */

72   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
73     throws XPathException
74   {
75     Var var = env.getVar(name);
76
77     return var == null ? Double.NaN : var.getDouble();
78   }
79
80   /**
81    * Returns the value of the variable as a string
82    *
83    * @param cb the buffer to append the value
84    * @param node the current node
85    * @param env the XPath envivonment
86    *
87    * @return the string value
88    */

89   public void evalString(CharBuffer cb, Node JavaDoc node, ExprEnvironment env)
90     throws XPathException
91   {
92     Var var = env.getVar(name);
93
94     if (var != null)
95       var.getString(cb);
96   }
97
98   /**
99    * Returns the value of the variable as a string
100    *
101    * @param env the XPath envivonment
102    * @param node the current node
103    *
104    * @return the string value
105    */

106   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
107     throws XPathException
108   {
109     Var var = env.getVar(name);
110
111     return var == null ? "" : var.getString();
112   }
113
114   /**
115    * Returns the value of the variable as an object
116    *
117    * @param env the XPath envivonment
118    * @param node the current node
119    *
120    * @return the value
121    */

122   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
123     throws XPathException
124   {
125     Var var = env.getVar(name);
126
127     return var == null ? null : var.getObject();
128   }
129
130   /**
131    * Returns the value of the variable as an variable
132    *
133    * @param node the current node
134    * @param env the XPath envivonment
135    *
136    * @return the value
137    */

138   public Var evalVar(Node JavaDoc node, ExprEnvironment env)
139     throws XPathException
140   {
141     Var var = env.getVar(name);
142
143     return var;
144   }
145
146   /**
147    * Returns the value of the variable as a node set.
148    *
149    * @param node the current node
150    * @param env the variable envivonment
151    *
152    * @return the value
153    */

154   public NodeIterator evalNodeSet(Node JavaDoc node, ExprEnvironment env)
155     throws XPathException
156   {
157     Var var = env.getVar(name);
158
159     if (var == null)
160       return new SingleNodeIterator(env, null);
161     else
162       return var.getNodeSet(env);
163   }
164
165   public String JavaDoc toString()
166   {
167     return "$" + name;
168   }
169 }
170
Popular Tags