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.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.Append;
30  import at.ac.tuwien.infosys.sm4all.copal.api.xml.BaseUnmarshallerBuilder;
31  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ElementUnmarshaller;
32  import at.ac.tuwien.infosys.sm4all.copal.api.xml.ListUnmarshaller;
33  import at.ac.tuwien.infosys.sm4all.copal.api.xml.StringAttribute;
34  
35  /**
36   * Unmarshalls and marshalls 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 marshalled {@link URL}
51       * .
52       */
53      public static final String URL_ELEMENT = "URL";
54      /**
55       * The name of attribute that holds marshalled {@link URL} address.
56       */
57      public static final String URL_ADDRESS_ATTRIBUTE = "address";
58      /**
59       * The local name of child {@link Element} that holds marshalled classpath
60       * location.
61       */
62      public static final String CLASSPATH_ELEMENT = "ClassPath";
63      /**
64       * The name of attribute that holds marshalled 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()).withStrategy(
70              new Append());
71  
72      private final ClassLoader classLoader;
73      private final Element element;
74      private final Unmarshaller<String> url;
75      private final Unmarshaller<String> classPath;
76  
77      /**
78       * Creates instance of {@link Schema} {@link URL} {@link Unmarshaller} which
79       * uses specified {@link Element} for unmarshalling and/or marshalling.
80       * 
81       * @param element the {@link Element} used for unmarshalling and
82       *        marshalling.
83       * @throws NullPointerException if specified {@link Element} is
84       *         <code>null</code>.
85       */
86      public SchemaURLUnmarshaller(final Element element) {
87          this(null, element);
88      }
89  
90      /**
91       * Creates instance of {@link Schema} {@link URL} {@link Unmarshaller} which
92       * uses specified {@link Element} for unmarshalling and/or marshalling. The
93       * specified {@link ClassLoader} is used for resolving {@link Schema}
94       * {@link URL} that is in the classpath.
95       * 
96       * @param classLoader the {@link ClassLoader} used for resolving
97       *        {@link Schema} {@link URL} that is in the classpath.
98       * @param element the {@link Element} used for marshalling and
99       *        unmarshalling.
100      * @throws NullPointerException if specified {@link Element} is
101      *         <code>null</code>.
102      */
103     public SchemaURLUnmarshaller(final ClassLoader classLoader,
104             final Element element) {
105         super();
106 
107         if (element == null)
108             throw new NullPointerException("XML DOM element cannot be null.");
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                 new Append());
115         this.classPath = new ElementUnmarshaller<String>(
116                 element,
117                 CLASSPATH_ELEMENT,
118                 new StringAttribute.Builder().withName(CLASSPATH_LOCATION_ATTRIBUTE),
119                 new Append());
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      * Unmarshalls a {@link Schema} {@link URL} from the {@link Element}.
144      * 
145      * @return the unmarshalled {@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             if (this.classLoader == null)
158                 throw new MissingFieldException("Schema URL", ex);
159 
160             try {
161                 result = this.classLoader.getResource(this.classPath.unmarshal());
162                 if (result == null)
163                     throw new InvalidFieldValueException(
164                             "Schema classpath location cannot be found.");
165             } catch (final MissingFieldException ex1) {
166                 if (CLASSPATH_LOCATION_ATTRIBUTE.equals(ex1.getFieldName()))
167                     throw new MissingFieldException(
168                             "Schema classpath location", ex);
169 
170                 throw new MissingFieldException(URL_ELEMENT + " or "
171                         + CLASSPATH_ELEMENT);
172             }
173         } catch (final MalformedURLException ex) {
174             throw new InvalidFieldValueException(
175                     "Schema URL address is malformed.", ex);
176         }
177 
178         return result;
179     }
180 
181     /**
182      * Marshalls specified {@link Schema} {@link URL} into the {@link Element}.
183      * 
184      * @param schemaURL the {@link Schema} {@link URL}.
185      * @throws NullPointerException if specified {@link Schema} {@link URL} is
186      *         <code>null</code>.
187      */
188     @Override
189     public void marshal(final URL schemaURL) {
190         if (schemaURL == null)
191             throw new NullPointerException("Schema URL cannot be null.");
192 
193         this.classPath.remove();
194         this.url.marshal(schemaURL.toString());
195     }
196 
197     /**
198      * Removes any marshalled {@link Schema} {@link URL} from the
199      * {@link Element}.
200      */
201     @Override
202     public void remove() {
203         this.url.remove();
204         this.classPath.remove();
205     }
206 
207     /**
208      * Creates instance of {@link ListUnmarshaller.Builder} for {@link Schema}
209      * {@link URL}s. The returned {@link ListUnmarshaller.Builder} does not have
210      * the parent {@link Element} set and caller should set it before building
211      * the {@link ListUnmarshaller} for {@link Schema} {@link URL}s. The name
212      * for child {@link Element}s is set to {@link #SCHEMA_ELEMENT} and the
213      * strategy is set to {@link Append}.
214      * 
215      * @return the {@link ListUnmarshaller.Builder} for {@link Schema}
216      *         {@link URL}s.
217      */
218     public static ListUnmarshaller.Builder<URL> getListBuilder() {
219         return LIST_BUILDER;
220     }
221 
222     /**
223      * Creates instance of {@link ListUnmarshaller.Builder} for {@link Schema}
224      * {@link URL}s. The {@link Schema} {@link URL}s in classpath will be
225      * resolved with specified {@link ClassLoader}. The returned
226      * {@link ListUnmarshaller.Builder} does not have the parent {@link Element}
227      * set and caller should set it before building the {@link ListUnmarshaller}
228      * for {@link Schema} {@link URL}s. The name for child {@link Element}s is
229      * set to {@link #SCHEMA_ELEMENT} and the strategy is set to {@link Append}.
230      * 
231      * @param classLoader the {@link ClassLoader} used for resolving
232      *        {@link Schema} {@link URL}s that are in the classpath.
233      * @return the {@link ListUnmarshaller.Builder} for {@link Schema}
234      *         {@link URL}s.
235      */
236     public static ListUnmarshaller.Builder<URL> getListBuilder(
237             final ClassLoader classLoader) {
238         return new ListUnmarshaller.Builder<URL>().withChildName(SCHEMA_ELEMENT).withBuilder(
239                 new Builder().withClassLoader(classLoader)).withStrategy(
240                 new Append());
241     }
242 
243     /**
244      * Builder of {@link SchemaURLUnmarshaller}.
245      * 
246      * @author sanjin
247      */
248     public static class Builder extends BaseUnmarshallerBuilder<URL> {
249 
250         private final AtomicReference<ClassLoader> classLoaderRef = new AtomicReference<ClassLoader>();
251 
252         /**
253          * Create uninitialized instance of
254          * {@link SchemaURLUnmarshaller.Builder}.
255          */
256         public Builder() {
257             super();
258         }
259 
260         /**
261          * Clone-constructor.
262          * 
263          * @param builder the cloned {@link SchemaURLUnmarshaller.Builder}.
264          */
265         private Builder(final Builder builder) {
266             super(builder);
267 
268             this.classLoaderRef.set(builder.classLoaderRef.get());
269         }
270 
271         /**
272          * Returns the {@link ClassLoader}.
273          * 
274          * @return the {@link ClassLoader}.
275          */
276         public ClassLoader getClassLoader() {
277             return this.classLoaderRef.get();
278         }
279 
280         /**
281          * Create instance of {@link SchemaURLUnmarshaller.Builder} that will
282          * build {@link SchemaURLUnmarshaller}s that will use specified
283          * {@link ClassLoader} to resolve {@link Schema} {@link URL}s that are
284          * in the classpath.
285          * 
286          * @param classLoader the {@link ClassLoader}.
287          * @return an {@link SchemaURLUnmarshaller.Builder}.
288          */
289         public Builder withClassLoader(final ClassLoader classLoader) {
290             final Builder result = clone();
291 
292             result.classLoaderRef.set(classLoader);
293 
294             return result;
295         }
296 
297         /**
298          * Create instance of {@link SchemaURLUnmarshaller}.
299          * 
300          * @return a {@link SchemaURLUnmarshaller}.
301          */
302         @Override
303         public SchemaURLUnmarshaller build() {
304             return new SchemaURLUnmarshaller(getClassLoader(), getElement());
305         }
306 
307         @Override
308         protected Builder clone() {
309             return new Builder(this);
310         }
311     }
312 }