1 package org.jaxen.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 import org.jaxen.Navigator;
52 import org.jaxen.UnsupportedAxisException;
53 import org.jaxen.JaxenRuntimeException;
54
55 import java.util.Iterator;
56 import java.util.NoSuchElementException;
57 import java.util.ArrayList;
58
59 /***
60 * Represents the XPath <code>descendant</code> axis.
61 * The "<code>descendant</code> axis contains the descendants of the context node;
62 * a descendant is a child or a child of a child and so on; thus
63 * the descendant axis never contains attribute or namespace nodes."
64 *
65 * @version 1.2b12
66 */
67 public class DescendantAxisIterator implements Iterator
68 {
69
70 private ArrayList stack = new ArrayList();
71 private Iterator children;
72 private Navigator navigator;
73
74 /***
75 * Create a new <code>descendant</code> axis iterator.
76 *
77 * @param contextNode the node to start from
78 * @param navigator the object model specific navigator
79 */
80 public DescendantAxisIterator(Object contextNode,
81 Navigator navigator) throws UnsupportedAxisException
82 {
83 this(navigator, navigator.getChildAxisIterator(contextNode));
84 }
85
86 public DescendantAxisIterator(Navigator navigator,
87 Iterator iterator)
88 {
89 this.navigator = navigator;
90 this.children = iterator;
91 }
92
93 /***
94 * Returns true if there are any descendants remaining; false otherwise.
95 *
96 * @return true if any descendants remain; false otherwise
97 *
98 * @see java.util.Iterator#hasNext()
99 */ public boolean hasNext()
100 {
101 while (!children.hasNext())
102 {
103 if (stack.isEmpty())
104 {
105 return false;
106 }
107 children = (Iterator) stack.remove(stack.size()-1);
108 }
109 return true;
110 }
111
112 /***
113 * Returns the next descendant node.
114 *
115 * @return the next descendant node
116 *
117 * @throws NoSuchElementException if no descendants remain
118 *
119 * @see java.util.Iterator#next()
120 */
121 public Object next()
122 {
123 try
124 {
125 if (hasNext())
126 {
127 Object node = children.next();
128 stack.add(children);
129 children = navigator.getChildAxisIterator(node);
130 return node;
131 }
132 throw new NoSuchElementException();
133 }
134 catch (UnsupportedAxisException e)
135 {
136 throw new JaxenRuntimeException(e);
137 }
138 }
139
140 /***
141 * This operation is not supported.
142 *
143 * @throws UnsupportedOperationException always
144 */
145 public void remove()
146 {
147 throw new UnsupportedOperationException();
148 }
149
150 }