OSDN Git Service

suppress SimplifyBooleanReturns warning in PMD.
[mikutoga/TogaGem.git] / src / main / java / jp / sfjp / mikutoga / xml / SiblingElemIterator.java
1 /*
2  * sibling element iterator on DOM tree
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.xml;
9
10 import java.util.Iterator;
11 import java.util.NoSuchElementException;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14
15 /**
16  * 兄弟要素間用Iterator。
17  *
18  * <p>同じ親と名前空間とローカル名を持つ要素同士を「兄弟要素」とする。
19  *
20  * <p>ノードの持つ名前空間がnullの場合、全ての名前空間引数にマッチする。
21  *
22  * <p>削除操作は未サポート。
23  */
24 public class SiblingElemIterator implements Iterator<Element> {
25
26     private Element next;
27     private final String nsuri;
28     private final String localName;
29
30
31     /**
32      * コンストラクタ。
33      * @param first 最初の兄弟要素。nullだと一度もiterateしない。
34      */
35     public SiblingElemIterator(Element first){
36         super();
37
38         this.next = first;
39
40         if(this.next == null){
41             this.nsuri     = null;
42             this.localName = null;
43         }else{
44             this.nsuri     = this.next.getNamespaceURI();
45             this.localName = this.next.getLocalName();
46         }
47
48         return;
49     }
50
51     /**
52      * コンストラクタ。
53      *
54      * <p>名前空間引数にnullが渡された場合、全ての名前空間にマッチする。
55      *
56      * <p>ローカル名引数にnullが渡された場合、全てのローカル名にマッチする。
57      *
58      * @param parent 親要素
59      * @param nsuri 子要素の名前空間URI
60      * @param localName 子要素のローカル名
61      */
62     public SiblingElemIterator(Element parent,
63                                String nsuri,
64                                String localName ){
65         super();
66
67         this.next = DomNsUtils.pickFirstChild(parent, nsuri, localName);
68
69         if(this.next == null){
70             this.nsuri     = null;
71             this.localName = null;
72         }else{
73             this.nsuri     = nsuri;
74             this.localName = localName;
75         }
76
77         return;
78     }
79
80
81     /**
82      * {@inheritDoc}
83      * @return {@inheritDoc}
84      */
85     @Override
86     public boolean hasNext(){
87         if(this.next != null) return true;
88         return false;
89     }
90
91     /**
92      * {@inheritDoc}
93      * @return {@inheritDoc}
94      * @throws NoSuchElementException {@inheritDoc}
95      */
96     @Override
97     public Element next() throws NoSuchElementException {
98         if(this.next == null) throw new NoSuchElementException();
99
100         Element result = this.next;
101
102         Node sibNode = result;
103         do{
104             sibNode = sibNode.getNextSibling();
105             if(sibNode == null) break;
106         }while( ! matchElemName(sibNode) );
107         this.next = (Element) sibNode;
108
109         return result;
110     }
111
112     /**
113      * 兄弟要素にふさわしい名前を持つか判定する。
114      * @param node 判定対象
115      * @return 兄弟にふさわしい名前を持つならtrue
116      */
117     private boolean matchElemName(Node node){
118         return DomNsUtils.hasNsLocalNameElem(node,
119                                              this.nsuri, this.localName );
120     }
121
122     /**
123      * {@inheritDoc}
124      * ※削除不可。
125      * @throws UnsupportedOperationException 削除を試みたので失敗した
126      */
127     @Override
128     public void remove() throws UnsupportedOperationException {
129         throw new UnsupportedOperationException();
130     }
131
132 }