KickJava   Java API By Example, From Geeks To Geeks.

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


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.xpath.ExprEnvironment;
32 import com.caucho.xpath.XPathException;
33
34 import org.w3c.dom.Node JavaDoc;
35
36 import java.util.logging.Level JavaDoc;
37
38 /**
39  * Uses the axis to select new nodes.
40  */

41 public class AxisIterator extends NodeIterator {
42   protected NodeIterator _parentIter;
43   protected AbstractPattern _axis;
44   protected Node JavaDoc _node;
45   protected Node JavaDoc _next;
46   protected Node JavaDoc _lastNode;
47   protected AbstractPattern _match;
48   
49   protected AxisIterator(ExprEnvironment env)
50   {
51     super(env);
52   }
53   
54   /**
55    * Creates the new AxisIterator.
56    *
57    * @param parentIter the parent iterator
58    * @param axis the owning axis
59    * @param node the first node
60    * @param env the variable environment
61    * @param match the node matching pattern
62    */

63   public AxisIterator(NodeIterator parentIter, AbstractPattern axis,
64                       Node JavaDoc node, ExprEnvironment env,
65                       AbstractPattern match)
66     throws XPathException
67   {
68     super(env);
69     
70     _parentIter = parentIter;
71     _axis = axis;
72     _match = match;
73
74     if (parentIter != null && parentIter.hasNext()) {
75       node = parentIter.nextNode();
76     }
77
78     if (node != null) {
79       _lastNode = axis.lastNode(node);
80       _node = findFirstMatchingNode(axis.firstNode(node, _env));
81     }
82
83     _next = _node;
84   }
85   
86   /**
87    * True if there's more data.
88    */

89   public boolean hasNext()
90   {
91     if (_next == null) {
92       try {
93         _next = nextNode();
94       } catch (XPathException e) {
95         log.log(Level.FINE, e.toString(), e);
96       }
97     }
98     
99     return _next != null;
100   }
101   
102   /**
103    * Returns the next selected node.
104    */

105   public Node JavaDoc nextNode()
106     throws XPathException
107   {
108     if (_next != null) {
109       _node = _next;
110       _next = null;
111       
112       return _node;
113     }
114
115     if (_node != null)
116       _node = findFirstMatchingNode(_axis.nextNode(_node, _lastNode));
117     
118     _next = null;
119
120     return _node;
121   }
122
123   /**
124    * Finds the next matching node.
125    */

126   private Node JavaDoc findFirstMatchingNode(Node JavaDoc node)
127     throws XPathException
128   {
129     while (true) {
130       if (node != null) {
131         if (_match == null || _match.match(node, _env)) {
132           _position++;
133           return node;
134         }
135         else {
136           node = _axis.nextNode(node, _lastNode);
137           continue;
138         }
139       }
140       
141       else if (_parentIter == null || (node = _parentIter.nextNode()) == null)
142         return null;
143       else {
144         _position = 0;
145         _size = 0;
146         _lastNode = _axis.lastNode(node);
147         node = _axis.firstNode(node, _env);
148       }
149     }
150   }
151   
152   /**
153    * Returns the number of nodes in the context list.
154    */

155   public int getContextSize()
156   {
157     if (_size == 0) {
158       _size = _position;
159       
160       try {
161         Node JavaDoc ptr = _node == null ? null : _axis.nextNode(_node, _lastNode);
162         
163         for (; ptr != null; ptr = _axis.nextNode(ptr, _lastNode)) {
164           if (_match == null || _match.match(ptr, _env))
165             _size++;
166         }
167       } catch (XPathException e) {
168         log.log(Level.FINE, e.toString(), e);
169       }
170     }
171     
172     return _size;
173   }
174
175
176   public Object JavaDoc clone()
177   {
178     AxisIterator iter = new AxisIterator(_env);
179
180     iter.copy(this);
181
182     if (_parentIter != null)
183       iter._parentIter = (NodeIterator) _parentIter.clone();
184     
185     iter._axis = _axis;
186     iter._node = _node;
187     iter._match = _match;
188
189     return iter;
190   }
191
192   public String JavaDoc toString()
193   {
194     return "AxisIterator[axis:" + _axis + ",match:" + _match + "]";
195   }
196 }
197
Popular Tags