LibreOffice Module xmerge (master)  1
ConverterInfoMgr.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.util.*;
22 import java.io.*;
23 
32 public final class ConverterInfoMgr {
33 
34  private static final ArrayList<ConverterInfo> converterInfoList = new ArrayList<ConverterInfo>();
35 
46  private static void addPlugIn(ConverterInfo ci) throws RegistryException {
47 
48  // Validate
49  if (ci.getDisplayName() == null) {
51  "Converter must have valid name.");
52  throw re;
53  }
54  if (ci.getClassImpl() == null) {
56  "Converter must have valid class implementation specified.");
57  throw re;
58  }
59  if (ci.getOfficeMime() == null) {
61  "Converter must have valid office mime specified.");
62  throw re;
63  }
64  if (! ci.getDeviceMime().hasNext()) {
66  "Converter must have valid device mime specified.");
67  throw re;
68  }
69 
70  // Verify there is no converter with the same Display Name in the
71  // registry.
72  for (ConverterInfo converterInfo : converterInfoList) {
73  if (ci.getDisplayName().equals(converterInfo.getDisplayName())) {
75  "Converter with specified display name already exists.");
76  throw re;
77  }
78  }
79 
80  // Since this is adding to a static Vector, make sure this add method
81  // call is synchronized.
82  synchronized (converterInfoList) {
83  converterInfoList.add(ci);
84  }
85  }
86 
100  public static void addPlugIn(Iterator<ConverterInfo> jarEnum)
101  throws RegistryException {
102 
103  while (jarEnum.hasNext()) {
104  ConverterInfo converterInfo = jarEnum.next();
105  addPlugIn(converterInfo);
106  }
107  }
108 
116  private static Iterator<ConverterInfo> getConverterInfoEnumeration() {
117  return converterInfoList.iterator();
118  }
119 
129  public static boolean removeByJar(String jar) {
130 
131  boolean rc = false;
132 
133  for (Iterator<ConverterInfo> it = converterInfoList.iterator(); it.hasNext();) {
134  ConverterInfo converterInfo = it.next();
135  if (jar.equals(converterInfo.getJarName())) {
136  it.remove();
137  rc = true;
138  }
139  }
140  return rc;
141  }
142 
152  private static boolean removeByName(String name) {
153 
154  boolean rc = false;
155 
156  for (Iterator<ConverterInfo> it = converterInfoList.iterator(); it.hasNext();) {
157  ConverterInfo converterInfo = it.next();
158  if (name.equals(converterInfo.getDisplayName())) {
159  it.remove();
160  rc = true;
161  }
162  }
163  return rc;
164  }
165 
178  public static ConverterInfo findConverterInfo(String deviceMime, String officeMime) {
179 
180  if (deviceMime == null ||
181  !ConverterInfo.isValidOfficeType(officeMime)) {
182  return null;
183  }
184 
185  // Loop over elements comparing with deviceFromMime
186  for (ConverterInfo converterInfo : converterInfoList) {
187  String toDeviceInfo = converterInfo.getOfficeMime();
188  Iterator<String> fromEnum = converterInfo.getDeviceMime();
189 
190  // Loop over the deviceMime types.
191  while (fromEnum.hasNext()) {
192  String fromDeviceInfo = fromEnum.next();
193  if (deviceMime.trim().equals(fromDeviceInfo) &&
194  officeMime.trim().equals(toDeviceInfo)) {
195  return converterInfo;
196  }
197  }
198  }
199  return null;
200  }
201 
215  private static ConverterInfo[] findConverterInfoChain(String deviceFromMime, String deviceToMime) {
216 
217  if (deviceFromMime == null || deviceToMime == null) {
218  return null;
219  }
220 
221  ConverterInfo[] converterInfo = new ConverterInfo[2];
222 
223  // Loop over elements comparing with deviceFromMime
224  Iterator<ConverterInfo> cifEnum = converterInfoList.iterator();
225  while (cifEnum.hasNext()) {
226 
227  converterInfo[0] = cifEnum.next();
228  String fromOfficeInfo = converterInfo[0].getOfficeMime();
229  Iterator<String> fromEnum = converterInfo[0].getDeviceMime();
230 
231  // Loop over the deviceMime types looking for a deviceFromMime
232  // match.
233  while (fromEnum.hasNext()) {
234  String fromDeviceInfo = fromEnum.next();
235 
236  if (deviceFromMime.trim().equals(fromDeviceInfo)) {
237 
238  // Found a match for deviceFrom. Now loop over the
239  // elements comparing with deviceToMime
240  Iterator<ConverterInfo> citEnum = converterInfoList.iterator();
241  while (citEnum.hasNext()) {
242 
243  converterInfo[1] = citEnum.next();
244  String toOfficeInfo = converterInfo[1].getOfficeMime();
245  Iterator<String> toEnum = converterInfo[1].getDeviceMime();
246 
247  // Loop over deviceMime types looking for a
248  // deviceToMime match.
249  while (toEnum.hasNext()) {
250  String toDeviceInfo = toEnum.next();
251  if (deviceToMime.trim().equals(toDeviceInfo) &&
252  fromOfficeInfo.equals(toOfficeInfo)) {
253 
254  // Found a match
255  return converterInfo;
256  }
257  }
258  }
259  }
260  }
261  }
262  return null;
263  }
264 
265  static String readLine(BufferedReader br) throws IOException{
266  String ret = br.readLine();
267  if (ret == null) {
268  throw new IOException("short read");
269  }
270  return ret;
271  }
272 
279  public static void main(String args[]) {
280 
281  ConverterInfoReader cir = null;
282  boolean validate = false;
283  InputStreamReader isr = new InputStreamReader(System.in);
284  BufferedReader br = new BufferedReader(isr);
285  char c = ' ';
286 
287  boolean exitFlag = false;
288  while (!exitFlag) {
289 
290  System.out.println("\nMenu:");
291  System.out.println("(L)oad plug-ins from a jar file");
292  System.out.println("(D)isplay name unload");
293  System.out.println("(J)ar name unload");
294  System.out.println("(F)ind ConverterInfo");
295  System.out.println("(C)ind ConverterInfo chain");
296  System.out.println("(V)iew plug-ins");
297  System.out.println("(T)oggle Validation");
298  System.out.println("(Q)uit\n");
299 
300  try {
301  c = readLine(br).toUpperCase().trim().charAt(0);
302  } catch(Exception e) {
303  System.out.println("Invalid entry");
304  System.out.println("Error msg: " + e.getMessage());
305  continue;
306  }
307 
308  System.out.println("");
309 
310  // Quit
311  if (c == 'Q') {
312  exitFlag = true;
313 
314  // Load by Jarfile
315  } else if (c == 'L') {
316 
317  System.out.println("Enter path to jarfile: ");
318  try {
319  String jarname = readLine(br).trim();
320  cir = new ConverterInfoReader(jarname,validate);
321  } catch (RegistryException e) {
322  System.out.println("Cannot load plug-in ConverterFactory implementation.");
323  System.out.println("Error msg: " + e.getMessage());
324  } catch (Exception e) {
325  System.out.println("Error adding data to registry");
326  System.out.println("Error msg: " + e.getMessage());
327  }
328 
329  if (cir != null) {
330  Iterator<ConverterInfo> jarInfoEnum = cir.getConverterInfoEnumeration();
331  try {
332  ConverterInfoMgr.addPlugIn(jarInfoEnum);
333  } catch (Exception e) {
334  System.out.println("Error adding data to registry");
335  System.out.println("Error msg: " + e.getMessage());
336  }
337  }
338 
339  // Unload by Display Name or Jarfile
340  } else if (c == 'T') {
341  if (validate){
342  System.out.println("Validation switched off");
343  validate=false;
344  } else {
345  System.out.println("Validation switched on");
346  validate=true;
347  }
348  } else if (c == 'D' || c == 'J') {
349 
350  if (c == 'D') {
351  System.out.println("Enter display name: ");
352  } else {
353  System.out.println("Enter path to jarfile: ");
354  }
355 
356  try {
357  String name = readLine(br).trim();
358  boolean rc = false;
359 
360  if (c == 'D') {
361  rc = ConverterInfoMgr.removeByName(name);
362  } else {
363  rc = ConverterInfoMgr.removeByJar(name);
364  }
365 
366  if (rc) {
367  System.out.println("Remove successful.");
368  } else {
369  System.out.println("Remove failed.");
370  }
371 
372  } catch (Exception e) {
373  System.out.println("Error removing value from registry");
374  System.out.println("Error msg: " + e.getMessage());
375  }
376 
377  // Find Office Mime
378 
379  } else if (c == 'F' || c == 'C') {
380 
381  String findMimeOne = null;
382  String findMimeTwo = null;
383 
384  if (c == 'F') {
385  System.out.println("Enter device mime: ");
386  } else {
387  System.out.println("Enter device from mime: ");
388  }
389 
390  try {
391  findMimeOne = readLine(br).trim();
392  } catch (Exception e) {
393  System.out.println("Error adding data to registry");
394  System.out.println("Error msg: " + e.getMessage());
395  }
396 
397  if (c == 'F') {
398  System.out.println("Enter office mime: ");
399  } else {
400  System.out.println("Enter device to mime: ");
401  }
402 
403  try {
404  findMimeTwo = readLine(br).trim();
405  } catch (Exception e) {
406  System.out.println("Error adding data to registry");
407  System.out.println("Error msg: " + e.getMessage());
408  }
409 
410  if (c == 'F') {
411  ConverterInfo foundInfo = ConverterInfoMgr.findConverterInfo(findMimeOne, findMimeTwo);
412  if (foundInfo != null) {
413  System.out.println(" Found ConverterInfo");
414  System.out.println(" DisplayName : " + foundInfo.getDisplayName());
415  } else {
416  System.out.println(" Did not find ConverterInfo");
417  }
418  } else {
419  ConverterInfo[] foundInfo = ConverterInfoMgr.findConverterInfoChain(findMimeOne,
420  findMimeTwo);
421  if (foundInfo != null && foundInfo[0] != null && foundInfo[1] != null ) {
422  System.out.println(" Found ConverterInfo Chain");
423  System.out.println(" DisplayName : " + foundInfo[0].getDisplayName());
424  System.out.println(" DisplayName : " + foundInfo[1].getDisplayName());
425  } else {
426  System.out.println(" Did not find ConverterInfo");
427  }
428  }
429 
430  // View
431 
432  } else if (c == 'V') {
433 
434  Iterator<ConverterInfo> ciEnum = ConverterInfoMgr.getConverterInfoEnumeration();
435 
436  int ciCnt = 0;
437  while (ciEnum.hasNext())
438  {
439  System.out.println("");
440  System.out.println(" Displaying converter number " + ciCnt);
441  ConverterInfo converterInfo = ciEnum.next();
442  System.out.println(" DisplayName : " + converterInfo.getDisplayName());
443  System.out.println(" JarFile : " + converterInfo.getJarName());
444  System.out.println(" Description : " + converterInfo.getDescription());
445  System.out.println(" Version : " + converterInfo.getVersion());
446  System.out.println(" OfficeMime : " + converterInfo.getOfficeMime());
447  Iterator<String> fromEnum = converterInfo.getDeviceMime();
448  int feCnt = 1;
449  while (fromEnum.hasNext())
450  {
451  System.out.println(" DeviceMime : (#" + feCnt + ") : " +
452  fromEnum.next());
453  feCnt++;
454  }
455  if (feCnt == 1) {
456  System.out.println(" DeviceMime : None specified");
457  }
458 
459  System.out.println(" Vendor : " + converterInfo.getVendor());
460  System.out.println(" ClassImpl : " + converterInfo.getClassImpl());
461  System.out.println(" XsltSerial : " + converterInfo.getXsltSerial());
462  System.out.println(" XsltDeserial : " + converterInfo.getXsltDeserial());
463  System.out.println(" Serialize : " + converterInfo.canSerialize());
464  System.out.println(" Deserialize : " + converterInfo.canDeserialize());
465  System.out.println(" Merge : " + converterInfo.canMerge());
466  ciCnt++;
467  }
468 
469  if (ciCnt == 0) {
470  System.out.println("No converters registered");
471  }
472  } else {
473  System.out.println("Invalid input");
474  }
475  }
476  }
477 }
static boolean removeByName(String name)
Removes any.
static ConverterInfo[] findConverterInfoChain(String deviceFromMime, String deviceToMime)
Returns an array of two.
static void main(String args[])
Main to let the user specify what plug-ins to register from jarfiles and to display the currently reg...
static boolean removeByJar(String jar)
Removes any.
static Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an.
String getVendor()
Returns the vendor name.
String getDescription()
Returns the description.
static final ArrayList< ConverterInfo > converterInfoList
String getOfficeMime()
Returns the office mime-type.
String getDisplayName()
Returns the display name.
exports com.sun.star. java
Iterator< String > getDeviceMime()
Returns an.
Manages the converter plug-ins that are currently active.
static void addPlugIn(ConverterInfo ci)
Adds a converter plug-in to the registry.
static ConverterInfo findConverterInfo(String deviceMime, String officeMime)
Returns the.
static void addPlugIn(Iterator< ConverterInfo > jarEnum)
Adds a list of converter plug-ins to the registry.
String getClassImpl()
Returns the implementation class name of PluginFactory.
static boolean isValidOfficeType(String officeMime)
Returns.
const char * name
Class for storing the information about a converter plug-in.
String getJarName()
Returns the jar file name.
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an.