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