2 * sibling element iterator on DOM tree
4 * License : The MIT License
5 * Copyright(c) 2011 MikuToga Partners
8 package jp.sfjp.mikutoga.xml;
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
18 * <p>同じ親と名前空間とローカル名を持つ要素同士を「兄弟要素」とする。
20 * <p>ノードの持つ名前空間がnullの場合、全ての名前空間引数にマッチする。
24 public class SiblingElemIterator implements Iterator<Element> {
27 private final String nsuri;
28 private final String localName;
33 * @param first 最初の兄弟要素。nullだと一度もiterateしない。
35 public SiblingElemIterator(Element first){
40 if(this.next == null){
42 this.localName = null;
44 this.nsuri = this.next.getNamespaceURI();
45 this.localName = this.next.getLocalName();
54 * <p>名前空間引数にnullが渡された場合、全ての名前空間にマッチする。
56 * <p>ローカル名引数にnullが渡された場合、全てのローカル名にマッチする。
59 * @param nsuri 子要素の名前空間URI
60 * @param localName 子要素のローカル名
62 public SiblingElemIterator(Element parent,
67 this.next = DomNsUtils.pickFirstChild(parent, nsuri, localName);
69 if(this.next == null){
71 this.localName = null;
74 this.localName = localName;
83 * @return {@inheritDoc}
86 public boolean hasNext(){
87 if(this.next != null) return true;
93 * @return {@inheritDoc}
94 * @throws NoSuchElementException {@inheritDoc}
97 public Element next() throws NoSuchElementException {
98 if(this.next == null) throw new NoSuchElementException();
100 Element result = this.next;
102 Node sibNode = result;
104 sibNode = sibNode.getNextSibling();
105 if(sibNode == null) break;
106 }while( ! matchElemName(sibNode) );
107 this.next = (Element) sibNode;
113 * 兄弟要素にふさわしい名前を持つか判定する。
115 * @return 兄弟にふさわしい名前を持つならtrue
117 private boolean matchElemName(Node node){
118 return DomNsUtils.hasNsLocalNameElem(node,
119 this.nsuri, this.localName );
125 * @throws UnsupportedOperationException 削除を試みたので失敗した
128 public void remove() throws UnsupportedOperationException {
129 throw new UnsupportedOperationException();