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.MissingFieldException;
25  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ParsingException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.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 XMLDefaultAction implements Unmarshaller<DefaultAction>,
35          Marshaller<DefaultAction> {
36  
37      private final Element element;
38  
39      /**
40       * Creates 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 XMLDefaultAction(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          final String name = new DefaultActionName(this.element).unmarshal();
76          final Boolean required = new DefaultActionRequirement(this.element).unmarshal();
77  
78          final DefaultAction result;
79  
80          if (required == null)
81              result = new DefaultAction(name);
82          else
83              result = new DefaultAction(name, required);
84  
85          return result;
86      }
87  
88      /**
89       * Sets the <code>name</code> attribute and <code>required</code> attribute
90       * of specified {@link Element} to name and requirement of specified
91       * {@link DefaultAction}.
92       * 
93       * @param action the {@link DefaultAction}.
94       * @throws NullPointerException if specified {@link DefaultAction} is
95       *         <code>null</code>.
96       */
97      @Override
98      public void marshal(final DefaultAction action) {
99          if (action == null)
100             throw new NullPointerException("Action cannot be null.");
101 
102         new DefaultActionName(this.element).marshal(action);
103         new DefaultActionRequirement(this.element).marshal(action);
104     }
105 }