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.processor;
19  
20  import org.w3c.dom.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.ProcessorAction;
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 ProcessorAction} into/from an
30   * {@link Element}.
31   * 
32   * @author sanjin
33   */
34  public class Action implements Unmarshaller<ProcessorAction>,
35          Marshaller<ProcessorAction> {
36  
37      private final Element element;
38  
39      /**
40       * Constructs instance of {@link ProcessorAction} marshaller and
41       * unmarshaller which uses specified {@link Element} to marshal and/or
42       * unmarshal a {@link ProcessorAction}.
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 ProcessorAction} with its name equal to the
60       * <code>name</code> attribute of specified {@link Element}, event type
61       * equal to the <code>inputEvent</code> attribute, and, if present, result
62       * type equal to the <code>outputEvent</code> attribute.
63       * 
64       * @return the {@link ProcessorAction}
65       * @throws MissingFieldException if specified {@link Element} does not have
66       *         a <code>name</code> or an <code>inputEvent</code> attribute.
67       * @throws InvalidFieldValueException if the <code>name</code>,
68       *         <code>inputEvent</code> or <code>outputEvent</code> attribute is
69       *         empty or blank string.
70       */
71      @Override
72      public ProcessorAction unmarshal() throws ParsingException {
73          if (!this.element.hasAttribute("name"))
74              throw new MissingFieldException("Action name");
75          if (!this.element.hasAttribute("inputEvent"))
76              throw new MissingFieldException("Action input event");
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 String inputEvent = this.element.getAttribute("inputEvent");
84          if (inputEvent.trim().isEmpty())
85              throw new InvalidFieldValueException(
86                      "Action input event is empty or blank.");
87  
88          final ProcessorAction result;
89          if (this.element.hasAttribute("outputEvent")) {
90              final String outputEvent = this.element.getAttribute("outputEvent");
91              if (outputEvent.trim().isEmpty())
92                  throw new InvalidFieldValueException(
93                          "Action output event is empty or blank.");
94  
95              result = new ProcessorAction(name, inputEvent, outputEvent);
96          } else
97              result = new ProcessorAction(name, inputEvent);
98  
99          return result;
100     }
101 
102     /**
103      * Sets the <code>name</code> attribute, <code>inputEvent</code>, and
104      * <code>outputEvent</code> attribute of specified {@link Element} to name,
105      * event type and result type of specified {@link ProcessorAction}. If
106      * specified {@link ProcessorAction} does not have a result type the
107      * <code>outputEvent</code> attribute is removed from specified
108      * {@link Element}.
109      * 
110      * @throws NullPointerException if specified {@link ProcessorAction} is
111      *         <code>null</code>.
112      */
113     @Override
114     public void marshal(final ProcessorAction action) {
115         if (action == null)
116             throw new NullPointerException("Action cannot be null.");
117 
118         this.element.setAttribute("name", action.getName());
119         this.element.setAttribute("inputEvent", action.getEventType());
120         final String resultType = action.getResultType();
121         if ((resultType != null) && !resultType.trim().isEmpty())
122             this.element.setAttribute("outputEvent", action.getResultType());
123     }
124 }