LibreOffice Module xmerge (master)  1
ConverterInfo.java
Go to the documentation of this file.
1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements. See the NOTICE file distributed
12  * with this work for additional information regarding copyright
13  * ownership. The ASF licenses this file to you under the Apache
14  * License, Version 2.0 (the "License"); you may not use this file
15  * except in compliance with the License. You may obtain a copy of
16  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xmerge.util.registry;
20 
21 import java.lang.reflect.Constructor;
22 import java.net.URL;
23 import java.net.URLClassLoader;
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 
33 
37 public class ConverterInfo {
38 
40  private static final String[] validOfficeTypes = new String[] {
41  // This needs to be updated to reflect all valid office types.
42  "staroffice/sxw",
43  "staroffice/sxc"
44  };
45 
46  private final String piJarName;
47  private final String piOfficeMime;
48  private final ArrayList<String> piDeviceMime;
49  private final String piDisplayName;
50  private final String piDescription;
51  private final String piVersion;
52  private final String piVendor;
53  private final String piClassImpl;
56  private boolean piCanSerialize = false;
57  private boolean piCanDeserialize = false;
58  private boolean piCanMerge = false;
59  private final ClassLoader piClassLoader;
61 
62 
80  public ConverterInfo(String jarName, String officeMime,
81  ArrayList<String> deviceMime, String displayName,
82  String description, String version, String vendor,
83  String impl,String xsltSerial, String xsltDeserial)
84  throws RegistryException {
85 
86  if (!isValidOfficeType(officeMime.trim())) {
87  RegistryException re = new RegistryException( "Invalid office type");
88  throw re;
89  }
90 
91  piJarName = jarName.trim();
92  piOfficeMime = officeMime.trim();
93  piDeviceMime = deviceMime;
94  piDisplayName = displayName.trim();
95  piDescription = description.trim();
96  piVersion = version.trim();
97  piVendor = vendor.trim();
98  piXsltSerial = xsltSerial.trim();
99  piXsltDeserial= xsltDeserial.trim();
100  piClassImpl = impl.trim();
101  piClassLoader = this.getClass().getClassLoader();
102 
103  // Get instance of the PluginFactory.
104 
105  try {
106  final URL jarURL = new URL(jarName);
107  final URL[] urls = new URL[] { jarURL };
108  URLClassLoader loader = AccessController.doPrivileged(
109  new PrivilegedAction<URLClassLoader>() {
110  public URLClassLoader run() {
111  return new URLClassLoader(urls, piClassLoader);
112  }
113  });
114  Class<?> clas = loader.loadClass(piClassImpl);
115  Class<?>[] argumentTypes = { org.openoffice.xmerge.util.registry.ConverterInfo.class };
116  Constructor<?> construct = clas.getConstructor(argumentTypes);
117 
118  Object[] arguments = { this };
119  piPluginFactory = ( PluginFactory ) construct.newInstance(arguments);
120 
121  // See which interfaces the plug-in PluginFactory supports.
122 
123  Class<?>[] cl = piPluginFactory.getClass().getInterfaces();
124  for (int i=0; i < cl.length; i++) {
125 
126  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentSerializerFactory")) {
127  piCanSerialize = true;
128  }
129  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentDeserializerFactory")) {
130  piCanDeserialize = true;
131  }
132  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentMergerFactory")) {
133  piCanMerge = true;
134  }
135  }
136 
137  } catch (Exception e) {
138  throw new RegistryException(
139  "Class implementation of the plug-in cannot be loaded.", e);
140  }
141  }
142 
158  public ConverterInfo(String jarName, String officeMime,
159  ArrayList<String> deviceMime, String displayName, String description,
160  String version, String vendor, String impl)
161  throws RegistryException {
162 
163  if (officeMime == null || displayName == null || description == null ||
164  version == null || vendor == null || impl == null)
165  throw new IllegalArgumentException("arguments unexpected null");
166 
167  if (!isValidOfficeType(officeMime.trim())) {
169  "Invalid office type");
170  throw re;
171  }
172 
173  piJarName = jarName.trim();
174  piOfficeMime = officeMime.trim();
175  piDeviceMime = deviceMime;
176  piDisplayName = displayName.trim();
177  piDescription = description.trim();
178  piVersion = version.trim();
179  piVendor = vendor.trim();
180  piClassImpl = impl.trim();
181  piClassLoader = this.getClass().getClassLoader();
182 
183  // Get instance of the PluginFactory.
184 
185  try {
186  final URL jarURL = new URL(jarName);
187  final URL[] urls = new URL[] { jarURL };
188  URLClassLoader loader = AccessController.doPrivileged(
189  new PrivilegedAction<URLClassLoader>() {
190  public URLClassLoader run() {
191  return new URLClassLoader(urls, piClassLoader);
192  }
193  });
194  Class<?> clas = loader.loadClass(piClassImpl);
195  Class<?>[] argumentTypes = { org.openoffice.xmerge.util.registry.ConverterInfo.class };
196  Constructor<?> construct = clas.getConstructor(argumentTypes);
197 
198  Object[] arguments = { this };
199  piPluginFactory = ( PluginFactory ) construct.newInstance(arguments);
200 
201  // See which interfaces the plug-in PluginFactory supports.
202 
203  Class<?>[] cl = piPluginFactory.getClass().getInterfaces();
204  for (int i=0; i < cl.length; i++) {
205 
206  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentSerializerFactory")) {
207  piCanSerialize = true;
208  }
209  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentDeserializerFactory")) {
210  piCanDeserialize = true;
211  }
212  if (cl[i].getName().equals("org.openoffice.xmerge.DocumentMergerFactory")) {
213  piCanMerge = true;
214  }
215  }
216 
217  } catch (Exception e) {
218  throw new RegistryException(
219  "Class implementation of the plug-in cannot be loaded.", e);
220  }
221  }
222 
231  }
232 
241  }
242 
251  }
252 
258  public String getJarName() {
259  return piJarName;
260  }
261 
268  return piOfficeMime;
269  }
270 
278  public Iterator<String> getDeviceMime() {
279  return piDeviceMime.iterator();
280  }
281 
288  return piDisplayName;
289  }
290 
297  return piDescription;
298  }
299 
305  public String getVersion() {
306  return piVersion;
307  }
308 
314  public String getVendor() {
315  return piVendor;
316  }
317 
324  public String getClassImpl() {
325  return piClassImpl;
326  }
327 
334  return piPluginFactory;
335  }
336 
344  public boolean canSerialize() {
345  return piCanSerialize;
346  }
347 
355  public boolean canDeserialize() {
356  return piCanDeserialize;
357  }
358 
366  public boolean canMerge() {
367  return piCanMerge;
368  }
369 
375  public static boolean isValidOfficeType(String officeMime) {
376 
377  boolean rc = false;
378  for (String validOfficeType : validOfficeTypes) {
379  if (officeMime.equals(validOfficeType)) {
380  rc = true;
381  }
382  }
383 
384  return rc;
385  }
386 
394  return piXsltSerial;
395  }
396 
404  return piXsltDeserial;
405  }
406 }
static final String[] validOfficeTypes
Keep track of the valid Office mime types.
Provides general purpose utilities.
ConverterInfo(String jarName, String officeMime, ArrayList< String > deviceMime, String displayName, String description, String version, String vendor, String impl)
The constructor builds a ConverterInfo structure.
DocumentSerializerFactory getDocSerializerFactory()
Returns an instance of the.
Provides an interface for plug-in registration.
tuple loader
String getVendor()
Returns the vendor name.
String getDescription()
Returns the description.
String getOfficeMime()
Returns the office mime-type.
String getDisplayName()
Returns the display name.
def run
exports com.sun.star. java
Iterator< String > getDeviceMime()
Returns an.
int i
PyRef getClass(const OUString &name, const Runtime &runtime)
DocumentMergerFactory getDocMergerFactory()
Returns an instance of the DocumentMergerFactory interface.
String getClassImpl()
Returns the implementation class name of PluginFactory.
static boolean isValidOfficeType(String officeMime)
Returns.
DocumentDeserializerFactory getDocDeserializerFactory()
Returns an instance of the.
ConverterInfo(String jarName, String officeMime, ArrayList< String > deviceMime, String displayName, String description, String version, String vendor, String impl, String xsltSerial, String xsltDeserial)
The constructor builds a ConverterInfo structure.
Class for storing the information about a converter plug-in.
String getJarName()
Returns the jar file name.
Provides interfaces for converting between two.
Definition: Convert.java:19