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