LibreOffice Module xmerge (master)  1
Driver.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.test;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 
35 
42 public final class Driver {
43 
45  private String fromMime = null;
46 
48  private String toMime = null;
49 
51  private String mergeFile = null;
52 
54  private final ArrayList<String> deviceFiles = new ArrayList<String>();
55 
57  private final String mimeTypes[] = {
58  "sxc", "staroffice/sxc",
59  "sxw","staroffice/sxw"
60  };
61 
67  public static void main(String args[]) {
68  try {
69  // Register jarfiles
70  String propFile = "ConverterInfoList.properties";
71  ConverterInfoList cil = new ConverterInfoList(propFile);
72 
73  Iterator<String> jarFileEnum = cil.getJarFileEnum();
74  while (jarFileEnum.hasNext()) {
75  String jarName = jarFileEnum.next();
76  try {
77  ConverterInfoReader cir = new ConverterInfoReader(jarName, false);
78  Iterator<ConverterInfo> jarInfoEnumeration = cir.getConverterInfoEnumeration();
79  ConverterInfoMgr.addPlugIn(jarInfoEnumeration);
80  } catch (Exception e) {
81  System.out.println("\nCannot not load <" + jarName +
82  "> from the <" + propFile + "> property file");
83  }
84  }
85 
86  Driver app = new Driver();
87  app.parseCommandLine(args);
88  app.doConversion();
89  } catch (IllegalArgumentException ex) {
90 
91  String msg = ex.getMessage();
92  if (msg != null) System.out.println("\n" + msg);
93  showUsage();
94 
95  } catch (Exception ex) {
96 
97  String msg = ex.getMessage();
98  if (msg != null) System.out.println("\n" + msg);
99  ex.printStackTrace();
100  }
101  }
102 
103  private static void close(FileOutputStream c) {
104  if (c == null) return;
105  try {
106  c.close();
107  } catch (Exception e) {
108  e.printStackTrace();
109  }
110  }
111 
118  private void doConversion() throws IllegalArgumentException {
119 
121  Convert myConvert = cf.getConverter(fromMime, toMime);
122  String processFile = null;
123 
124  if (myConvert == null) {
125  System.out.println("\nNo plug-in exists to convert from <" +
126  fromMime + "> to <" + toMime + ">");
127  throw new IllegalArgumentException();
128  }
129 
130  try {
131  Iterator<String> dfEnum = deviceFiles.iterator();
132  while (dfEnum.hasNext()) {
133  processFile = dfEnum.next();
134  File f = new File(processFile);
135 
136  // Make sure the input file actually exists before using it
137  if (!f.exists()) {
138  System.out.println(processFile + " does not exist!");
139  System.exit(0);
140  }
141  FileInputStream fis = new FileInputStream(f);
142  myConvert.addInputStream(f.getName(), fis);
143  }
144  } catch (Exception addExcept) {
145  throw new IllegalArgumentException("\nFile <" + processFile + "> is not in <" +
146  fromMime + "> format", addExcept);
147  }
148 
149  ConvertData dataOut = null;
150 
151  try {
152  dataOut = myConvert.convert();
153  } catch (Exception convertExcept) {
154  System.out.println("\nThere was an error in the conversion");
155  convertExcept.printStackTrace();
156  }
157 
158  if (dataOut != null ) {
159 
160  if (mergeFile == null) {
161  Iterator<Object> docEnum = dataOut.getDocumentEnumeration();
162  while (docEnum.hasNext()) {
163  Document docOut = (Document)docEnum.next();
164  String fileName = docOut.getFileName();
165  FileOutputStream fos = null;
166  try {
167  fos = new FileOutputStream(fileName);
168  docOut.write(fos);
169  fos.flush();
170  } catch (Exception writeExcept) {
171  System.out.println("\nThere was a writing out file <" +
172  fileName + ">");
173  writeExcept.printStackTrace();
174  } finally {
175  close(fos);
176  }
177  }
178  } else {
179  try {
180  FileInputStream mergeIS = new FileInputStream(mergeFile);
181  Document mergeDoc = myConvert.getOfficeDocument(mergeFile, mergeIS);
182  DocumentMerger merger = myConvert.getDocumentMerger(mergeDoc);
183  Iterator<Object> mergeDocEnum = dataOut.getDocumentEnumeration();
184  Document convertedFile = (Document)mergeDocEnum.next();
185 
186  merger.merge(convertedFile);
187  mergeIS.close();
188 
189  FileOutputStream fos = null;
190  try {
191  fos = new FileOutputStream(mergeFile);
192  mergeDoc.write(fos);
193  fos.flush();
194  } finally {
195  close(fos);
196  }
197  } catch (Exception mergeExcept) {
198  System.out.println("\nThere was an error in the merge");
199  mergeExcept.printStackTrace();
200  }
201  }
202  }
203  }
204 
208  private static void showUsage() {
209  System.out.println("\nUsage:");
210  System.out.println("\n java org.openoffice.xmerge.test.Driver <args>");
211  System.out.println("\n where <args> is as follows:");
212  System.out.println(" -from <MIMETYPE> -to <MIMETYPE> [ -merge <OrigDoc ] <document>\n");
213  }
214 
222  private void parseCommandLine(String args[])
223  throws IllegalArgumentException {
224 
225  if (args.length == 0) {
226  throw new IllegalArgumentException();
227  }
228 
229  for (int i = 0; i < args.length; i++) {
230  String arg = args[i];
231 
232  if ("-to".equals(arg)) {
233  toMime = extractArg(i, args);
234  for (int j = 0; j < mimeTypes.length; j+=2) {
235  if(mimeTypes[j].equals(extractArg(i, args)))
236  toMime = mimeTypes[j+1];
237  }
238  i++;
239  } else if ("-from".equals(arg)) {
240  fromMime = extractArg(i, args);
241  for (int j = 0; j < mimeTypes.length; j+=2) {
242  if(mimeTypes[j].equals(extractArg(i, args)))
243  fromMime = mimeTypes[j+1];
244  }
245  i++;
246  } else if ("-merge".equals(arg)) {
247  mergeFile = extractArg(i, args);
248  if (!isZip(mergeFile)) {
249  throw new
250  IllegalArgumentException("Arg " + i +
251  ": expected zip, got " +
252  mergeFile);
253  }
254  i++;
255  } else {
256  deviceFiles.add(arg);
257  }
258  }
259 
260  System.out.println("\nConverting from " + fromMime + " to " + toMime +
261  ((mergeFile != null) ? " with merge " : " "));
262  }
263 
278  private String extractArg(int i, String args[])
279  throws IllegalArgumentException {
280 
281  if (i+1 < args.length)
282  return args[i+1];
283  else throw new
284  IllegalArgumentException("Arg " + i +
285  ": expected arg for " + args[i]);
286  }
287 
295  private boolean isZip(String zipName) {
296 
297  String str = zipName.toLowerCase();
298  return str.endsWith("sxw") || zipName.endsWith("sxc");
299  }
300 }
This class is a command-line driver for the converter framework.
Definition: Driver.java:42
Factory that provides access to.
Provides general purpose utilities.
Provides an interface for plug-in registration.
tuple args
void parseCommandLine(String args[])
Parse command-line arguments.
Definition: Driver.java:222
DocumentMerger getDocumentMerger(Document origDoc)
Returns a.
Definition: Convert.java:131
final String mimeTypes[]
Command-line parameter shortcuts.
Definition: Driver.java:57
Document getOfficeDocument(String name, InputStream is)
Returns the appropriate "Office".
Definition: Convert.java:229
Convert getConverter(String mimeTypeIn, String mimeTypeOut)
Returns the.
exports com.sun.star. java
int i
final ArrayList< String > deviceFiles
Command-line parameter.
Definition: Driver.java:54
Manages the converter plug-ins that are currently active.
static void addPlugIn(ConverterInfo ci)
Adds a converter plug-in to the registry.
static void showUsage()
Display usage.
Definition: Driver.java:208
boolean isZip(String zipName)
Simple validation for Office ZIP files.
Definition: Driver.java:295
Iterator< String > getJarFileEnum()
Returns an.
Iterator< Object > getDocumentEnumeration()
Gets an.
String fromMime
Command-line parameter.
Definition: Driver.java:45
String getFileName()
Returns the.
void write(OutputStream os)
Writes out the.
Class for storing the information about a converter plug-in.
static void close(FileOutputStream c)
Definition: Driver.java:103
void(* f)(TrueTypeTable *)
Provides interfaces for converting between two.
Definition: Convert.java:19
void addInputStream(String name, InputStream is)
Adds an.
Definition: Convert.java:80
static void main(String args[])
Main.
Definition: Driver.java:67
void merge(Document modifiedDoc)
This method will find the changes that had happened in the.
String extractArg(int i, String args[])
Extract the next argument from the array, while checking to see that the array size is not exceeded...
Definition: Driver.java:278
ConvertData convert()
Convert the input specified in calls to the.
Definition: Convert.java:181
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an.
String toMime
Command-line parameter.
Definition: Driver.java:48
Loads a properties file so that registry knows which plug-ins it needs to load.