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 at.ac.tuwien.infosys.sm4all.copal.api.util.Marshaller;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.Unmarshaller;
23  import static at.ac.tuwien.infosys.sm4all.copal.api.xml.Constants.COPAL_NAMESPACE;
24  import static at.ac.tuwien.infosys.sm4all.copal.api.xml.Constants.COPAL_PREFIX;
25  
26  /**
27   * Base utility class for XML {@link Marshaller}s and {@link Unmarshaller}s.
28   * 
29   * @author sanjin
30   */
31  public class Base {
32  
33      private final Element element;
34  
35      /**
36       * Create instance of XML utility class that works on specified
37       * {@link Element}.
38       * 
39       * @param element the {@link Element}.
40       */
41      protected Base(final Element element) {
42          super();
43  
44          if (null == element) {
45              throw new NullPointerException("XML DOM element cannot be null.");
46          }
47  
48          this.element = element;
49      }
50  
51      /**
52       * Create an {@link Element} with specified local name, namespace URI equal
53       * to {@link Constants#COPAL_NAMESPACE}, and prefix equal to
54       * {@link Constants#COPAL_PREFIX}.
55       * 
56       * @param localName the local name of the {@link Element}.
57       * @return new {@link Element}.
58       */
59      protected Element createElement(final String localName) {
60          if (null == localName) {
61              throw new NullPointerException("Local name cannot be null.");
62          }
63          if (localName.trim().isEmpty()) {
64              throw new IllegalArgumentException(
65                      "Local name cannot be an empty or blank string.");
66          }
67  
68          return this.element.getOwnerDocument().createElementNS(COPAL_NAMESPACE,
69                  COPAL_PREFIX + ":" + localName);
70      }
71  }