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.Marshaller;
23  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.OptionalAttributeUnmarshaller;
24  
25  /**
26   * Command that marshalls and unmarshalls result type of a
27   * {@link ProcessorAction} into/from an {@link Element}.
28   * 
29   * @author sanjin
30   */
31  public class ProcessorActionResultType extends
32          OptionalAttributeUnmarshaller<String> implements
33          Marshaller<ProcessorAction> {
34  
35      private static final String RESULT_TYPE_ATTRIBUTE = "outputEvent";
36  
37      private final Element element;
38  
39      /**
40       * Creates instance of {@link ProcessorAction} result type marshaller and
41       * unmarshaller which uses specified {@link Element} to marshal and/or
42       * unmarshal the result type.
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 ProcessorActionResultType(final Element element) {
50          super(element, RESULT_TYPE_ATTRIBUTE);
51  
52          this.element = element;
53      }
54  
55      @Override
56      protected String valueOf(final String value) {
57          return value;
58      }
59  
60      /**
61       * Sets the <code>outputEvent</code> attribute of specified {@link Element}
62       * to result type of specified {@link ProcessorAction}.
63       * 
64       * @param action the {@link ProcessorAction}.
65       * @throws NullPointerException if specified {@link ProcessorAction} is
66       *         <code>null</code>.
67       */
68      @Override
69      public void marshal(final ProcessorAction action) {
70          if (action == null)
71              throw new NullPointerException("Action cannot be null.");
72  
73          final String value = action.getResultType();
74          if ((value == null) || value.trim().isEmpty())
75              this.element.removeAttribute(RESULT_TYPE_ATTRIBUTE);
76          else
77              this.element.setAttribute(RESULT_TYPE_ATTRIBUTE, value);
78      }
79  }