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