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
19package org.openoffice.xmerge.test;
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.util.ArrayList;
25import java.util.Iterator;
26
35
42public 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();
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)) {
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)) {
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)) {
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}
ConvertData is used as a container for passing Document objects in and out of the Convert class.
Iterator< Object > getDocumentEnumeration()
Gets an Enumeration to access the Vector of Document objects.
The Convert class manages a conversion from one mime-type to another.
Definition: Convert.java:38
void addInputStream(String name, InputStream is)
Adds an InputStream to be used as input by the Convert class.
Definition: Convert.java:80
Document getOfficeDocument(String name, InputStream is)
Returns the appropriate "Office" Document object for this plug-in.
Definition: Convert.java:229
DocumentMerger getDocumentMerger(Document origDoc)
Returns a DocumentMerger for the given Document.
Definition: Convert.java:131
ConvertData convert()
Convert the input specified in calls to the addInputStream method to the output format specified by t...
Definition: Convert.java:181
Factory that provides access to Convert objects, which are used to do a conversion.
Convert getConverter(String mimeTypeIn, String mimeTypeOut)
Returns the Convert object that converts the specified device/office mime type conversion.
Loads a properties file so that registry knows which plug-ins it needs to load.
Iterator< String > getJarFileEnum()
Returns an Iterator of String objects.
This class is a command-line driver for the converter framework.
Definition: Driver.java:42
void parseCommandLine(String args[])
Parse command-line arguments.
Definition: Driver.java:222
static void main(String args[])
Main.
Definition: Driver.java:67
String toMime
Command-line parameter.
Definition: Driver.java:48
void doConversion()
Gets a Convert object using the ConverterFactory and does the conversion using this object.
Definition: Driver.java:118
static void close(FileOutputStream c)
Definition: Driver.java:103
static void showUsage()
Display usage.
Definition: Driver.java:208
final ArrayList< String > deviceFiles
Command-line parameter.
Definition: Driver.java:54
String fromMime
Command-line parameter.
Definition: Driver.java:45
boolean isZip(String zipName)
Simple validation for Office ZIP files.
Definition: Driver.java:295
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
final String mimeTypes[]
Command-line parameter shortcuts.
Definition: Driver.java:57
String mergeFile
mergeFile name.
Definition: Driver.java:51
Manages the converter plug-ins that are currently active.
static void addPlugIn(ConverterInfo ci)
Adds a converter plug-in to the registry.
The ConverterInfoReader pulls a META-INF/converter.xml file out of a jar file and parses it,...
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an Enumeration of ConverterInfo objects.
Class for storing the information about a converter plug-in.
A DocumentMerger can merge changes from a modified "Device" Document to the assigned original "Office...
void merge(Document modifiedDoc)
This method will find the changes that had happened in the modifiedDoc Document object given the desi...
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
void write(OutputStream os)
Writes out the Document content to the specified OutputStream.
String getFileName()
Returns the Document name with file extension.
@ Exception
int i
args
Provides an interface for plug-in registration.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19