1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.Hashtable; 32 import java.util.Enumeration; 33 import java.util.StringTokenizer; 34 35 import com.sun.star.script.framework.container.ScriptEntry; 36 import com.sun.star.script.framework.container.ParcelDescriptor; 37 38 import org.openoffice.idesupport.zip.ParcelZipper; 39 import org.openoffice.idesupport.filter.AllFilesFilter; 40 import com.sun.star.script.framework.container.XMLParserFactory; 41 import org.openoffice.idesupport.*; 42 43 public class CommandLineTools { 44 private static final String PARCEL_XML_FILE = 45 ParcelZipper.PARCEL_DESCRIPTOR_XML; 46 47 private static String officePath = null; 48 49 public static void main(String[] args) { 50 CommandLineTools driver = new CommandLineTools(); 51 Command command = driver.parseArgs(args); 52 53 // Get the URL for the Office DTD directory and pass it to the 54 // XMLParserFactory so that Office xml files can be parsed 55 if (officePath == null) 56 { 57 try { 58 SVersionRCFile sv = SVersionRCFile.createInstance(); 59 if (sv.getDefaultVersion() != null) 60 { 61 officePath = sv.getDefaultVersion().getPath(); 62 } 63 } 64 catch (IOException ioe) { 65 System.err.println("Error getting Office directory"); 66 } 67 } 68 69 if (officePath == null) 70 { 71 driver.fatalUsage("Error: Office Installation path not set"); 72 } 73 74 File officeDir = new File(officePath); 75 if (officeDir.exists() == false || officeDir.isDirectory() == false) 76 { 77 driver.fatalUsage( 78 "Error: Office Installation path not valid: " + officePath); 79 } 80 81 OfficeInstallation oi = new OfficeInstallation(officePath); 82 String url = oi.getURL("share/dtd/officedocument/1_0/"); 83 XMLParserFactory.setOfficeDTDURL(url); 84 85 if (command == null) 86 driver.printUsage(); 87 else { 88 try { 89 command.execute(); 90 } 91 catch (Exception e) { 92 driver.fatal("Error: " + e.getMessage()); 93 } 94 } 95 } 96 97 private interface Command { 98 public void execute() throws Exception; 99 } 100 101 private void printUsage() { 102 System.out.println("java " + getClass().getName() + " -h " + 103 "prints this message"); 104 System.out.println("java " + getClass().getName() + 105 " [-o Path to Office Installation] " + 106 "-d <script parcel zip file> " + 107 "<destination document or directory>"); 108 System.out.println("java " + getClass().getName() + 109 " [-o Path to Office Installation] " + 110 "-g [parcel root directory] [options] [script names]"); 111 System.out.println("options:"); 112 System.out.println("\t[-l language[=supported extension 1[" + 113 File.pathSeparator + "supported extension 2]]]"); 114 System.out.println("\t[-p name=value]"); 115 System.out.println("\t[-v]"); 116 } 117 118 private void fatal(String message) { 119 System.err.println(message); 120 System.exit(-1); 121 } 122 123 private void fatalUsage(String message) { 124 System.err.println(message); 125 System.err.println(); 126 printUsage(); 127 System.exit(-1); 128 } 129 private Command parseArgs(String[] args) { 130 131 if (args.length < 1) { 132 return null; 133 } 134 else if (args[0].equals("-h")) { 135 return new Command() { 136 public void execute() { 137 printUsage(); 138 } 139 }; 140 } 141 142 int i = 0; 143 144 if(args[0].equals("-o")) { 145 officePath = args[i+1]; 146 i += 2; 147 } 148 149 if(args[i].equals("-d")) { 150 if ((args.length - i) != 3) 151 return null; 152 else 153 return new DeployCommand(args[i+1], args[i+2]); 154 } 155 else if(args[i].equals("-g")) { 156 157 if ((args.length - i) == 1) 158 return new GenerateCommand(System.getProperty("user.dir")); 159 160 GenerateCommand command; 161 i++; 162 if (!args[i].startsWith("-")) 163 command = new GenerateCommand(args[i++]); 164 else 165 command = new GenerateCommand(System.getProperty("user.dir")); 166 167 for (; i < args.length; i++) { 168 if (args[i].equals("-l")) { 169 command.setLanguage(args[++i]); 170 } 171 else if (args[i].equals("-p")) { 172 command.addProperty(args[++i]); 173 } 174 else if (args[i].equals("-v")) { 175 command.setVerbose(); 176 } 177 else { 178 command.addScript(args[i]); 179 } 180 } 181 return command; 182 } 183 return null; 184 } 185 186 private static class GenerateCommand implements Command { 187 188 private File basedir, contents, parcelxml; 189 private boolean verbose = false; 190 private String language = null; 191 private MethodFinder finder = null; 192 private ArrayList scripts = null; 193 private Hashtable properties = new Hashtable(3); 194 195 public GenerateCommand(String basedir) { 196 this.basedir = new File(basedir); 197 this.contents = new File(basedir, ParcelZipper.CONTENTS_DIRNAME); 198 this.parcelxml = new File(contents, PARCEL_XML_FILE); 199 } 200 201 public void setLanguage(String language) { 202 StringTokenizer tokenizer = new StringTokenizer(language, "="); 203 this.language = tokenizer.nextToken(); 204 205 if (this.language.toLowerCase().equals("java")) { 206 this.finder = JavaFinder.getInstance(); 207 return; 208 } 209 210 if (tokenizer.hasMoreTokens()) { 211 String ext = (String)tokenizer.nextToken(); 212 String[] extensions; 213 214 if (ext.indexOf(File.pathSeparator) != -1) { 215 tokenizer = new StringTokenizer(ext, File.pathSeparator); 216 extensions = new String[tokenizer.countTokens()]; 217 int i = 0; 218 219 while(tokenizer.hasMoreTokens()) 220 extensions[i++] = (String)tokenizer.nextToken(); 221 } 222 else { 223 extensions = new String[1]; 224 extensions[0] = ext; 225 } 226 this.finder = new ExtensionFinder(this.language, extensions); 227 } 228 } 229 230 public void addProperty(String prop) { 231 StringTokenizer tok = new StringTokenizer(prop, "="); 232 233 if (tok.countTokens() != 2) 234 return; 235 236 String name = tok.nextToken(); 237 String value = tok.nextToken(); 238 239 properties.put(name, value); 240 } 241 242 public void setVerbose() { 243 verbose = true; 244 } 245 246 public void addScript(String script) { 247 if (language == null) 248 return; 249 250 addScript(new ScriptEntry(language, script, script, basedir.getName())); 251 } 252 253 public void addScript(ScriptEntry entry) { 254 if (scripts == null) 255 scripts = new ArrayList(3); 256 scripts.add(entry); 257 } 258 259 public void execute() throws Exception { 260 261 if (basedir.isDirectory() != true) { 262 throw new Exception(basedir.getName() + " is not a directory"); 263 } 264 else if (contents.exists() != true) { 265 throw new Exception(basedir.getName() + 266 " does not contain a Contents directory"); 267 } 268 269 if (language == null && parcelxml.exists() == false) { 270 throw new Exception(parcelxml.getName() + " not found and language " + 271 "not specified"); 272 } 273 274 if (language != null && parcelxml.exists() == true) { 275 ParcelDescriptor desc; 276 String desclang = ""; 277 278 desc = new ParcelDescriptor(parcelxml); 279 desclang = desc.getLanguage().toLowerCase(); 280 281 if (!desclang.equals(language.toLowerCase())) 282 throw new Exception(parcelxml.getName() + " already exists, " + 283 "and has a different language attribute: " + 284 desc.getLanguage()); 285 } 286 287 if (language != null && scripts == null) { 288 if (finder == null) 289 throw new Exception("Extension list not specified for this language"); 290 291 log("Searching for " + language + " scripts"); 292 293 ScriptEntry[] entries = finder.findMethods(contents); 294 for (int i = 0; i < entries.length; i++) { 295 addScript(entries[i]); 296 log("Found: " + entries[i].getLogicalName()); 297 } 298 } 299 300 if (scripts != null) { 301 if (scripts.size() == 0) 302 throw new Exception("No valid scripts found"); 303 304 ParcelDescriptor desc = new ParcelDescriptor(parcelxml, language); 305 desc.setScriptEntries((ScriptEntry[])scripts.toArray(new ScriptEntry[0])); 306 if (properties.size() != 0) { 307 Enumeration enumer = properties.keys(); 308 309 while (enumer.hasMoreElements()) { 310 String name = (String)enumer.nextElement(); 311 String value = (String)properties.get(name); 312 log("Setting property: " + name + " to " + value); 313 314 desc.setLanguageProperty(name, value); 315 } 316 } 317 desc.write(); 318 } 319 else { 320 if (parcelxml.exists() == false) 321 throw new Exception("No valid scripts found"); 322 } 323 324 contents = new File(contents.getAbsolutePath()); 325 String name = ParcelZipper.getParcelZipper().zipParcel(contents, AllFilesFilter.getInstance()); 326 System.out.println(name + " generated"); 327 } 328 329 private void log(String message) { 330 if (verbose) 331 System.out.println(message); 332 } 333 } 334 335 private static class DeployCommand implements Command { 336 337 File source, target; 338 339 public DeployCommand(String source, String target) { 340 this.source = new File(source); 341 this.target = new File(target); 342 } 343 344 public void execute() throws Exception { 345 ParcelZipper.getParcelZipper().deployParcel(source, target); 346 System.out.println(source.getName() + 347 " successfully deployed to " + target.getAbsolutePath()); 348 } 349 } 350 } 351