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.InvalidFieldValueException;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.MissingFieldException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.util.Unmarshaller;
24  
25  /**
26   * Unmarshals and marshals an optional value from/to an {@link Element}.
27   * 
28   * @param <T> the type of unmarshaled and marshaled value.
29   * @author sanjin
30   */
31  public class Optional<T> implements Unmarshaller<T> {
32  
33      private final Attribute<T> attribute;
34  
35      /**
36       * Creates instance of optional value {@link Unmarshaller} from specified
37       * {@link Attribute}.
38       * 
39       * @param attribute the {@link Attribute}.
40       * @throws NullPointerException if specified {@link Attribute} is
41       *         <code>null</code>.
42       */
43      public Optional(final Attribute<T> attribute) {
44          super();
45  
46          if (null == attribute) {
47              throw new NullPointerException("Attribute cannot be null.");
48          }
49  
50          this.attribute = attribute;
51      }
52  
53      /**
54       * Unmarshals the attribute from the {@link Element}. If attribute is
55       * missing then the <code>null</code> is returned.
56       * 
57       * @return the unmarshaled attribute or <code>null</code> if attribute is
58       *         missing.
59       * @throws InvalidFieldValueException if the attribute is empty or blank
60       *         string or invalid value.
61       */
62      @Override
63      public T unmarshal() throws InvalidFieldValueException {
64          T result = null;
65  
66          try {
67              result = this.attribute.unmarshal();
68          } catch (final MissingFieldException ignored) {
69              /* ignore and return null */
70          }
71  
72          return result;
73      }
74  
75      /**
76       * Marshals specified value into the attribute in the {@link Element}. If
77       * specified value is <code>null</code> then attribute is removed from the
78       * {@link Element}.
79       * 
80       * @param value the value of the attribute.
81       */
82      @Override
83      public void marshal(final T value) {
84          if (null == value) {
85              remove();
86          } else {
87              this.attribute.marshal(value);
88          }
89      }
90  
91      /**
92       * Removes the attribute from the {@link Element}.
93       */
94      @Override
95      public void remove() {
96          this.attribute.remove();
97      }
98  }