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