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 org.w3c.dom.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.XMLContextEventType;
22  import at.ac.tuwien.infosys.sm4all.copal.api.xml.InvalidFieldValueException;
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 priority of a
28   * {@link XMLContextEventType} into/from an {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class Priority extends OptionalAttributeUnmarshaller<Integer> implements
33          Marshaller<XMLContextEventType> {
34  
35      private static final String PRIORITY_ATTRIBUTE = "priority";
36  
37      private final Element element;
38  
39      /**
40       * Creates instance of {@link XMLContextEventType} priority marshaller and
41       * unmarshaller which uses specified {@link Element} to marshal and/or
42       * unmarshal the priority.
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 Priority(final Element element) {
50          super(element, PRIORITY_ATTRIBUTE);
51  
52          this.element = element;
53      }
54  
55      @Override
56      protected Integer valueOf(final String value)
57              throws InvalidFieldValueException {
58          final Integer result;
59  
60          try {
61              result = Integer.valueOf(value);
62          } catch (final NumberFormatException ex) {
63              throw new InvalidFieldValueException(
64                      "Priority is not a valid number.", ex);
65          }
66  
67          return result;
68      }
69  
70      /**
71       * Sets the <code>priority</code> attribute of specified {@link Element} to
72       * priority of specified {@link XMLContextEventType}.
73       * 
74       * @param eventType the {@link XMLContextEventType}.
75       * @throws NullPointerException if specified {@link XMLContextEventType} is
76       *         <code>null</code>.
77       */
78      @Override
79      public void marshal(final XMLContextEventType eventType) {
80          if (eventType == null)
81              throw new NullPointerException("Event type cannot be null.");
82  
83          this.element.setAttribute(PRIORITY_ATTRIBUTE,
84                  String.valueOf(eventType.getPriority()));
85      }
86  }