1:
37: package ;
38:
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44:
45:
50: public class DomNodeIterator
51: implements NodeIterator, TreeWalker
52: {
53:
54: Node root;
55: final int whatToShow;
56: final NodeFilter filter;
57: final boolean entityReferenceExpansion;
58: final boolean walk;
59: Node current;
60:
61: public DomNodeIterator(Node root, int whatToShow, NodeFilter filter,
62: boolean entityReferenceExpansion, boolean walk)
63: {
64: if (root == null)
65: {
66: throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
67: }
68: this.root = root;
69: this.whatToShow = whatToShow;
70: this.filter = filter;
71: this.entityReferenceExpansion = entityReferenceExpansion;
72: this.walk = walk;
73: current = root;
74: }
75:
76: public Node getRoot()
77: {
78: return root;
79: }
80:
81: public int getWhatToShow()
82: {
83: return whatToShow;
84: }
85:
86: public NodeFilter getFilter()
87: {
88: return filter;
89: }
90:
91: public boolean getExpandEntityReferences()
92: {
93: return entityReferenceExpansion;
94: }
95:
96: public Node nextNode()
97: throws DOMException
98: {
99: if (root == null)
100: {
101: throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
102: }
103: Node ret;
104: do
105: {
106: if (current.equals(root))
107: {
108: ret = root.getFirstChild();
109: }
110: else if (walk)
111: {
112: ret = current.getFirstChild();
113: if (ret == null)
114: {
115: ret = current.getNextSibling();
116: }
117: if (ret == null)
118: {
119: Node tmp = current;
120: ret = tmp.getParentNode();
121: while (!ret.equals(root) && tmp.equals(ret.getLastChild()))
122: {
123: tmp = ret;
124: ret = tmp.getParentNode();
125: }
126: if (ret.equals(root))
127: {
128: ret = null;
129: }
130: else
131: {
132: ret = ret.getNextSibling();
133: }
134: }
135: }
136: else
137: {
138: ret = current.getNextSibling();
139: }
140: current = (ret == null) ? current : ret;
141: }
142: while (!accept(ret));
143:
144: return ret;
145: }
146:
147: public Node previousNode()
148: throws DOMException
149: {
150: if (root == null)
151: {
152: throw new DOMException(DOMException.INVALID_STATE_ERR, "null root");
153: }
154: Node ret;
155: do
156: {
157: if (current.equals(root))
158: {
159: ret = current.getLastChild();
160: }
161: else if (walk)
162: {
163: ret = current.getLastChild();
164: if (ret == null)
165: {
166: ret = current.getPreviousSibling();
167: }
168: if (ret == null)
169: {
170: Node tmp = current;
171: ret = tmp.getParentNode();
172: while (!ret.equals(root) && tmp.equals(ret.getFirstChild()))
173: {
174: tmp = ret;
175: ret = tmp.getParentNode();
176: }
177: if (ret.equals(root))
178: {
179: ret = null;
180: }
181: else
182: {
183: ret = ret.getPreviousSibling();
184: }
185: }
186: }
187: else
188: {
189: ret = current.getPreviousSibling();
190: }
191: }
192: while (!accept(ret));
193: current = (ret == null) ? current : ret;
194: return ret;
195: }
196:
197: public Node getCurrentNode()
198: {
199: return current;
200: }
201:
202: public void setCurrentNode(Node current)
203: throws DOMException
204: {
205: if (current == null)
206: {
207: throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "null root");
208: }
209: this.current = current;
210: }
211:
212: public Node parentNode()
213: {
214: Node ret = current.getParentNode();
215: if (!accept (ret))
216: {
217: ret = null;
218: }
219: current = (ret == null) ? current : ret;
220: return ret;
221: }
222:
223: public Node firstChild ()
224: {
225: Node ret = current.getFirstChild();
226: while (!accept(ret))
227: {
228: ret = ret.getNextSibling();
229: }
230: current = (ret == null) ? current : ret;
231: return ret;
232: }
233:
234: public Node lastChild()
235: {
236: Node ret = current.getLastChild();
237: while (!accept(ret))
238: {
239: ret = ret.getPreviousSibling();
240: }
241: current = (ret == null) ? current : ret;
242: return ret;
243: }
244:
245: public Node previousSibling()
246: {
247: Node ret = current.getPreviousSibling();
248: while (!accept(ret))
249: {
250: ret = ret.getPreviousSibling();
251: }
252: current = (ret == null) ? current : ret;
253: return ret;
254: }
255:
256: public Node nextSibling()
257: {
258: Node ret = current.getNextSibling();
259: while (!accept(ret))
260: {
261: ret = ret.getNextSibling();
262: }
263: current = (ret == null) ? current : ret;
264: return ret;
265: }
266:
267: public void detach()
268: {
269: root = null;
270: }
271:
272: boolean accept(Node node)
273: {
274: if (node == null)
275: {
276: return true;
277: }
278: boolean ret;
279: switch (node.getNodeType())
280: {
281: case Node.ATTRIBUTE_NODE:
282: ret = (whatToShow & NodeFilter.SHOW_ATTRIBUTE) != 0;
283: break;
284: case Node.CDATA_SECTION_NODE:
285: ret = (whatToShow & NodeFilter.SHOW_CDATA_SECTION) != 0;
286: break;
287: case Node.COMMENT_NODE:
288: ret = (whatToShow & NodeFilter.SHOW_COMMENT) != 0;
289: break;
290: case Node.DOCUMENT_NODE:
291: ret = (whatToShow & NodeFilter.SHOW_DOCUMENT) != 0;
292: break;
293: case Node.DOCUMENT_FRAGMENT_NODE:
294: ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT) != 0;
295: break;
296: case Node.DOCUMENT_TYPE_NODE:
297: ret = (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE) != 0;
298: break;
299: case Node.ELEMENT_NODE:
300: ret = (whatToShow & NodeFilter.SHOW_ELEMENT) != 0;
301: break;
302: case Node.ENTITY_NODE:
303: ret = (whatToShow & NodeFilter.SHOW_ENTITY) != 0;
304: break;
305: case Node.ENTITY_REFERENCE_NODE:
306: ret = (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE) != 0;
307: ret = ret && entityReferenceExpansion;
308: break;
309: case Node.NOTATION_NODE:
310: ret = (whatToShow & NodeFilter.SHOW_NOTATION) != 0;
311: break;
312: case Node.PROCESSING_INSTRUCTION_NODE:
313: ret = (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION) != 0;
314: break;
315: case Node.TEXT_NODE:
316: ret = (whatToShow & NodeFilter.SHOW_TEXT) != 0;
317: break;
318: default:
319: ret = true;
320: }
321: if (ret && filter != null)
322: {
323: ret = (filter.acceptNode(node) == NodeFilter.FILTER_ACCEPT);
324: }
325: return ret;
326: }
327:
328: }