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