View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.document;
13  import java.util.EnumSet;
14  import java.util.Iterator;
15  import java.util.NoSuchElementException;
16  import org.w3c.dom.Node;
17  import sk.baka.ikslibs.levelmapper.IdDomMapper;
18  import sk.baka.ikslibs.ptr.DOMPoint;
19  import sk.baka.ikslibs.ptr.DomPointer;
20  import sk.baka.ikslibs.ptr.DomPointerFactory;
21  import sk.baka.ikslibs.ptr.DomPointerFlag;
22  /***
23   * Iterates over the content nodes (comment, pi, text and element nodes). Goes
24   * through entity reference nodes. Iterator does not iterate through contents of
25   * other elements. Class is not intended to be subclassed nor instantiated by
26   * clients.
27   * @author Martin Vysny
28   * @deprecated use STAX
29   */
30  @Deprecated
31  public final class ContentNodesIterator implements Iterator<Node> {
32  	/***
33  	 * The node where the iteration shall occur.
34  	 */
35  	public final Node node;
36  	/***
37  	 * Constructs the instance.
38  	 * @param node children of the node are to be iterated
39  	 */
40  	public ContentNodesIterator(final Node node) {
41  		super();
42  		this.node = node;
43  		DomPointer start = DomPointerFactory.create(node, DOMPoint.FIRST);
44  		next = start.getNext(EnumSet.of(DomPointerFlag.NotEntityReference,
45  				DomPointerFlag.PointToNode), true, true, true);
46  	}
47  	/***
48  	 * This pointer shall be returned as a next. If <code>null</code> then no
49  	 * further nodes are available.
50  	 */
51  	private DomPointer next;
52  	/*
53  	 * (non-Javadoc)
54  	 * @see java.util.Iterator#hasNext()
55  	 */
56  	public boolean hasNext() {
57  		return next != null;
58  	}
59  	/*
60  	 * (non-Javadoc)
61  	 * @see java.util.Iterator#next()
62  	 */
63  	public Node next() {
64  		if (next == null)
65  			throw new NoSuchElementException();
66  		Node result = next.pointsTo;
67  		next = next.getNext(EnumSet.of(DomPointerFlag.NotEntityReference,
68  				DomPointerFlag.PointToNode), false, true, true);
69  		lastResult = result;
70  		return result;
71  	}
72  	/*
73  	 * (non-Javadoc)
74  	 * @see java.util.Iterator#remove()
75  	 */
76  	public void remove() {
77  		throw new UnsupportedOperationException("Removal is not supported."); //$NON-NLS-1$
78  	}
79  	/***
80  	 * Node returned by the last call to <code>next()</code> function.
81  	 */
82  	private Node lastResult = null;
83  	/***
84  	 * Skips all nodes belonging to same ID-level node as node returned by the
85  	 * last call to <code>next()</code> function. Next <code>next()</code>
86  	 * function call shall return node from next ID-level node.
87  	 */
88  	public void moveToNextIdNode() {
89  		if (next == null)
90  			throw new NoSuchElementException();
91  		Node nextIdNode = IdDomMapper.getNextIdNode(lastResult);
92  		if (nextIdNode == null) {
93  			next = null;
94  			return;
95  		}
96  		next = DomPointerFactory.create(nextIdNode);
97  	}
98  	/***
99  	 * Sets the node that shall be returned by the next call to the
100 	 * <code>next()</code> function.
101 	 * @param node the node to set. If <code>null</code> then the iterator had
102 	 * walked over last item and <code>next()</code> call shall fail
103 	 */
104 	public void setNext(Node node) {
105 		if (node == null) {
106 			next = null;
107 			return;
108 		}
109 		DomPointer start = DomPointerFactory.create(node);
110 		next = start.getNext(EnumSet.of(DomPointerFlag.NotEntityReference,
111 				DomPointerFlag.PointToNode), true, true, true);
112 	}
113 }