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.DefaultAction;
22  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.InvalidFieldValueException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
24  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.MissingFieldException;
25  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.ParsingException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Unmarshaller;
27  
28  /**
29   * Command that marshalls and unmarshalls a {@link DefaultAction} into/from an
30   * {@link Element}.
31   * 
32   * @author sanjin
33   */
34  public class Action implements Unmarshaller<DefaultAction>,
35          Marshaller<DefaultAction> {
36  
37      private final Element element;
38  
39      /**
40       * Constructs instance of {@link DefaultAction} marshaller and unmarshaller
41       * which uses specified {@link Element} to marshal and/or unmarshal a
42       * {@link DefaultAction}.
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 Action(final Element element) {
50          super();
51  
52          if (element == null)
53              throw new NullPointerException("XML DOM element cannot be null.");
54  
55          this.element = element;
56      }
57  
58      /**
59       * Returns a {@link DefaultAction} with its name equal to the
60       * <code>name</code> attribute of specified {@link Element} and requirement
61       * equal to the <code>required</code> attribute. If specified
62       * {@link Element} does not have a <code>required</code> attribute, then the
63       * {@link DefaultAction#DefaultAction(String)} constructor will be used,
64       * thus setting the requirement to default value.
65       * 
66       * @return the {@link DefaultAction}
67       * @throws MissingFieldException if specified {@link Element} does not have
68       *         a <code>name</code> attribute.
69       * @throws InvalidFieldValueException if the <code>name</code> attribute is
70       *         empty or blank string, or the <code>required</code> attribute is
71       *         not <code>true</code> or <code>false</code>.
72       */
73      @Override
74      public DefaultAction unmarshal() throws ParsingException {
75          if (!this.element.hasAttribute("name"))
76              throw new MissingFieldException("Action name");
77  
78          final String name = this.element.getAttribute("name");
79          if (name.trim().isEmpty())
80              throw new InvalidFieldValueException(
81                      "Action name is empty or blank.");
82  
83          final DefaultAction result;
84          if (this.element.hasAttribute("required")) {
85              final String required = this.element.getAttribute("required").trim();
86              if ("true".equalsIgnoreCase(required)
87                      || "false".equalsIgnoreCase(required))
88                  result = new DefaultAction(name, Boolean.valueOf(required));
89              else
90                  throw new InvalidFieldValueException(
91                          "Action requirement is not true or false.");
92          } else
93              result = new DefaultAction(name);
94  
95          return result;
96      }
97  
98      /**
99       * Sets the <code>name</code> attribute and <code>required</code> attribute
100      * of specified {@link Element} to name and requirement of specified
101      * {@link DefaultAction}.
102      * 
103      * @throws NullPointerException if specified {@link DefaultAction} is
104      *         <code>null</code>.
105      */
106     @Override
107     public void marshal(final DefaultAction action) {
108         if (action == null)
109             throw new NullPointerException("Action cannot be null.");
110 
111         this.element.setAttribute("name", action.getName());
112         this.element.setAttribute("required",
113                 String.valueOf(action.isRequried()));
114     }
115 }