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;
19  
20  import org.w3c.dom.Element;
21  import at.ac.tuwien.infosys.sm4all.copal.api.event.ContextEventAction;
22  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
23  import at.ac.tuwien.infosys.sm4all.copal.api.util.Marshaller;
24  import at.ac.tuwien.infosys.sm4all.copal.api.util.MissingFieldException;
25  import at.ac.tuwien.infosys.sm4all.copal.api.util.Unmarshaller;
26  import at.ac.tuwien.infosys.sm4all.copal.api.xml.BaseMarshallerBuilder;
27  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ListMarshaller;
28  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ListUnmarshaller;
29  import at.ac.tuwien.infosys.sm4all.copal.api.xml.StringAttribute;
30  
31  /**
32   * Unmarshals and marshals a {@link ContextEventAction} from/into an
33   * {@link Element}.
34   * 
35   * @author sanjin
36   */
37  public class ContextEventActionMarshaller implements
38          Marshaller<ContextEventAction> {
39  
40      /**
41       * The local name of child {@link Element}s used in the
42       * {@link ListUnmarshaller.Builder} that is returned by the
43       * {@link #getListBuilder()}.
44       */
45      public static final String ACTION_ELEMENT = "Action";
46      /**
47       * The name of attribute that holds marshaled name.
48       */
49      public static final String NAME_ATTRIBUTE = "name";
50  
51      private static final ListMarshaller.Builder<ContextEventAction> LIST_BUILDER = new ListMarshaller.Builder<ContextEventAction>().withChildName(
52              ACTION_ELEMENT).withBuilder(new Builder());
53  
54      private final Element element;
55      private final Unmarshaller<String> name;
56  
57      /**
58       * Creates instance of {@link ContextEventAction} {@link Unmarshaller} which
59       * uses specified {@link Element} for unmarshalling and/or marshalling.
60       * 
61       * @param element the {@link Element} used for unmarshalling and
62       *        marshalling.
63       * @throws NullPointerException if specified {@link Element} is
64       *         <code>null</code>.
65       */
66      public ContextEventActionMarshaller(final Element element) {
67          super();
68  
69          if (null == element) {
70              throw new NullPointerException("XML DOM element cannot be null.");
71          }
72  
73          this.element = element;
74          this.name = new StringAttribute(NAME_ATTRIBUTE, element);
75      }
76  
77      /**
78       * Returns the {@link Element} used for unmarshalling and marshalling.
79       * 
80       * @return the {@link Element} used for unmarshalling and marshalling.
81       */
82      public Element getElement() {
83          return this.element;
84      }
85  
86      /**
87       * Unmarshals name of {@link ContextEventAction} from the {@link Element}.
88       * 
89       * @return the name of marshaled {@link ContextEventAction}.
90       * @throws FailedUnmarshallingException if unmarshalling was unsuccessful.
91       */
92      public String unmarshalName() throws FailedUnmarshallingException {
93          final String result;
94  
95          try {
96              result = this.name.unmarshal();
97          } catch (final MissingFieldException ex) {
98              throw new MissingFieldException("Action name", ex);
99          }
100 
101         return result;
102     }
103 
104     /**
105      * Marshals specified {@link ContextEventAction} into the {@link Element}.
106      * 
107      * @param action the {@link ContextEventAction}.
108      * @throws NullPointerException if specified {@link ContextEventAction} is
109      *         <code>null</code>.
110      */
111     @Override
112     public void marshal(final ContextEventAction action) {
113         if (null == action) {
114             throw new NullPointerException("Action cannot be null.");
115         }
116 
117         this.name.marshal(action.getName());
118     }
119 
120     /**
121      * Removes any marshaled {@link ContextEventAction} from the {@link Element}
122      * .
123      */
124     @Override
125     public void remove() {
126         this.name.remove();
127     }
128 
129     /**
130      * Creates instance of {@link ListMarshaller.Builder} for
131      * {@link ContextEventAction}s. The returned {@link ListMarshaller.Builder}
132      * does not have the parent {@link Element} set and caller should set it
133      * before building the {@link ListMarshaller.Builder} for
134      * {@link ContextEventAction}s. The name for child {@link Element}s is set
135      * to {@link #ACTION_ELEMENT}.
136      * 
137      * @return the {@link ListMarshaller.Builder} for {@link ContextEventAction}
138      *         s.
139      */
140     public static ListMarshaller.Builder<ContextEventAction> getListBuilder() {
141         return LIST_BUILDER;
142     }
143 
144     /**
145      * Builder of {@link ContextEventActionMarshaller}.
146      * 
147      * @author sanjin
148      */
149     public static class Builder extends
150             BaseMarshallerBuilder<ContextEventAction> {
151 
152         /**
153          * Create uninitialized instance of
154          * {@link ContextEventActionMarshaller.Builder}.
155          */
156         public Builder() {
157             super();
158         }
159 
160         /**
161          * Clone-constructor.
162          * 
163          * @param builder the cloned
164          *        {@link ContextEventActionMarshaller.Builder}.
165          */
166         private Builder(final Builder builder) {
167             super(builder);
168         }
169 
170         /**
171          * Create instance of {@link ContextEventActionMarshaller}.
172          * 
173          * @return a {@link ContextEventActionMarshaller}.
174          */
175         @Override
176         public ContextEventActionMarshaller build() {
177             return new ContextEventActionMarshaller(getElement());
178         }
179 
180         @Override
181         protected Builder copy() {
182             return new Builder(this);
183         }
184     }
185 }