KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > NodeIterator


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.pattern;
30
31 import com.caucho.log.Log;
32 import com.caucho.xml.XmlUtil;
33 import com.caucho.xpath.ExprEnvironment;
34 import com.caucho.xpath.StylesheetEnv;
35 import com.caucho.xpath.XPathException;
36 import com.caucho.xpath.XPathFun;
37 import com.caucho.xpath.expr.Var;
38
39 import org.w3c.dom.Attr JavaDoc;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42
43 import java.util.Iterator JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Iterates through matching nodes.
49  */

50 public abstract class NodeIterator implements ExprEnvironment, Iterator JavaDoc<Node JavaDoc> {
51   protected static final Logger JavaDoc log = Log.open(NodeIterator.class);
52   
53   protected ExprEnvironment _env;
54
55   protected Node JavaDoc _contextNode;
56   protected int _position;
57   protected int _size;
58
59   protected NodeIterator(ExprEnvironment env)
60   {
61     /** XXX: children of NodeList implement iterator() for quercus
62      if (env == null)
63      throw new NullPointerException();
64      */

65     
66     _env = env;
67   }
68   
69   /**
70    * True if there's more data.
71    */

72   public abstract boolean hasNext();
73   
74   /**
75    * Returns the next node.
76    */

77   public abstract Node JavaDoc nextNode()
78     throws XPathException;
79
80   /**
81    * Iterator interface.
82    */

83   public Node JavaDoc next()
84   {
85     Node JavaDoc value = null;
86
87     try {
88       value = nextNode();
89     } catch (Exception JavaDoc e) {
90       log.log(Level.FINE, e.toString(), e);
91     }
92
93     return value;
94   }
95   
96   /**
97    * Returns the next selected node.
98    */

99   public SelectedNode nextSelectedNode()
100     throws XPathException
101   {
102     Node JavaDoc node = nextNode();
103
104     if (node == null)
105       return null;
106     else if (node instanceof Attr JavaDoc)
107       return new SelectedAttribute(node);
108     else
109       return new SelectedNode(node);
110   }
111
112   /**
113    * Sets the current node.
114    */

115   public Node JavaDoc getCurrentNode()
116   {
117     return _env.getCurrentNode();
118   }
119
120   /**
121    * Gets the env node.
122    */

123   public Node JavaDoc getContextNode()
124   {
125     if (_contextNode != null)
126       return _contextNode;
127     else
128       return _env.getContextNode();
129   }
130
131   /**
132    * Sets the env node.
133    */

134   public Node JavaDoc setContextNode(Node JavaDoc node)
135   {
136     Node JavaDoc oldNode = _contextNode;
137     _contextNode = node;
138     return oldNode;
139   }
140
141   /**
142    * Returns the position of the context node.
143    */

144   public int getContextPosition()
145   {
146     return _position;
147   }
148
149   /**
150    * Returns the number of nodes in the context list.
151    */

152   public int getContextSize()
153   {
154     if (_size == 0) {
155       _size = _position;
156
157       NodeIterator clone = (NodeIterator) clone();
158       try {
159         while (clone != null && clone.nextNode() != null)
160           _size++;
161       } catch (XPathException e) {
162       }
163     }
164     
165     return _size;
166   }
167
168   /**
169    * Returns a document for creating nodes.
170    */

171   public Document JavaDoc getOwnerDocument()
172   {
173     return _env.getOwnerDocument();
174   }
175
176   /**
177    * Returns the given variable
178    */

179   public Var getVar(String JavaDoc name)
180   {
181     return _env.getVar(name);
182   }
183
184   /**
185    * Returns the given variable
186    */

187   public XPathFun getFunction(String JavaDoc name)
188   {
189     return _env.getFunction(name);
190   }
191   /**
192    * Returns the stylesheet environment.
193    */

194   public StylesheetEnv getStylesheetEnv()
195   {
196     return _env.getStylesheetEnv();
197   }
198
199   /**
200    * Returns the given system property.
201    */

202   public Object JavaDoc systemProperty(String JavaDoc namespaceURI, String JavaDoc localName)
203   {
204     return _env.getOwnerDocument();
205   }
206
207   /**
208    * Returns the string-value of the ndoe.
209    */

210   public String JavaDoc stringValue(Node JavaDoc node)
211   {
212     return XmlUtil.textValue(node);
213   }
214
215   /**
216    * Returns the position index count.
217    */

218   public int getPositionIndex()
219   {
220     return 0;
221   }
222   
223   /**
224    * Set true if should test more positions.
225    */

226   public void setMorePositions(boolean more)
227   {
228   }
229
230   /**
231    * clones the iterator
232    */

233   public abstract Object JavaDoc clone();
234
235   /**
236    * copies the iterator.
237    */

238   public void copy(NodeIterator src)
239   {
240     _env = src._env;
241     _position = src._position;
242     _size = src._size;
243   }
244   
245   /**
246    * remove is unsupported
247    */

248   public void remove()
249     throws UnsupportedOperationException JavaDoc
250   {
251     throw new UnsupportedOperationException JavaDoc();
252   }
253 }
254
Popular Tags