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.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.concurrent.atomic.AtomicReference;
23  import javax.xml.validation.Schema;
24  import org.w3c.dom.Element;
25  import at.ac.tuwien.infosys.sm4all.copal.api.util.FailedUnmarshallingException;
26  import at.ac.tuwien.infosys.sm4all.copal.api.util.InvalidFieldValueException;
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.ElementUnmarshaller;
31  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ListUnmarshaller;
32  import at.ac.tuwien.infosys.sm4all.copal.api.xml.RemoveStrategy;
33  import at.ac.tuwien.infosys.sm4all.copal.api.xml.StringAttribute;
34  
35  /**
36   * Unmarshals and marshals a {@link URL} to a {@link Schema} file from/into an
37   * {@link Element}.
38   * 
39   * @author sanjin
40   */
41  public class SchemaURLUnmarshaller implements Unmarshaller<URL> {
42  
43      /**
44       * The local name of child {@link Element}s used in the
45       * {@link ListUnmarshaller.Builder} that is returned by the
46       * {@link #getListBuilder()}.
47       */
48      public static final String SCHEMA_ELEMENT = "Schema";
49      /**
50       * The local name of child {@link Element} that holds marshaled {@link URL}
51       * .
52       */
53      public static final String URL_ELEMENT = "URL";
54      /**
55       * The name of attribute that holds marshaled {@link URL} address.
56       */
57      public static final String URL_ADDRESS_ATTRIBUTE = "address";
58      /**
59       * The local name of child {@link Element} that holds marshaled classpath
60       * location.
61       */
62      public static final String CLASSPATH_ELEMENT = "ClassPath";
63      /**
64       * The name of attribute that holds marshaled classpath location.
65       */
66      public static final String CLASSPATH_LOCATION_ATTRIBUTE = "location";
67  
68      private static final ListUnmarshaller.Builder<URL> LIST_BUILDER = new ListUnmarshaller.Builder<URL>().withChildName(
69              SCHEMA_ELEMENT).withBuilder(new Builder());
70  
71      private final ClassLoader classLoader;
72      private final Element element;
73      private final Unmarshaller<String> url;
74      private final Unmarshaller<String> classPath;
75  
76      /**
77       * Creates instance of {@link Schema} {@link URL} {@link Unmarshaller} which
78       * uses specified {@link Element} for unmarshalling and/or marshalling.
79       * 
80       * @param element the {@link Element} used for unmarshalling and
81       *        marshalling.
82       * @throws NullPointerException if specified {@link Element} is
83       *         <code>null</code>.
84       */
85      public SchemaURLUnmarshaller(final Element element) {
86          this(null, element);
87      }
88  
89      /**
90       * Creates instance of {@link Schema} {@link URL} {@link Unmarshaller} which
91       * uses specified {@link Element} for unmarshalling and/or marshalling. The
92       * specified {@link ClassLoader} is used for resolving {@link Schema}
93       * {@link URL} that is in the classpath.
94       * 
95       * @param classLoader the {@link ClassLoader} used for resolving
96       *        {@link Schema} {@link URL} that is in the classpath.
97       * @param element the {@link Element} used for marshalling and
98       *        unmarshalling.
99       * @throws NullPointerException if specified {@link Element} is
100      *         <code>null</code>.
101      */
102     public SchemaURLUnmarshaller(final ClassLoader classLoader,
103             final Element element) {
104         super();
105 
106         if (null == element) {
107             throw new NullPointerException("XML DOM element cannot be null.");
108         }
109 
110         this.classLoader = classLoader;
111         this.element = element;
112         this.url = new ElementUnmarshaller<String>(element, URL_ELEMENT,
113                 new StringAttribute.Builder().withName(URL_ADDRESS_ATTRIBUTE),
114                 RemoveStrategy.RemoveElement);
115         this.classPath = new ElementUnmarshaller<String>(
116                 element,
117                 CLASSPATH_ELEMENT,
118                 new StringAttribute.Builder().withName(CLASSPATH_LOCATION_ATTRIBUTE),
119                 RemoveStrategy.RemoveElement);
120     }
121 
122     /**
123      * Returns the {@link ClassLoader} used for resolving {@link Schema}
124      * {@link URL} that is in the classpath.
125      * 
126      * @return the {@link ClassLoader} used for resolving {@link Schema}
127      *         {@link URL} that is in the classpath.
128      */
129     public ClassLoader getClassLoader() {
130         return this.classLoader;
131     }
132 
133     /**
134      * Returns the {@link Element} used for unmarshalling and marshalling.
135      * 
136      * @return the {@link Element} used for unmarshalling and marshalling.
137      */
138     public Element getElement() {
139         return this.element;
140     }
141 
142     /**
143      * Unmarshals a {@link Schema} {@link URL} from the {@link Element}.
144      * 
145      * @return the unmarshaled {@link Schema} {@link URL}.
146      * @throws FailedUnmarshallingException if unmarshalling was unsuccessful.
147      */
148     @Override
149     public URL unmarshal() throws FailedUnmarshallingException {
150         URL result = null;
151 
152         try {
153             result = new URL(this.url.unmarshal());
154         } catch (final MissingFieldException ex) {
155             if (URL_ADDRESS_ATTRIBUTE.equals(ex.getFieldName())) {
156                 throw new MissingFieldException("Schema URL address", ex);
157             }
158             if (null == this.classLoader) {
159                 throw new MissingFieldException("Schema URL", ex);
160             }
161         } catch (final MalformedURLException ex) {
162             throw new InvalidFieldValueException(
163                     "Schema URL address is malformed.", ex);
164         }
165 
166         if (null == result) {
167             try {
168                 result = this.classLoader.getResource(this.classPath.unmarshal());
169                 if (null == result) {
170                     throw new InvalidFieldValueException(
171                             "Schema classpath location cannot be found.");
172                 }
173             } catch (final MissingFieldException ex) {
174                 if (CLASSPATH_LOCATION_ATTRIBUTE.equals(ex.getFieldName())) {
175                     throw new MissingFieldException(
176                             "Schema classpath location", ex);
177                 }
178             }
179         }
180 
181         if (null == result) {
182             throw new MissingFieldException(URL_ELEMENT + " or "
183                     + CLASSPATH_ELEMENT);
184         }
185 
186         return result;
187     }
188 
189     /**
190      * Marshals specified {@link Schema} {@link URL} into the {@link Element}.
191      * 
192      * @param schemaURL the {@link Schema} {@link URL}.
193      * @throws NullPointerException if specified {@link Schema} {@link URL} is
194      *         <code>null</code>.
195      */
196     @Override
197     public void marshal(final URL schemaURL) {
198         if (null == schemaURL) {
199             throw new NullPointerException("Schema URL cannot be null.");
200         }
201 
202         this.classPath.remove();
203         this.url.marshal(schemaURL.toString());
204     }
205 
206     /**
207      * Removes any marshaled {@link Schema} {@link URL} from the {@link Element}
208      * .
209      */
210     @Override
211     public void remove() {
212         this.url.remove();
213         this.classPath.remove();
214     }
215 
216     /**
217      * Creates instance of {@link ListUnmarshaller.Builder} for {@link Schema}
218      * {@link URL}s. The returned {@link ListUnmarshaller.Builder} does not have
219      * the parent {@link Element} set and caller should set it before building
220      * the {@link ListUnmarshaller} for {@link Schema} {@link URL}s. The name
221      * for child {@link Element}s is set to {@link #SCHEMA_ELEMENT}.
222      * 
223      * @return the {@link ListUnmarshaller.Builder} for {@link Schema}
224      *         {@link URL}s.
225      */
226     public static ListUnmarshaller.Builder<URL> getListBuilder() {
227         return LIST_BUILDER;
228     }
229 
230     /**
231      * Creates instance of {@link ListUnmarshaller.Builder} for {@link Schema}
232      * {@link URL}s. The {@link Schema} {@link URL}s in classpath will be
233      * resolved with specified {@link ClassLoader}. The returned
234      * {@link ListUnmarshaller.Builder} does not have the parent {@link Element}
235      * set and caller should set it before building the {@link ListUnmarshaller}
236      * for {@link Schema} {@link URL}s. The name for child {@link Element}s is
237      * set to {@link #SCHEMA_ELEMENT}.
238      * 
239      * @param classLoader the {@link ClassLoader} used for resolving
240      *        {@link Schema} {@link URL}s that are in the classpath.
241      * @return the {@link ListUnmarshaller.Builder} for {@link Schema}
242      *         {@link URL}s.
243      */
244     public static ListUnmarshaller.Builder<URL> getListBuilder(
245             final ClassLoader classLoader) {
246         return new ListUnmarshaller.Builder<URL>().withChildName(SCHEMA_ELEMENT).withBuilder(
247                 new Builder().withClassLoader(classLoader));
248     }
249 
250     /**
251      * Builder of {@link SchemaURLUnmarshaller}.
252      * 
253      * @author sanjin
254      */
255     public static class Builder extends BaseUnmarshallerBuilder<URL> {
256 
257         private final AtomicReference<ClassLoader> classLoaderRef = new AtomicReference<ClassLoader>();
258 
259         /**
260          * Create uninitialized instance of
261          * {@link SchemaURLUnmarshaller.Builder}.
262          */
263         public Builder() {
264             super();
265         }
266 
267         /**
268          * Clone-constructor.
269          * 
270          * @param builder the cloned {@link SchemaURLUnmarshaller.Builder}.
271          */
272         private Builder(final Builder builder) {
273             super(builder);
274 
275             this.classLoaderRef.set(builder.classLoaderRef.get());
276         }
277 
278         /**
279          * Returns the {@link ClassLoader}.
280          * 
281          * @return the {@link ClassLoader}.
282          */
283         public ClassLoader getClassLoader() {
284             return this.classLoaderRef.get();
285         }
286 
287         /**
288          * Create instance of {@link SchemaURLUnmarshaller.Builder} that will
289          * build {@link SchemaURLUnmarshaller}s that will use specified
290          * {@link ClassLoader} to resolve {@link Schema} {@link URL}s that are
291          * in the classpath.
292          * 
293          * @param classLoader the {@link ClassLoader}.
294          * @return an {@link SchemaURLUnmarshaller.Builder}.
295          */
296         public Builder withClassLoader(final ClassLoader classLoader) {
297             final Builder result = copy();
298 
299             result.classLoaderRef.set(classLoader);
300 
301             return result;
302         }
303 
304         /**
305          * Create instance of {@link SchemaURLUnmarshaller}.
306          * 
307          * @return a {@link SchemaURLUnmarshaller}.
308          */
309         @Override
310         public SchemaURLUnmarshaller build() {
311             return new SchemaURLUnmarshaller(getClassLoader(), getElement());
312         }
313 
314         @Override
315         protected Builder copy() {
316             return new Builder(this);
317         }
318     }
319 }