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;
19  
20  import org.w3c.dom.Element;
21  
22  /**
23   * Command that unmarshalls an optional attribute from an {@link Element}.
24   * 
25   * @param <T> the class of unmarshaled value.
26   * @author sanjin
27   */
28  public abstract class OptionalAttributeUnmarshaller<T> implements
29          Unmarshaller<T> {
30  
31      private final Element element;
32      private final String attributeName;
33  
34      /**
35       * Creates instance of optional attribute unmarshaller which uses specified
36       * {@link Element} to unmarshal the optional attribute.
37       * 
38       * @param element the {@link Element} used for unmarshalling.
39       * @param attributeName the name of optional attribute.
40       * @throws NullPointerException if specified {@link Element} or attribute
41       *         name is <code>null</code>.
42       * @throws IllegalArgumentException if specified attribute name is an empty
43       *         or blank string.
44       */
45      public OptionalAttributeUnmarshaller(final Element element,
46              final String attributeName) {
47          super();
48  
49          if (element == null)
50              throw new NullPointerException("XML DOM element cannot be null.");
51          if (attributeName == null)
52              throw new NullPointerException("Attribute name cannot be null.");
53          if (attributeName.trim().isEmpty())
54              throw new IllegalArgumentException(
55                      "Attribute name cannot be empty or blank.");
56  
57          this.element = element;
58          this.attributeName = attributeName;
59      }
60  
61      /**
62       * Returns the optional attribute of specified {@link Element}.
63       * 
64       * @return the optional attribute of specified {@link Element} or
65       *         <code>null</code> if the optional attribute is missing or is
66       *         empty or blank string.
67       * @throws InvalidFieldValueException if specified value is not valid.
68       */
69      @Override
70      public T unmarshal() throws ParsingException {
71          T result = null;
72  
73          if (this.element.hasAttribute(this.attributeName)) {
74              final String value = this.element.getAttribute(this.attributeName).trim();
75              if (!value.isEmpty())
76                  result = valueOf(value);
77  
78          }
79  
80          return result;
81      }
82  
83      /**
84       * Converts specified {@link String} value of the optional attribute to
85       * desired class.
86       * 
87       * @param value the {@link String} value of the optional attribute.
88       * @return the optional attribute as instance of the desired class.
89       * @throws InvalidFieldValueException if specified value is not valid.
90       */
91      protected abstract T valueOf(String value)
92              throws InvalidFieldValueException;
93  }