2 * XML DOM utilities with namespace
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.LinkedList;
12 import java.util.List;
13 import java.util.NoSuchElementException;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
21 public class DomNsUtils {
24 * ノードの名前空間とローカル名を判定する。
26 * @param nsuri 名前空間URI
27 * @param localName ローカル名
28 * @return ノードの名前空間とローカル名が一致したらtrue
30 public static boolean hasNsLocalName(Node node,
33 String nodeLocalName = node.getLocalName();
34 String nodeNsUri = node.getNamespaceURI();
36 if( ! localName.equals(nodeLocalName) ) return false;
37 if( ! nsuri.equals(nodeNsUri) ) return false;
45 * @param nsuri 名前空間URI
46 * @param localName 子要素名
48 * @throws TogaXmlException 1つも見つからなかった
50 public static Element getChild(Element parent,
53 throws TogaXmlException{
54 Element result = null;
56 for(Node node = parent.getFirstChild();
58 node = node.getNextSibling() ){
60 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
61 Element elem = (Element) node;
63 if(hasNsLocalName(elem, nsuri, localName)){
71 "Elem:[" + localName + "] was not found in "
72 +"Elem:[" + parent.getTagName() + "]";
73 throw new TogaXmlException(message);
80 * 親要素が指定された名前の子要素を持つか判定する。
82 * @param nsuri 名前空間URI
83 * @param localName 子要素名
84 * @return 指定名の子要素が存在すればtrue
86 public static boolean hasChild(Element parent,
89 for(Node node = parent.getFirstChild();
91 node = node.getNextSibling() ){
93 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
94 Element elem = (Element) node;
96 if(hasNsLocalName(elem, nsuri, localName)){
105 * 指定された名前の子要素のリストを返す。
107 * @param nsuri 名前空間URI
108 * @param localName 子要素名
111 public static List<Element> getChildList(Element parent,
114 List<Element> result = new LinkedList<Element>();
116 for(Node node = parent.getFirstChild();
118 node = node.getNextSibling() ){
120 if(node.getNodeType() != Node.ELEMENT_NODE) continue;
121 Element elem = (Element) node;
123 if(hasNsLocalName(elem, nsuri, localName)){
132 * 指定された名前の子要素の列挙子を返す。
134 * @param nsuri 名前空間URI
135 * @param localName 子要素名
138 public static Iterator<Element> getChildIterator(Element parent,
143 firstElem = getChild(parent, nsuri, localName);
144 }catch(TogaXmlException e){
148 Iterator<Element> result = new ElemIterator(firstElem);
154 * 指定された名前の子要素のforeachを返す。
156 * @param nsuri 名前空間URI
157 * @param localName 子要素名
158 * @return 子要素のforeach
160 public static Iterable<Element> getEachChild(Element parent,
163 final Iterator<Element> iterator =
164 getChildIterator(parent, nsuri, localName);
165 Iterable<Element> result = new Iterable<Element>(){
167 public Iterator<Element> iterator(){
175 * 同じ名前空間とローカル名を持つ次の要素を返す。
177 * @return 次の要素。なければnull
179 public static Element nextNamedElement(Element elem){
180 String nsuri = elem.getNamespaceURI();
181 String localName = elem.getLocalName();
182 Element nextElem = elem;
184 nextElem = DomUtils.nextElement(nextElem);
185 if(nextElem == null) break;
186 if(hasNsLocalName(nextElem, nsuri, localName)) break;
194 * 同じ親要素と同じ要素名を持つ兄弟要素を列挙する列挙子。
196 private static class ElemIterator implements Iterator<Element>{
197 private Element next;
201 * @param elem 最初の要素。nullを指定すれば空列挙子となる。
203 private ElemIterator(Element elem){
210 * @return {@inheritDoc}
213 public boolean hasNext(){
214 if(this.next == null) return false;
220 * @return {@inheritDoc}
221 * @throws NoSuchElementException {@inheritDoc}
224 public Element next() throws NoSuchElementException{
225 if(this.next == null) throw new NoSuchElementException();
226 Element result = this.next;
227 this.next = nextNamedElement(this.next);
236 public void remove(){
237 throw new UnsupportedOperationException();