KickJava   Java API By Example, From Geeks To Geeks.

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


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.NodeListImpl;
32 import com.caucho.xml.XmlUtil;
33 import com.caucho.xpath.Expr;
34 import com.caucho.xpath.ExprEnvironment;
35 import com.caucho.xpath.XPathException;
36 import com.caucho.xpath.pattern.*;
37
38 import org.w3c.dom.Node JavaDoc;
39
40 public class NodeSetExpr extends Expr {
41   private AbstractPattern _pattern;
42
43   NodeSetExpr(AbstractPattern pattern)
44   {
45     _pattern = pattern;
46   }
47
48   /**
49    * Creates an expr, handling some special cases.
50    */

51   public static Expr create(AbstractPattern pattern)
52   {
53     if (pattern instanceof NodeTypePattern &&
54         pattern.getParent() instanceof FromSelf &&
55         pattern.toString().equals("."))
56       return new ObjectExpr(SELF, ".");
57     else if (pattern instanceof FromContext &&
58              ((FromContext) pattern).getCount() == 0 &&
59              pattern.getParent() == null)
60       return new ObjectExpr(SELF, ".");
61     else if (pattern instanceof NodePattern &&
62              pattern.getParent() instanceof FromAttributes &&
63              pattern.getParent().getParent() instanceof FromContext &&
64              ((FromContext) pattern.getParent().getParent()).getCount() == 0)
65       return new ObjectExpr(ATTRIBUTE, ((NodePattern) pattern).getNodeName());
66     else
67       return new NodeSetExpr(pattern);
68   }
69
70   /**
71    * Returns the underlying pattern.
72    */

73   public AbstractPattern getPattern()
74   {
75     return _pattern;
76   }
77
78   /**
79    * NodeSetExprs prefer to be node sets.
80    */

81   public boolean isNodeSet()
82   {
83     return true;
84   }
85
86   /**
87    * Returns the value of the expression as a number.
88    *
89    * @param node the current node
90    * @param env the variable environment.
91    */

92   public double evalNumber(Node JavaDoc node, ExprEnvironment env)
93     throws XPathException
94   {
95     Node JavaDoc value = _pattern.findAny(node, env);
96
97     if (value == null)
98       return Double.NaN;
99
100     String JavaDoc string = XmlUtil.textValue(value);
101
102     return stringToNumber(string);
103   }
104
105   /**
106    * Returns true if there are any patterns matching the pattern.
107    *
108    * @param node the current node
109    * @param env the variable environment.
110    */

111   public boolean evalBoolean(Node JavaDoc node, ExprEnvironment env)
112     throws XPathException
113   {
114     return _pattern.findAny(node, env) != null;
115   }
116
117   /**
118    * Returns the value of the node set expression as a string.
119    * The value is the text value of the first node.
120    *
121    * @param node the current node
122    * @param env the variable environment
123    *
124    * @return the combined text value of the node.
125    */

126   public String JavaDoc evalString(Node JavaDoc node, ExprEnvironment env)
127     throws XPathException
128   {
129     Node JavaDoc value = _pattern.findAny(node, env);
130
131     if (value == null)
132       return "";
133     else
134       return XmlUtil.textValue(value);
135   }
136
137   /**
138    * Evaluate a node-set object, returning an ArrayList of the node set.
139    *
140    * @param node the current node
141    * @param env the variable environment
142    *
143    * @return an array list of the nodes
144    */

145   public Object JavaDoc evalObject(Node JavaDoc node, ExprEnvironment env)
146     throws XPathException
147   {
148     NodeListImpl list = new NodeListImpl();
149
150     NodeIterator iter = _pattern.select(node, env);
151     Node JavaDoc value = null;
152     while ((value = iter.nextNode()) != null)
153       list.add(value);
154
155     return list;
156   }
157
158   /**
159    * Evaluate a node-set object, returning an iterator of the node set.
160    *
161    * @param node the current node
162    * @param env the variable environment
163    *
164    * @return an iterator of the nodes
165    */

166   public NodeIterator evalNodeSet(Node JavaDoc node, ExprEnvironment env)
167     throws XPathException
168   {
169     return _pattern.select(node, env);
170   }
171   
172   /**
173    * Convert from an expression to a pattern.
174    */

175   protected AbstractPattern toNodeList()
176   {
177     return _pattern;
178   }
179
180   public boolean equals(Object JavaDoc b)
181   {
182     if (! (b instanceof NodeSetExpr))
183       return false;
184
185     return _pattern.equals(((NodeSetExpr) b)._pattern);
186   }
187
188   public String JavaDoc toString()
189   {
190     return _pattern.toString();
191   }
192 }
193
Popular Tags