fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / scripting / java / org / openoffice / idesupport / CommandLineTools.java
blob5cb8d5f51b5571bc1ca71fdcddbc56e045064b19
1 /*
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 .
19 import java.io.File;
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)
48 try {
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)
68 driver.fatalUsage(
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);
76 if (command == null)
77 driver.printUsage();
78 else {
79 try {
80 command.execute();
82 catch (Exception e) {
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);
111 System.exit(-1);
114 private void fatalUsage(String message) {
115 System.err.println(message);
116 System.err.println();
117 printUsage();
118 System.exit(-1);
120 private Command parseArgs(String[] args) {
122 if (args.length < 1) {
123 return null;
125 else if (args[0].equals("-h")) {
126 return new Command() {
127 public void execute() {
128 printUsage();
133 int i = 0;
135 if(args[0].equals("-o")) {
136 officePath = args[i+1];
137 i += 2;
140 if(args[i].equals("-d")) {
141 if ((args.length - i) != 3)
142 return null;
143 else
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;
152 i++;
153 if (!args[i].startsWith("-"))
154 command = new GenerateCommand(args[i++]);
155 else
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();
168 else {
169 command.addScript(args[i]);
172 return command;
174 return null;
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();
198 return;
201 if (tokenizer.hasMoreTokens()) {
202 String ext = (String)tokenizer.nextToken();
203 String[] extensions;
205 if (ext.indexOf(File.pathSeparator) != -1) {
206 tokenizer = new StringTokenizer(ext, File.pathSeparator);
207 extensions = new String[tokenizer.countTokens()];
208 int i = 0;
210 while(tokenizer.hasMoreTokens())
211 extensions[i++] = (String)tokenizer.nextToken();
213 else {
214 extensions = new String[1];
215 extensions[0] = ext;
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)
225 return;
227 String name = tok.nextToken();
228 String value = tok.nextToken();
230 properties.put(name, value);
233 public void setVerbose() {
234 verbose = true;
237 public void addScript(String script) {
238 if (language == null)
239 return;
241 addScript(new ScriptEntry(language, script, script, basedir.getName()));
244 public void addScript(ScriptEntry entry) {
245 if (scripts == null)
246 scripts = new ArrayList(3);
247 scripts.add(entry);
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 " +
262 "not specified");
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: " +
275 desc.getLanguage());
278 if (language != null && scripts == null) {
279 if (finder == 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);
308 desc.write();
310 else {
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) {
321 if (verbose)
322 System.out.println(message);
326 private static class DeployCommand implements Command {
328 File source, target;
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());