| 1 | |
package net.mtu.eggplant.xml; |
| 2 | |
|
| 3 | |
import java.util.Iterator; |
| 4 | |
import java.util.LinkedList; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.NoSuchElementException; |
| 7 | |
|
| 8 | |
import org.w3c.dom.Element; |
| 9 | |
import org.w3c.dom.NodeList; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | 0 | public final class NodelistElementCollectionAdapter implements Iterator<Element>, Iterable<Element> { |
| 15 | |
|
| 16 | |
public Iterator<Element> iterator() { |
| 17 | 0 | return this; |
| 18 | |
} |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public List<Element> asList() { |
| 24 | 0 | final List<Element> retval = new LinkedList<Element>(); |
| 25 | 0 | for (final Element ele : this) { |
| 26 | 0 | retval.add(ele); |
| 27 | 0 | } |
| 28 | 0 | return retval; |
| 29 | |
} |
| 30 | |
|
| 31 | |
private final NodeList nodelist; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
private int nextIndex; |
| 37 | |
|
| 38 | 2 | public NodelistElementCollectionAdapter(final NodeList nodelist) { |
| 39 | 2 | this.nodelist = nodelist; |
| 40 | |
|
| 41 | 2 | this.nextIndex = -1; |
| 42 | 2 | findNextElement(); |
| 43 | 2 | } |
| 44 | |
|
| 45 | |
public boolean hasNext() { |
| 46 | 5 | return nextIndex < nodelist.getLength(); |
| 47 | |
} |
| 48 | |
|
| 49 | |
public Element next() { |
| 50 | 2 | if (hasNext()) { |
| 51 | 2 | final Element retval = (Element) nodelist.item(nextIndex); |
| 52 | 2 | findNextElement(); |
| 53 | 2 | return retval; |
| 54 | |
} else { |
| 55 | 0 | throw new NoSuchElementException(); |
| 56 | |
} |
| 57 | |
} |
| 58 | |
|
| 59 | |
public void remove() { |
| 60 | 0 | throw new UnsupportedOperationException(); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private void findNextElement() { |
| 68 | |
do { |
| 69 | 4 | ++nextIndex; |
| 70 | 4 | } while (nextIndex < nodelist.getLength() |
| 71 | 2 | && !(nodelist.item(nextIndex) instanceof Element)); |
| 72 | 4 | } |
| 73 | |
} |