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