2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Hashtable
;
23 import java
.util
.Enumeration
;
24 import java
.util
.StringTokenizer
;
26 import com
.sun
.star
.script
.framework
.container
.ScriptEntry
;
27 import com
.sun
.star
.script
.framework
.container
.ParcelDescriptor
;
29 import org
.openoffice
.idesupport
.zip
.ParcelZipper
;
30 import org
.openoffice
.idesupport
.filter
.AllFilesFilter
;
31 import com
.sun
.star
.script
.framework
.container
.XMLParserFactory
;
32 import org
.openoffice
.idesupport
.*;
34 public class CommandLineTools
{
35 private static final String PARCEL_XML_FILE
=
36 ParcelZipper
.PARCEL_DESCRIPTOR_XML
;
38 private static String officePath
= null;
40 public static void main(String
[] args
) {
41 CommandLineTools driver
= new CommandLineTools();
42 Command command
= driver
.parseArgs(args
);
44 // Get the URL for the Office DTD directory and pass it to the
45 // XMLParserFactory so that Office xml files can be parsed
46 if (officePath
== null)
49 SVersionRCFile sv
= SVersionRCFile
.createInstance();
50 if (sv
.getDefaultVersion() != null)
52 officePath
= sv
.getDefaultVersion().getPath();
55 catch (IOException ioe
) {
56 System
.err
.println("Error getting Office directory");
60 if (officePath
== null)
62 driver
.fatalUsage("Error: Office Installation path not set");
65 File officeDir
= new File(officePath
);
66 if (officeDir
.exists() == false || officeDir
.isDirectory() == false)
69 "Error: Office Installation path not valid: " + officePath
);
72 OfficeInstallation oi
= new OfficeInstallation(officePath
);
73 String url
= oi
.getURL("share/dtd/officedocument/1_0/");
74 XMLParserFactory
.setOfficeDTDURL(url
);
83 driver
.fatal("Error: " + e
.getMessage());
88 private interface Command
{
89 public void execute() throws Exception
;
92 private void printUsage() {
93 System
.out
.println("java " + getClass().getName() + " -h " +
94 "prints this message");
95 System
.out
.println("java " + getClass().getName() +
96 " [-o Path to Office Installation] " +
97 "-d <script parcel zip file> " +
98 "<destination document or directory>");
99 System
.out
.println("java " + getClass().getName() +
100 " [-o Path to Office Installation] " +
101 "-g [parcel root directory] [options] [script names]");
102 System
.out
.println("options:");
103 System
.out
.println("\t[-l language[=supported extension 1[" +
104 File
.pathSeparator
+ "supported extension 2]]]");
105 System
.out
.println("\t[-p name=value]");
106 System
.out
.println("\t[-v]");
109 private void fatal(String message
) {
110 System
.err
.println(message
);
114 private void fatalUsage(String message
) {
115 System
.err
.println(message
);
116 System
.err
.println();
120 private Command
parseArgs(String
[] args
) {
122 if (args
.length
< 1) {
125 else if (args
[0].equals("-h")) {
126 return new Command() {
127 public void execute() {
135 if(args
[0].equals("-o")) {
136 officePath
= args
[i
+1];
140 if(args
[i
].equals("-d")) {
141 if ((args
.length
- i
) != 3)
144 return new DeployCommand(args
[i
+1], args
[i
+2]);
146 else if(args
[i
].equals("-g")) {
148 if ((args
.length
- i
) == 1)
149 return new GenerateCommand(System
.getProperty("user.dir"));
151 GenerateCommand command
;
153 if (!args
[i
].startsWith("-"))
154 command
= new GenerateCommand(args
[i
++]);
156 command
= new GenerateCommand(System
.getProperty("user.dir"));
158 for (; i
< args
.length
; i
++) {
159 if (args
[i
].equals("-l")) {
160 command
.setLanguage(args
[++i
]);
162 else if (args
[i
].equals("-p")) {
163 command
.addProperty(args
[++i
]);
165 else if (args
[i
].equals("-v")) {
166 command
.setVerbose();
169 command
.addScript(args
[i
]);
177 private static class GenerateCommand
implements Command
{
179 private File basedir
, contents
, parcelxml
;
180 private boolean verbose
= false;
181 private String language
= null;
182 private MethodFinder finder
= null;
183 private ArrayList scripts
= null;
184 private HashMap properties
= new HashMap(3);
186 public GenerateCommand(String basedir
) {
187 this.basedir
= new File(basedir
);
188 this.contents
= new File(basedir
, ParcelZipper
.CONTENTS_DIRNAME
);
189 this.parcelxml
= new File(contents
, PARCEL_XML_FILE
);
192 public void setLanguage(String language
) {
193 StringTokenizer tokenizer
= new StringTokenizer(language
, "=");
194 this.language
= tokenizer
.nextToken();
196 if (this.language
.toLowerCase().equals("java")) {
197 this.finder
= JavaFinder
.getInstance();
201 if (tokenizer
.hasMoreTokens()) {
202 String ext
= (String
)tokenizer
.nextToken();
205 if (ext
.indexOf(File
.pathSeparator
) != -1) {
206 tokenizer
= new StringTokenizer(ext
, File
.pathSeparator
);
207 extensions
= new String
[tokenizer
.countTokens()];
210 while(tokenizer
.hasMoreTokens())
211 extensions
[i
++] = (String
)tokenizer
.nextToken();
214 extensions
= new String
[1];
217 this.finder
= new ExtensionFinder(this.language
, extensions
);
221 public void addProperty(String prop
) {
222 StringTokenizer tok
= new StringTokenizer(prop
, "=");
224 if (tok
.countTokens() != 2)
227 String name
= tok
.nextToken();
228 String value
= tok
.nextToken();
230 properties
.put(name
, value
);
233 public void setVerbose() {
237 public void addScript(String script
) {
238 if (language
== null)
241 addScript(new ScriptEntry(language
, script
, script
, basedir
.getName()));
244 public void addScript(ScriptEntry entry
) {
246 scripts
= new ArrayList(3);
250 public void execute() throws Exception
{
252 if (basedir
.isDirectory() != true) {
253 throw new Exception(basedir
.getName() + " is not a directory");
255 else if (contents
.exists() != true) {
256 throw new Exception(basedir
.getName() +
257 " does not contain a Contents directory");
260 if (language
== null && parcelxml
.exists() == false) {
261 throw new Exception(parcelxml
.getName() + " not found and language " +
265 if (language
!= null && parcelxml
.exists() == true) {
266 ParcelDescriptor desc
;
267 String desclang
= "";
269 desc
= new ParcelDescriptor(parcelxml
);
270 desclang
= desc
.getLanguage().toLowerCase();
272 if (!desclang
.equals(language
.toLowerCase()))
273 throw new Exception(parcelxml
.getName() + " already exists, " +
274 "and has a different language attribute: " +
278 if (language
!= null && scripts
== null) {
280 throw new Exception("Extension list not specified for this language");
282 log("Searching for " + language
+ " scripts");
284 ScriptEntry
[] entries
= finder
.findMethods(contents
);
285 for (int i
= 0; i
< entries
.length
; i
++) {
286 addScript(entries
[i
]);
287 log("Found: " + entries
[i
].getLogicalName());
291 if (scripts
!= null) {
292 if (scripts
.size() == 0)
293 throw new Exception("No valid scripts found");
295 ParcelDescriptor desc
= new ParcelDescriptor(parcelxml
, language
);
296 desc
.setScriptEntries((ScriptEntry
[])scripts
.toArray(new ScriptEntry
[0]));
297 if (properties
.size() != 0) {
298 Enumeration enumer
= properties
.keys();
300 while (enumer
.hasMoreElements()) {
301 String name
= (String
)enumer
.nextElement();
302 String value
= (String
)properties
.get(name
);
303 log("Setting property: " + name
+ " to " + value
);
305 desc
.setLanguageProperty(name
, value
);
311 if (parcelxml
.exists() == false)
312 throw new Exception("No valid scripts found");
315 contents
= new File(contents
.getAbsolutePath());
316 String name
= ParcelZipper
.getParcelZipper().zipParcel(contents
, AllFilesFilter
.getInstance());
317 System
.out
.println(name
+ " generated");
320 private void log(String message
) {
322 System
.out
.println(message
);
326 private static class DeployCommand
implements Command
{
330 public DeployCommand(String source
, String target
) {
331 this.source
= new File(source
);
332 this.target
= new File(target
);
335 public void execute() throws Exception
{
336 ParcelZipper
.getParcelZipper().deployParcel(source
, target
);
337 System
.out
.println(source
.getName() +
338 " successfully deployed to " + target
.getAbsolutePath());