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 XMLProcessorAction implements Unmarshaller<ProcessorAction>,
35          Marshaller<ProcessorAction> {
36  
37      private final Element element;
38  
39      /**
40       * Creates instance of {@link ProcessorAction} marshaller and unmarshaller
41       * which uses specified {@link Element} to marshal and/or unmarshal a
42       * {@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 XMLProcessorAction(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 unmarshalled {@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          final String name = new ProcessorActionName(this.element).unmarshal();
74          final String inputEvent = new ProcessorActionEventType(this.element).unmarshal();
75          final String outputEvent = new ProcessorActionResultType(this.element).unmarshal();
76  
77          final ProcessorAction result;
78  
79          if (outputEvent == null)
80              result = new ProcessorAction(name, inputEvent);
81          else
82              result = new ProcessorAction(name, inputEvent, outputEvent);
83  
84          return result;
85      }
86  
87      /**
88       * Sets the <code>name</code> attribute, <code>inputEvent</code>, and
89       * <code>outputEvent</code> attribute of specified {@link Element} to name,
90       * event type and result type of specified {@link ProcessorAction}. If
91       * specified {@link ProcessorAction} does not have a result type the
92       * <code>outputEvent</code> attribute is removed from specified
93       * {@link Element}.
94       * 
95       * @param action the {@link ProcessorAction}.
96       * @throws NullPointerException if specified {@link ProcessorAction} is
97       *         <code>null</code>.
98       */
99      @Override
100     public void marshal(final ProcessorAction action) {
101         if (action == null)
102             throw new NullPointerException("Action cannot be null.");
103 
104         new ProcessorActionName(this.element).marshal(action);
105         new ProcessorActionEventType(this.element).marshal(action);
106         new ProcessorActionResultType(this.element).marshal(action);
107     }
108 }