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.event.xml.type;
19  
20  import java.net.URI;
21  import org.w3c.dom.Element;
22  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.XMLContextEventType;
23  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Marshaller;
24  import at.ac.tuwien.infosys.sm4all.copal.api.xml.OptionalAttributeUnmarshaller;
25  
26  /**
27   * Command that marshalls and unmarshalls namespace {@link URI} of a
28   * {@link XMLContextEventType} into/from an {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class Namespace extends OptionalAttributeUnmarshaller<URI> implements
33          Marshaller<XMLContextEventType> {
34  
35      private static final String NAMESPACE_URI_ATTRIBUTE = "namespace";
36  
37      private final Element element;
38  
39      /**
40       * Creates instance of {@link XMLContextEventType} namespace {@link URI}
41       * marshaller and unmarshaller which uses specified {@link Element} to
42       * marshal and/or unmarshal the namespace {@link URI}.
43       * 
44       * @param element the {@link Element} used for marshalling and
45       *        unmarshalling.
46       * @throws NullPointerException if specified {@link Element} is
47       *         <code>null</code>.
48       */
49      public Namespace(final Element element) {
50          super(element, NAMESPACE_URI_ATTRIBUTE);
51  
52          this.element = element;
53      }
54  
55      @Override
56      protected URI valueOf(final String value) {
57          return URI.create(value);
58      }
59  
60      /**
61       * Sets the <code>namespace</code> attribute of specified {@link Element} to
62       * namespace {@link URI} of specified {@link XMLContextEventType} or removes
63       * the <code>namespace</code> attribute if specified
64       * {@link XMLContextEventType} does not have a namespace {@link URI}.
65       * 
66       * @param eventType the {@link XMLContextEventType}.
67       * @throws NullPointerException if specified {@link XMLContextEventType} is
68       *         <code>null</code>.
69       */
70      @Override
71      public void marshal(final XMLContextEventType eventType) {
72          if (eventType == null)
73              throw new NullPointerException("Event type cannot be null.");
74  
75          if (eventType.hasNamespace())
76              this.element.setAttribute(NAMESPACE_URI_ATTRIBUTE,
77                      eventType.getNamespaceURI().toString());
78          else
79              this.element.removeAttribute(NAMESPACE_URI_ATTRIBUTE);
80      }
81  }