KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.XmlUtil;
32 import com.caucho.xpath.Expr;
33 import com.caucho.xpath.ExprEnvironment;
34 import com.caucho.xpath.XPathException;
35 import com.caucho.xpath.pattern.*;
36
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39
40 import java.util.ArrayList JavaDoc;
41
42 public class ObjectExpr extends Expr {
43   private int _code;
44   private String JavaDoc _name;
45   private Expr _left;
46   private Expr _right;
47   private Expr _third;
48
49   public ObjectExpr(int code, ArrayList JavaDoc args)
50   {
51     _code = code;
52
53     if (args != null && args.size() > 0)
54       _left = (Expr) args.get(0);
55     if (args != null && args.size() > 1)
56       _right = (Expr) args.get(1);
57     if (args != null && args.size() > 2)
58       _third = (Expr) args.get(2);
59
60     if (_right == null || _third == null)
61       throw new NullPointerException JavaDoc();
62   }
63
64   public ObjectExpr(int code, String JavaDoc name)
65   {
66     _code = code;
67     _name = name;
68   }
69
70   /**
71    * Returns true if the expression evaluates to a node-set.
72    */

73   public boolean isNodeSet()
74   {
75     return _code == SELF;
76   }
77
78   /**
79    * Returns true if the expression evaluates to a node-set.
80    */

81   public boolean isString()
82   {
83     return _code == ATTRIBUTE;
84   }
85
86   /**
87    * Evaluates the expression as a boolean.
88    *
89    * @param node current node
90    * @param env the environment
91    *
92    * @return the boolean value
93    */

94   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
95     throws XPathException
96   {
97     switch (_code) {
98     case IF:
99       if (_left.evalBoolean(node, env))
100     return _right.evalBoolean(node, env);
101       else
102     return _third.evalBoolean(node, env);
103
104     case ATTRIBUTE:
105       if (node instanceof Element JavaDoc)
106         return ! ((Element JavaDoc) node).getAttribute(_name).equals("");
107       else
108         return false;
109       
110     case SELF:
111       return true;
112
113     default:
114       return toBoolean(evalObject(node, env));
115     }
116   }
117
118   /**
119    * Evaluates the expression as number.
120    *
121    * @param node current node
122    * @param env the environment
123    */

124   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
125     throws XPathException
126   {
127     switch (_code) {
128     case IF:
129       if (_left.evalBoolean(node, env))
130     return _right.evalNumber(node, env);
131       else
132     return _third.evalNumber(node, env);
133
134     case ATTRIBUTE:
135       if (node instanceof Element JavaDoc)
136         return toDouble(((Element JavaDoc) node).getAttribute(_name));
137       else
138         return Double.NaN;
139       
140     case SELF:
141       return toDouble(XmlUtil.textValue(node));
142       
143     default:
144       return toDouble(evalObject(node, env));
145     }
146   }
147
148   /**
149    * Evaluates the expression as string.
150    *
151    * @param node current node
152    * @param env the environment
153    *
154    * @return the string representation
155    */

156   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
157     throws XPathException
158   {
159     switch (_code) {
160     case IF:
161       if (_left.evalBoolean(node, env))
162     return _right.evalString(node, env);
163       else
164     return _third.evalString(node, env);
165       
166     case ATTRIBUTE:
167       if (node instanceof Element JavaDoc)
168         return ((Element JavaDoc) node).getAttribute(_name);
169       else
170         return "";
171
172     case SELF:
173       return XmlUtil.textValue(node);
174       
175     default:
176       return toString(evalObject(node, env));
177     }
178   }
179
180   /**
181    * Evaluates the expression as an object.
182    *
183    * @param node current node
184    * @param env the environment
185    *
186    * @return the object representation
187    */

188   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
189     throws XPathException
190   {
191     switch (_code) {
192     case IF:
193       if (_left.evalBoolean(node, env))
194     return _right.evalObject(node, env);
195       else
196     return _third.evalObject(node, env);
197
198     case SELF:
199       return node;
200       
201     case ATTRIBUTE:
202       if (node instanceof Element JavaDoc)
203         return ((Element JavaDoc) node).getAttributeNode(_name);
204       else
205         return null;
206       
207     default:
208       return null;
209     }
210   }
211
212   /**
213    * Evaluates the expression as a node set.
214    *
215    * @param node current node
216    * @param env the variable environment
217    */

218   public NodeIterator evalNodeSet(Node JavaDoc node, ExprEnvironment env)
219     throws XPathException
220   {
221     switch (_code) {
222     case IF:
223       if (_left.evalBoolean(node, env))
224     return _right.evalNodeSet(node, env);
225       else
226     return _third.evalNodeSet(node, env);
227
228     case SELF:
229       return new SingleNodeIterator(env, node);
230
231     case ATTRIBUTE:
232       if (node instanceof Element JavaDoc)
233         return new SingleNodeIterator(env, ((Element JavaDoc) node).getAttributeNode(_name));
234       else
235         return new SingleNodeIterator(env, null);
236       
237     default:
238       return null;
239     }
240   }
241   
242   /**
243    * Convert from an expression to a pattern.
244    */

245   protected AbstractPattern toNodeList()
246   {
247     switch (_code) {
248     case SELF:
249       return NodeTypePattern.create(new FromSelf(null), NodeTypePattern.ANY);
250
251     case ATTRIBUTE:
252       return new NodePattern(new FromAttributes(null),
253                              _name, Node.ATTRIBUTE_NODE);
254
255     default:
256       return super.toNodeList();
257     }
258   }
259
260   public String JavaDoc toString()
261   {
262     switch (_code) {
263     case IF:
264       return "if(" + _left + "," + _right + "," + _third + ")";
265
266     case SELF:
267       return ".";
268
269     case ATTRIBUTE:
270       return "@" + _name;
271
272     default:
273       return super.toString();
274     }
275   }
276 }
277
Popular Tags