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 27 import org.openoffice.xmerge.Convert; 28 import org.openoffice.xmerge.ConvertData; 29 import org.openoffice.xmerge.ConverterFactory; 30 import org.openoffice.xmerge.Document; 31 import org.openoffice.xmerge.DocumentMerger; 32 import org.openoffice.xmerge.util.registry.ConverterInfo; 33 import org.openoffice.xmerge.util.registry.ConverterInfoMgr; 34 import org.openoffice.xmerge.util.registry.ConverterInfoReader; 35 36 /** 37 * This class is a command-line driver for the converter framework. 38 * 39 * <p>It is expected that this code will be later called by the device server. 40 * It does some basic validation of the command-line parameters.</p> 41 */ 42 public final class Driver { 43 44 /** Command-line parameter. */ 45 private String fromMime = null; 46 47 /** Command-line parameter. */ 48 private String toMime = null; 49 50 /** {@code mergeFile} name. */ 51 private String mergeFile = null; 52 53 /** Command-line parameter. */ 54 private final ArrayList<String> deviceFiles = new ArrayList<String>(); 55 56 /** Command-line parameter shortcuts. */ 57 private final String mimeTypes[] = { 58 "sxc", "staroffice/sxc", 59 "sxw","staroffice/sxw" 60 }; 61 62 /** 63 * Main. 64 * 65 * @param args The argument passed on the command line. 66 */ main(String args[])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 close(FileOutputStream c)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 112 /** 113 * Gets a {@code Convert} object using the {@code ConverterFactory} and does 114 * the conversion using this object. 115 * 116 * @throws IllegalArgumentException If an argument is invalid. 117 */ doConversion()118 private void doConversion() throws IllegalArgumentException { 119 120 ConverterFactory cf = new ConverterFactory(); 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 205 /** 206 * Display usage. 207 */ showUsage()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 215 /** 216 * Parse command-line arguments. 217 * 218 * @param args Array of command line arguments. 219 * 220 * @throws IllegalArgumentException If an argument is invalid. 221 */ parseCommandLine(String args[])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 264 /** 265 * Extract the next argument from the array, while checking to see that the 266 * array size is not exceeded. 267 * 268 * <p>Throw a friendly error message in case the {@code args[i+1]} is 269 * missing.</p> 270 * 271 * @param i Argument index. 272 * @param args Array of command line arguments. 273 * 274 * @return The argument with the specified index. 275 * 276 * @throws IllegalArgumentException If an argument is invalid. 277 */ extractArg(int i, String args[])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 288 /** 289 * Simple validation for Office ZIP files. 290 * 291 * @param zipName The name of the ZIP file. 292 * 293 * @return {@code true} if {@code zipName} is valid, {@code false} otherwise. 294 */ isZip(String zipName)295 private boolean isZip(String zipName) { 296 297 String str = zipName.toLowerCase(); 298 return str.endsWith("sxw") || zipName.endsWith("sxc"); 299 } 300 } 301