2
* xslthl - XSLT Syntax Highlighting
3
* https://sourceforge.net/projects/xslthl/
4
* Copyright (C) 2005-2009 Michal Molhanec, Jirka Kosek, Michiel Hendriks
6
* This software is provided 'as-is', without any express or implied
7
* warranty. In no event will the authors be held liable for any damages
8
* arising from the use of this software.
10
* Permission is granted to anyone to use this software for any purpose,
11
* including commercial applications, and to alter it and redistribute it
12
* freely, subject to the following restrictions:
14
* 1. The origin of this software must not be misrepresented; you must not
15
* claim that you wrote the original software. If you use this software
16
* in a product, an acknowledgment in the product documentation would be
17
* appreciated but is not required.
18
* 2. Altered source versions must be plainly marked as such, and must not be
19
* misrepresented as being the original software.
20
* 3. This notice may not be removed or altered from any source distribution.
22
* Michal Molhanec <mol1111 at users.sourceforge.net>
23
* Jirka Kosek <kosek at users.sourceforge.net>
24
* Michiel Hendriks <elmuerte at users.sourceforge.net>
26
package net.sf.xslthl;
28
import java.util.HashSet;
29
import java.util.Iterator;
30
import java.util.NoSuchElementException;
33
import org.w3c.dom.Element;
34
import org.w3c.dom.Node;
37
* An iterator over the child elements of a given element, filtered by a set of
40
* @author Michiel Hendriks
42
public class FilteredElementIterator implements Iterator<Element> {
45
* Defines a filter used by this iterator
47
* @author Michiel Hendriks
49
public interface Acceptor {
51
* Accept or reject an element
55
* @return True if the element is accepted
57
boolean accepted(Element elm);
61
* Acceptor that accepts elements based on the tag name
63
public static class TagNameAcceptor implements Acceptor {
64
protected Set<String> names;
66
public TagNameAcceptor(Set<String> acceptNames) {
67
if (acceptNames == null) {
68
throw new NullPointerException("Set can not be null");
70
names = new HashSet<String>(acceptNames);
77
* net.sf.xslthl.FilteredElementIterator.Acceptor#accepted(org.w3c.dom
80
public boolean accepted(Element elm) {
81
return names.contains(elm.getTagName());
89
protected Element base;
94
protected Element current;
97
* The used acceptor for filtering
99
protected Acceptor acceptor;
102
* If true a new value should be retrieved
104
protected boolean dirty = true;
107
* Will be true if the search reached an end
109
protected boolean finished;
111
public FilteredElementIterator(Element baseElement, Acceptor filter) {
112
if (baseElement == null) {
113
throw new NullPointerException("Base Element can not be null");
115
if (filter == null) {
116
throw new NullPointerException("Acceptor can not be null");
124
* Creates a filtered element iterator that accepts elements with a given
128
* @param acceptTagNames
130
public FilteredElementIterator(Element baseElement,
131
Set<String> acceptTagNames) {
132
this(baseElement, new TagNameAcceptor(acceptTagNames));
138
* @see java.util.Iterator#hasNext()
140
public boolean hasNext() {
141
if (!finished && dirty) {
142
// not finished, and the current value is dirty
145
return current != null;
151
* @see java.util.Iterator#next()
153
public Element next() {
154
if (!finished && dirty) {
155
// if dirty first get a new value
160
throw new NoSuchElementException();
169
* @see java.util.Iterator#remove()
171
public void remove() {
172
throw new UnsupportedOperationException("Elements can not be removed");
176
* Get the next valid element
178
protected void getNextElement() {
179
assert !finished : "Should not call getNextElements on a finished list";
180
if (current == null) {
181
current = getAcceptedElement(base.getFirstChild());
183
current = getAcceptedElement(current.getNextSibling());
185
// iteration is finished when the current element is null
186
finished = current == null;
191
* Get the next accepted element. Will continue searching for the next
195
* The first node to test
196
* @return The accepted element, or null
198
protected Element getAcceptedElement(Node node) {
199
while (node != null) {
200
if (node.getNodeType() == Node.ELEMENT_NODE
201
&& node instanceof Element) {
202
Element result = (Element) node;
203
if (acceptor.accepted(result)) {
207
node = node.getNextSibling();