View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springmodules.validation.util.xml;
18  
19  import java.util.Iterator;
20  
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  
25  /**
26   * Utility methods/functions to deal with org.w3c.dom constructs.
27   *
28   * @author Uri Boness
29   */
30  public class DomUtils {
31  
32      public static Element firstChildElement(Element element) {
33          Iterator iter = childElements(element);
34          return (iter.hasNext()) ? (Element) iter.next() : null;
35      }
36  
37      public static Iterator childElements(Element element) {
38          return new SubElementsIterator(element);
39      }
40  
41      public static Iterator childNodes(Node node) {
42          return new ChildNodesIterator(node);
43      }
44  
45      public static Element getSingleSubElement(Element parent, String elementNamespace, String localName) {
46          NodeList nodes = parent.getElementsByTagNameNS(elementNamespace, localName);
47          return (nodes.getLength() > 0) ? (Element) nodes.item(0) : null;
48      }
49  
50      // this class should not be instanciated.
51      private DomUtils() {
52      }
53  
54  }
55