View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.xml;
19  
20  import org.w3c.dom.Element;
21  import org.w3c.dom.Node;
22  import org.w3c.dom.NodeList;
23  
24  /**
25   * Utility class for an {@link Element}.
26   * 
27   * @author sanjin
28   */
29  public class ElementUtil {
30  
31      private final Element element;
32  
33      /**
34       * Creates instance of {@link Element} utility class that works on specified
35       * {@link Element}.
36       * 
37       * @param element the {@link Element}.
38       */
39      public ElementUtil(final Element element) {
40          super();
41  
42          if (element == null)
43              throw new NullPointerException("XML DOM element cannot be null.");
44  
45          this.element = element;
46      }
47  
48      /**
49       * Returns first child {@link Element} of specified {@link Element} with
50       * specified local name and namespace URI.
51       * 
52       * @param localName the local name of child {@link Element}.
53       * @param namespaceURI the namespace URI of child {@link Element}.
54       * @return first child {@link Element}.
55       * @throws MissingFieldException if there is no such child {@link Element}.
56       * @throws NullPointerException if specified namespace URI or local name is
57       *         <code>null</code>.
58       */
59      protected Element getChildElement(final String namespaceURI,
60              final String localName) throws MissingFieldException {
61          return getChildElement(this.element, namespaceURI, localName);
62      }
63  
64      /**
65       * Returns first child {@link Element} of specified {@link Element} with
66       * specified local name and namespace URI.
67       * 
68       * @param element the parent {@link Element}.
69       * @param localName the local name of child {@link Element}.
70       * @param namespaceURI the namespace URI of child {@link Element}.
71       * @return first child {@link Element}.
72       * @throws MissingFieldException if there is no such child {@link Element}.
73       * @throws NullPointerException if specified namespace URI or local name is
74       *         <code>null</code>.
75       */
76      public static Element getChildElement(final Element element,
77              final String namespaceURI, final String localName)
78              throws MissingFieldException {
79          if (namespaceURI == null)
80              throw new NullPointerException("Namespace URI cannot be null.");
81          if (localName == null)
82              throw new NullPointerException("Local name cannot be null.");
83  
84          Element result = null;
85  
86          final NodeList inputElements = element.getElementsByTagNameNS(
87                  element.getNamespaceURI(), localName);
88          for (int i = 0; i < inputElements.getLength(); i++) {
89              final Node node = inputElements.item(i);
90              if ((node.getParentNode() == element) && (node instanceof Element)) {
91                  result = (Element) node;
92                  break;
93              }
94          }
95  
96          if (result == null)
97              throw new MissingFieldException(localName);
98  
99          return result;
100     }
101 }