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