2 * sibling element iterator on DOM tree
4 * License : The MIT License
5 * Copyright(c) 2011 MikuToga Partners
8 package jp.sourceforge.mikutoga.xml;
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
17 * <p>同じ親と名前空間とローカル名を持つ要素同士を「兄弟要素」とする。
18 * <p>ノードの持つ名前空間がnullの場合、全ての名前空間引数にマッチする。
21 public class SiblingElemIterator implements Iterator<Element> {
24 private final String nsuri;
25 private final String localName;
30 * @param first 最初の兄弟要素。nullだと一度もiterateしない。
32 public SiblingElemIterator(Element first){
37 if(this.next == null){
39 this.localName = null;
41 this.nsuri = this.next.getNamespaceURI();
42 this.localName = this.next.getLocalName();
50 * <p>名前空間引数にnullが渡された場合、全ての名前空間にマッチする。
51 * <p>ローカル名引数にnullが渡された場合、全てのローカル名にマッチする。
53 * @param nsuri 子要素の名前空間URI
54 * @param localName 子要素のローカル名
56 public SiblingElemIterator(Element parent,
61 this.next = DomNsUtils.pickFirstChild(parent, nsuri, localName);
63 if(this.next == null){
65 this.localName = null;
68 this.localName = localName;
77 * @return {@inheritDoc}
80 public boolean hasNext(){
81 if(this.next != null) return true;
87 * @return {@inheritDoc}
88 * @throws NoSuchElementException {@inheritDoc}
91 public Element next() throws NoSuchElementException {
92 if(this.next == null) throw new NoSuchElementException();
94 Element result = this.next;
96 Node sibNode = result;
98 sibNode = sibNode.getNextSibling();
99 if(sibNode == null) break;
100 }while( ! matchElemName(sibNode) );
101 this.next = (Element) sibNode;
107 * 兄弟要素にふさわしい名前を持つか判定する。
109 * @return 兄弟にふさわしい名前を持つならtrue
111 private boolean matchElemName(Node node){
112 return DomNsUtils.hasNsLocalNameElem(node,
113 this.nsuri, this.localName );
119 * @throws UnsupportedOperationException 削除を試みたので失敗した
122 public void remove() throws UnsupportedOperationException {
123 throw new UnsupportedOperationException();