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.DefaultAction;
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 requirement of a {@link DefaultAction}
28   * into/from an {@link Element}.
29   * 
30   * @author sanjin
31   */
32  public class DefaultActionRequirement extends
33          OptionalAttributeUnmarshaller<Boolean> implements
34          Marshaller<DefaultAction> {
35  
36      private static final String REQUIREMENT_ATTRIBUTE = "required";
37  
38      private final Element element;
39  
40      /**
41       * Creates instance of {@link DefaultAction} requirement marshaller and
42       * unmarshaller which uses specified {@link Element} to marshal and/or
43       * unmarshal the requirement.
44       * 
45       * @param element the {@link Element} used for marshalling and
46       *        unmarshalling.
47       * @throws NullPointerException if specified {@link Element} is
48       *         <code>null</code>.
49       */
50      public DefaultActionRequirement(final Element element) {
51          super(element, REQUIREMENT_ATTRIBUTE);
52  
53          this.element = element;
54      }
55  
56      @Override
57      protected Boolean valueOf(final String value)
58              throws InvalidFieldValueException {
59          final Boolean result;
60  
61          if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))
62              result = Boolean.valueOf(value);
63          else
64              throw new InvalidFieldValueException(
65                      "Action requirement is not true or false.");
66  
67          return result;
68      }
69  
70      /**
71       * Sets the <code>required</code> attribute of specified {@link Element} to
72       * requirement of specified {@link DefaultAction}.
73       * 
74       * @param action the {@link DefaultAction}.
75       * @throws NullPointerException if specified {@link DefaultAction} is
76       *         <code>null</code>.
77       */
78      @Override
79      public void marshal(final DefaultAction action) {
80          if (action == null)
81              throw new NullPointerException("Action cannot be null.");
82  
83          this.element.setAttribute("required",
84                  String.valueOf(action.isRequried()));
85      }
86  }