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.test;
25 
26 import java.util.Vector;
27 import java.util.Enumeration;
28 import java.io.FileOutputStream;
29 import java.io.FileInputStream;
30 import java.io.File;
31 
32 import org.openoffice.xmerge.Convert;
33 import org.openoffice.xmerge.Document;
34 import org.openoffice.xmerge.ConvertData;
35 import org.openoffice.xmerge.ConverterFactory;
36 import org.openoffice.xmerge.test.ConverterInfoList;
37 import org.openoffice.xmerge.util.registry.ConverterInfoMgr;
38 import org.openoffice.xmerge.util.registry.ConverterInfoReader;
39 import org.openoffice.xmerge.DocumentMerger;
40 
41 /**
42  *  This class is a command-line driver for the converter framework.
43  *  It is expected that this code will be later called by the device
44  *  server. It does some basic validation of the command-line
45  *  parameters.
46  */
47 public final class Driver {
48 
49     /**  Command-line parameter. */
50     private String fromMime = null;
51 
52     /**  Command-line parameter. */
53     private String toMime = null;
54 
55     /**  mergeFile name. */
56     private String mergeFile = null;
57 
58     /**  Command-line parmeter. */
59     private Vector deviceFiles = new Vector();
60 
61     /**  Command-line parmeter shortcuts. */
62     private String mimeTypes[] = {
63                                     "sxc", "staroffice/sxc",
64                                     "sxw","staroffice/sxw"
65     };
66 
67 
68     /**
69      *  Main.
70      *
71      *  @param  args  The argument passed on the command line.
72      */
main(String args[])73     public static void main(String args[]) {
74 
75         // Register jarfiles
76         //
77         String propFile       = "ConverterInfoList.properties";
78         ConverterInfoList cil = null;
79         try {
80             cil = new ConverterInfoList(propFile);
81         } catch (Exception e) {
82             System.out.println("\nCannot not load " + propFile +
83                 " property file");
84         }
85 
86         Enumeration jarInfoEnumeration;
87         ConverterInfoReader cir;
88 
89         Enumeration jarFileEnum = cil.getJarFileEnum();
90         while (jarFileEnum.hasMoreElements()) {
91             String jarName = (String) jarFileEnum.nextElement();
92             try {
93                 cir = new ConverterInfoReader(jarName, false);
94                 jarInfoEnumeration = cir.getConverterInfoEnumeration();
95                 ConverterInfoMgr.addPlugIn(jarInfoEnumeration);
96             } catch (Exception e) {
97                 System.out.println("\nCannot not load <" + jarName +
98                     "> from the <" + propFile + "> property file");
99             }
100         }
101 
102         try {
103 
104             Driver app = new Driver();
105             app.parseCommandLine(args);
106             app.doConversion();
107         } catch (IllegalArgumentException ex) {
108 
109             String msg = ex.getMessage();
110             if (msg != null) System.out.println("\n" + msg);
111             showUsage();
112 
113         } catch (Exception ex) {
114 
115             String msg = ex.getMessage();
116             if (msg != null) System.out.println("\n" + msg);
117             ex.printStackTrace();
118         }
119     }
120 
121 
122     /**
123      *  Gets a <code>Convert</code> object using the
124      *  <code>ConverterFactory</code> and  does the conversion using
125      *  this object.
126      *
127      *  @throws  IllegalArgumentException  If an argument is invalid.
128      */
doConversion()129     private void doConversion() throws IllegalArgumentException {
130 
131         ConverterFactory cf = new ConverterFactory();
132         Convert myConvert   = cf.getConverter(fromMime, toMime);
133         String processFile  = null;
134 
135         if (myConvert == null) {
136             System.out.println("\nNo plug-in exists to convert from <" +
137                 fromMime + "> to <" + toMime + ">");
138             throw new IllegalArgumentException();
139         }
140 
141         try {
142             Enumeration dfEnum = deviceFiles.elements();
143             while (dfEnum.hasMoreElements()) {
144                 processFile = (String)dfEnum.nextElement();
145                 File f = new File(processFile);
146 
147                 // Make sure the input file actually exists before using it
148                 if (!f.exists()) {
149                     System.out.println(processFile + " does not exist!");
150                     System.exit(0);
151                 }
152                 FileInputStream fis = new FileInputStream(f);
153                 myConvert.addInputStream(f.getName(), fis);
154             }
155         } catch (Exception addExcept) {
156             System.out.println("\nFile <" + processFile + "> is not in <" +
157                 fromMime + "> format");
158             throw new IllegalArgumentException();
159         }
160 
161         ConvertData dataOut = null;
162 
163         try {
164             dataOut = myConvert.convert();
165         } catch (Exception convertExcept) {
166             System.out.println("\nThere was an error in the conversion");
167             convertExcept.printStackTrace();
168         }
169 
170         if (dataOut != null ) {
171 
172             if (mergeFile == null) {
173                 Enumeration docEnum = dataOut.getDocumentEnumeration();
174                 while (docEnum.hasMoreElements()) {
175                     Document docOut      = (Document)docEnum.nextElement();
176                     String fileName      = docOut.getFileName();
177                     try {
178                         FileOutputStream fos = new FileOutputStream(fileName);
179                         docOut.write(fos);
180                         fos.flush();
181                         fos.close();
182                     } catch (Exception writeExcept) {
183                         System.out.println("\nThere was an writing out file <" +
184                             fileName + ">");
185                         writeExcept.printStackTrace();
186                     }
187                 }
188             } else {
189                 try {
190                     FileInputStream mergeIS = new FileInputStream(mergeFile);
191                     Document mergeDoc = myConvert.getOfficeDocument(mergeFile, mergeIS);
192                     DocumentMerger merger = myConvert.getDocumentMerger(mergeDoc);
193                     Enumeration mergeDocEnum = dataOut.getDocumentEnumeration();
194                     Document convertedFile = (Document)mergeDocEnum.nextElement();
195 
196                     merger.merge(convertedFile);
197 		      mergeIS.close();
198 
199                     FileOutputStream fos = new FileOutputStream(mergeFile);
200                     mergeDoc.write(fos);
201                     fos.flush();
202                     fos.close();
203                 } catch (Exception mergeExcept) {
204                     System.out.println("\nThere was an error in the merge");
205                     mergeExcept.printStackTrace();
206                 }
207             }
208         }
209     }
210 
211 
212     /**
213      *  Display usage.
214      */
showUsage()215     private static void showUsage() {
216 
217         System.out.println("\nUsage:");
218         System.out.println("\n   java org.openoffice.xmerge.test.Driver <args>");
219         System.out.println("\n   where <args> is as follows:");
220         System.out.println("   -from <MIMETYPE> -to <MIMETYPE> [ -merge <OrigDoc ] <document>\n");
221     }
222 
223 
224     /**
225      *  Parse command-line arguments.
226      *
227      *  @param  args  Array of command line arguments.
228      *
229      *  @throws  IllegalArgumentException  If an argument is invalid.
230      */
parseCommandLine(String args[])231     private void parseCommandLine(String args[])
232         throws IllegalArgumentException {
233 
234         if (args.length == 0) {
235             throw new IllegalArgumentException();
236         }
237 
238         for (int i = 0; i < args.length; i++) {
239             String arg = args[i];
240 
241             if ("-to".equals(arg)) {
242                 toMime = extractArg(i, args);
243                 for (int j = 0; j < mimeTypes.length; j+=2) {
244                     if(mimeTypes[j].equals(extractArg(i, args)))
245                         toMime = mimeTypes[j+1];
246                 }
247                 i++;
248             } else if ("-from".equals(arg)) {
249                 fromMime = extractArg(i, args);
250                 for (int j = 0; j < mimeTypes.length; j+=2) {
251                     if(mimeTypes[j].equals(extractArg(i, args)))
252                         fromMime = mimeTypes[j+1];
253                 }
254                 i++;
255             } else if ("-merge".equals(arg)) {
256                 mergeFile = extractArg(i, args);
257                 if (!isZip(mergeFile)) {
258                     throw new
259                         IllegalArgumentException("Arg " + i +
260                                                  ": expected zip, got " +
261                                                  mergeFile);
262                 }
263                 i++;
264             } else {
265                 deviceFiles.add(arg);
266             }
267         }
268 
269         System.out.println("\nConverting from " + fromMime + " to " + toMime +
270                            ((mergeFile != null) ? " with merge " : " "));
271     }
272 
273 
274     /**
275      *  Extract the next argument from the array, while checking to see
276      *  that the array size is not exceeded.  Throw a friendly error
277      *  message in case the arg is missing.
278      *
279      *  @param  i     Argument index.
280      *  @param  args  Array of command line arguments.
281      *
282      *  @return  The argument with the specified index.
283      *
284      *  @throws  IllegalArgumentException  If an argument is invalid.
285      */
extractArg(int i, String args[])286     private String extractArg(int i, String args[])
287         throws IllegalArgumentException {
288 
289         if (i+1 < args.length)
290             return args[i+1];
291         else throw new
292             IllegalArgumentException("Arg " + i +
293                                      ": expected arg for " + args[i]);
294     }
295 
296 
297     /**
298      *  Simple validation for Office ZIP files.
299      *
300      *  @param  zipName  The name of the ZIP file.
301      *
302      *  @return  true if zipName is valid, false otherwise.
303      */
isZip(String zipName)304     private boolean isZip(String zipName) {
305 
306         String str = zipName.toLowerCase();
307         if (str.endsWith("sxw") || zipName.endsWith("sxc")) {
308             return true;
309         }
310 
311         return false;
312     }
313 }
314 
315