1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.util.registry;
25 
26 import java.util.Vector;
27 import java.util.Enumeration;
28 import java.net.URL;
29 import java.net.URLClassLoader;
30 import java.lang.reflect.Constructor;
31 import org.openoffice.xmerge.PluginFactory;
32 import org.openoffice.xmerge.DocumentSerializerFactory;
33 import org.openoffice.xmerge.DocumentDeserializerFactory;
34 import org.openoffice.xmerge.DocumentMergerFactory;
35 
36 /**
37  *  Class for storing the information about a converter plugin.
38  *
39  *  @author  Brian Cameron
40  */
41 public class ConverterInfo {
42 
43     /**
44      *  Keep track of the valid Office mime types
45      */
46     private static String[] validOfficeTypes;
47     public static String SxwType = "staroffice/sxw";
48     public static String SxcType = "staroffice/sxc";
49 
50 
51     static {
52        // This needs to be updated to reflect all valid office types.
53        //
54        validOfficeTypes = new String[2];
55        validOfficeTypes[0] = SxwType;
56        validOfficeTypes[1] = SxcType;
57     }
58 
59     private String           piJarName;
60     private String           piOfficeMime;
61     private Vector           piDeviceMime;
62     private String           piDisplayName;
63     private String           piDescription;
64     private String           piVersion;
65     private String           piVendor;
66     private String           piClassImpl;
67     private String           piXsltSerial;
68     private String           piXsltDeserial;
69     private boolean          piCanSerialize      = false;
70     private boolean          piCanDeserialize    = false;
71     private boolean          piCanMerge          = false;
72     private ClassLoader      piClassLoader       = null;
73     private PluginFactory    piPluginFactory;
74 
75 
76     /**
77      *  The constructor builds a ConverterInfo structure.
78      *
79      *  @param  jarName      The URL of the jarfile.
80      *  @param  officeMime   The office mime-type.
81      *  @param  deviceMime   The device mime-type.
82      *  @param  displayName  The display name.
83      *  @param  description  The description.
84      *  @param  version      The version.
85      *  @param  vendor       The vendor name.
86      *  @param  impl         The implementation class name of
87      *                       PluginFactory.
88      *  @param  xsltSerial   The url of the serializer xsl stylesheet
89      *  @param  xsltDeserial The url of the deserializer xsl stylesheet
90      *
91      *  @throws RegistryException   If <code>ci</code> cannot
92      *                              be loaded.
93      */
ConverterInfo(String jarName, String officeMime, Vector deviceMime, String displayName, String description, String version, String vendor, String impl,String xsltSerial, String xsltDeserial)94     public ConverterInfo(String jarName, String officeMime,
95         Vector deviceMime, String displayName, String description,
96 	String version, String vendor, String impl,String xsltSerial,
97 	String xsltDeserial)
98         throws RegistryException {
99 
100         if (isValidOfficeType(officeMime.trim()) == false) {
101             RegistryException re = new RegistryException(
102                 "Invalid office type");
103             throw re;
104         }
105 
106         piJarName     = jarName.trim();
107         piOfficeMime  = officeMime.trim();
108         piDeviceMime  = deviceMime;
109         piDisplayName = displayName.trim();
110         piDescription = description.trim();
111         piVersion     = version.trim();
112         piVendor      = vendor.trim();
113 	piXsltSerial  = xsltSerial.trim();
114 	piXsltDeserial= xsltDeserial.trim();
115         piClassImpl   = impl.trim();
116         piClassLoader = this.getClass().getClassLoader();
117 
118         // Get instance of the PluginFactory.
119         //
120         try {
121             URL jarURL = new URL(jarName);
122             URLClassLoader loader = new URLClassLoader(new URL[] { jarURL },
123                piClassLoader);
124             Class clas = loader.loadClass(piClassImpl);
125             Class[] argumentTypes = { org.openoffice.xmerge.util.registry.ConverterInfo.class };
126             Constructor construct = clas.getConstructor(argumentTypes);
127 
128             Object[] arguments = { this };
129             piPluginFactory = ( PluginFactory ) construct.newInstance(arguments);
130 
131             // See which interfaces the plug-in PluginFactory supports.
132             //
133             Class[] cl = piPluginFactory.getClass().getInterfaces();
134             for (int i=0; i < cl.length; i++) {
135 
136                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentSerializerFactory")) {
137                    piCanSerialize = true;
138                }
139                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentDeserializerFactory")) {
140                    piCanDeserialize = true;
141                }
142                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentMergerFactory")) {
143                    piCanMerge = true;
144                }
145             }
146 
147         } catch (Exception e) {
148             RegistryException re = new RegistryException(
149                 "Class implementation of the plug-in cannot be loaded.");
150             throw re;
151         }
152     }
153 
154      /**
155      *  The constructor builds a ConverterInfo structure.
156      *
157      *  @param  jarName      The URL of the jarfile.
158      *  @param  officeMime   The office mime-type.
159      *  @param  deviceMime   The device mime-type.
160      *  @param  displayName  The display name.
161      *  @param  description  The description.
162      *  @param  version      The version.
163      *  @param  vendor       The vendor name.
164      *  @param  impl         The implementation class name of
165      *                       PluginFactory.
166      *
167      *  @throws RegistryException   If <code>ci</code> cannot
168      *                              be loaded.
169      */
170 
171 
ConverterInfo(String jarName, String officeMime, Vector deviceMime, String displayName, String description, String version, String vendor, String impl)172     public ConverterInfo(String jarName, String officeMime,
173         Vector deviceMime, String displayName, String description,
174         String version, String vendor, String impl)
175         throws RegistryException {
176 
177         if (isValidOfficeType(officeMime.trim()) == false) {
178             RegistryException re = new RegistryException(
179                 "Invalid office type");
180             throw re;
181         }
182 
183         piJarName     = jarName.trim();
184         piOfficeMime  = officeMime.trim();
185         piDeviceMime  = deviceMime;
186         piDisplayName = displayName.trim();
187         piDescription = description.trim();
188         piVersion     = version.trim();
189         piVendor      = vendor.trim();
190         piClassImpl   = impl.trim();
191         piClassLoader = this.getClass().getClassLoader();
192 
193         // Get instance of the PluginFactory.
194         //
195         try {
196             URL jarURL = new URL(jarName);
197             URLClassLoader loader = new URLClassLoader(new URL[] { jarURL },
198                piClassLoader);
199             Class clas = loader.loadClass(piClassImpl);
200             Class[] argumentTypes = { org.openoffice.xmerge.util.registry.ConverterInfo.class };
201             Constructor construct = clas.getConstructor(argumentTypes);
202 
203             Object[] arguments = { this };
204             piPluginFactory = ( PluginFactory ) construct.newInstance(arguments);
205 
206             // See which interfaces the plug-in PluginFactory supports.
207             //
208             Class[] cl = piPluginFactory.getClass().getInterfaces();
209             for (int i=0; i < cl.length; i++) {
210 
211                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentSerializerFactory")) {
212                    piCanSerialize = true;
213                }
214                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentDeserializerFactory")) {
215                    piCanDeserialize = true;
216                }
217                if (cl[i].getName().equals("org.openoffice.xmerge.DocumentMergerFactory")) {
218                    piCanMerge = true;
219                }
220             }
221 
222         } catch (Exception e) {
223             RegistryException re = new RegistryException(
224                 "Class implementation of the plug-in cannot be loaded.");
225             throw re;
226         }
227     }
228 
229 
230 
231 
232     /**
233      *  Create a default constructor so we can use isValidOfficeType
234      *  without having to actually have a valid ConverterInfo.
235      */
ConverterInfo()236     private ConverterInfo() {
237     }
238 
239 
240     /**
241      *  Returns an instance of the DocumentDeserializerFactory interface.
242      *
243      *  @return  instance of the DocumentDeserializer for this ConverterInfo.
244      */
getDocSerializerFactory()245     public DocumentSerializerFactory getDocSerializerFactory() {
246         return (DocumentSerializerFactory)piPluginFactory;
247     }
248 
249 
250     /**
251      *  Returns an instance of the DocumentSerializerFactory interface.
252      *
253      *  @return  instance of the DocumentSerializer for this ConverterInfo.
254      */
getDocDeserializerFactory()255     public DocumentDeserializerFactory getDocDeserializerFactory() {
256         return (DocumentDeserializerFactory)piPluginFactory;
257     }
258 
259 
260     /**
261      *  Returns an instance of the DocumentMergerFactory interface.
262      *
263      *  @return  instance of the DocumentMergerFactory for this ConverterInfo.
264      */
getDocMergerFactory()265     public DocumentMergerFactory getDocMergerFactory() {
266         return (DocumentMergerFactory)piPluginFactory;
267     }
268 
269 
270     /**
271      *  Returns the jar file name.
272      *
273      *  @return  The jar file name, null if none exists.
274      */
getJarName()275     public String getJarName() {
276         return piJarName;
277     }
278 
279 
280     /**
281      *  Returns the office mime-type.
282      *
283      *  @return  The office mime-type, null if none exists.
284      */
getOfficeMime()285     public String getOfficeMime() {
286         return piOfficeMime;
287     }
288 
289 
290     /**
291      *  Returns an <code>Enumeration</code> of <code>String</code>
292      *  objects indicating the device mime-type.
293      *
294      *  @return  An <code>Enumeration</code> of <code>String</code>
295      *           objects indicating the device mime-type.
296      */
getDeviceMime()297     public Enumeration getDeviceMime() {
298         return(piDeviceMime.elements());
299     }
300 
301 
302     /**
303      *  Returns the display name.
304      *
305      *  @return  The display name, null if none exists.
306      */
getDisplayName()307     public String getDisplayName() {
308         return piDisplayName;
309     }
310 
311 
312     /**
313      *  Returns the description.
314      *
315      *  @return  The description, null if none exists.
316      */
getDescription()317     public String getDescription() {
318         return piDescription;
319     }
320 
321 
322     /**
323      *  Returns the version.
324      *
325      *  @return  The version, null if none exists.
326      */
getVersion()327     public String getVersion() {
328         return piVersion;
329     }
330 
331 
332     /**
333      *  Returns the vendor name.
334      *
335      *  @return  The vendor name, null if none exists.
336      */
getVendor()337     public String getVendor() {
338         return piVendor;
339     }
340 
341 
342     /**
343      *  Returns the implementation class name of PluginFactory.
344      *
345      *  @return  The implementation class name of PluginFactory,
346      *           null if none exists.
347      */
getClassImpl()348     public String getClassImpl() {
349         return piClassImpl;
350     }
351 
352 
353     /**
354      *  Returns the PluginFactory instance for this plug-in.
355      *
356      *  @return  The PluginFactory instance for this plug-in.
357      */
getPluginFactory()358     public PluginFactory getPluginFactory() {
359         return piPluginFactory;
360     }
361 
362 
363     /**
364      *  Returns true if this plug-in has a serializier, false otherwise.
365      *
366      *  @return  true if this plug-in has a serializier, false otherwise.
367      */
canSerialize()368     public boolean canSerialize() {
369         return piCanSerialize;
370     }
371 
372 
373     /**
374      *  Returns true if this plug-in has a deserializier, false otherwise.
375      *
376      *  @return  true if this plug-in has a deserializier, false otherwise.
377      */
canDeserialize()378     public boolean canDeserialize() {
379         return piCanDeserialize;
380     }
381 
382 
383     /**
384      *  Returns true if this plug-in has a merger, false otherwise.
385      *
386      *  @return  true if this plug-in has a merger, false otherwise.
387      */
canMerge()388     public boolean canMerge() {
389         return piCanMerge;
390     }
391 
392 
393     /**
394      *  Returns true if the officeMime is a valid Office mime type.
395      *
396      *  @return  true if the officeMime is a valid Office mime type.
397      */
isValidOfficeType(String officeMime)398     public static boolean isValidOfficeType(String officeMime) {
399 
400         boolean rc = false;
401         for (int i=0; i < validOfficeTypes.length; i++) {
402             if (officeMime.equals(validOfficeTypes[i])) {
403                 rc = true;
404             }
405         }
406 
407        return rc;
408     }
409 
410     /**
411      *  Returns a <code>String</code> containing the Xslt stylesheet url that
412      *  is to be used by the Xslt Plugin Serializer.
413      *
414      *  @return  <code>String</code>
415      */
416 
getXsltSerial()417      public String getXsltSerial() {
418         return piXsltSerial;
419     }
420 
421     /**
422      * Returns a <code>String</code> containing the xslt stylesheet url that
423      * is to be used by the Xslt Plugin Deserializer.
424      *
425      *  @return  <code>String</code>
426      */
427 
getXsltDeserial()428     public String getXsltDeserial() {
429         return piXsltDeserial;
430     }
431 }
432 
433