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