2
* The Apache Software License, Version 1.1
4
* Copyright (c) 2000 The Apache Software Foundation. All rights
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in
16
* the documentation and/or other materials provided with the
19
* 3. The end-user documentation included with the redistribution, if
20
* any, must include the following acknowlegement:
21
* "This product includes software developed by the
22
* Apache Software Foundation (http://www.apache.org/)."
23
* Alternately, this acknowlegement may appear in the software itself,
24
* if and wherever such third-party acknowlegements normally appear.
26
* 4. The names "The Jakarta Project", "Ant", and "Apache Software
27
* Foundation" must not be used to endorse or promote products derived
28
* from this software without prior written permission. For written
29
* permission, please contact apache@apache.org.
31
* 5. Products derived from this software may not be called "Apache"
32
* nor may "Apache" appear in their names without prior written
33
* permission of the Apache Group.
35
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47
* ====================================================================
49
* This software consists of voluntary contributions made by many
50
* individuals on behalf of the Apache Software Foundation. For more
51
* information on the Apache Software Foundation, please see
52
* <http://www.apache.org/>.
55
package org.apache.tools.ant.types;
57
import org.apache.tools.ant.BuildException;
59
import java.util.Vector;
60
import java.util.StringTokenizer;
64
* Commandline objects help handling command lines specifying processes to
67
* The class can be used to define a command line as nested elements or as a
68
* helper to define a command line by an application.
71
* <someelement><br>
72
* <acommandline executable="/executable/to/run"><br>
73
* <argument value="argument 1" /><br>
74
* <argument line="argument_1 argument_2 argument_3" /><br>
75
* <argument value="argument 4" /><br>
76
* </acommandline><br>
77
* </someelement><br>
79
* The element <code>someelement</code> must provide a method
80
* <code>createAcommandline</code> which returns an instance of this class.
82
* @author thomas.haas@softwired-inc.com
83
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
85
public class Commandline implements Cloneable {
87
private Vector arguments = new Vector();
88
private String executable = null;
90
public Commandline(String to_process) {
92
String[] tmp = translateCommandline(to_process);
93
if (tmp != null && tmp.length > 0) {
94
setExecutable(tmp[0]);
95
for (int i=1; i<tmp.length; i++) {
96
createArgument().setValue(tmp[i]);
101
public Commandline() {
106
* Used for nested xml command line definitions.
108
public class Argument {
110
private String[] parts;
113
* Sets a single commandline argument.
115
* @param value a single commandline argument.
117
public void setValue(String value) {
118
parts = new String[] {value};
122
* Line to split into several commandline arguments.
124
* @param line line to split into several commandline arguments
126
public void setLine(String line) {
127
parts = translateCommandline(line);
131
* Sets a single commandline argument and treats it like a
132
* PATH - ensures the right separator for the local platform
135
* @param value a single commandline argument.
137
public void setPath(Path value) {
138
parts = new String[] {value.toString()};
142
* Sets a single commandline argument to the absolute filename
145
* @param value a single commandline argument.
147
public void setFile(File value) {
148
parts = new String[] {value.getAbsolutePath()};
152
* Returns the parts this Argument consists of.
154
public String[] getParts() {
160
* Class to keep track of the position of an Argument.
162
// <p>This class is there to support the srcfile and targetfile
163
// elements of <execon> and <transform> - don't know
164
// whether there might be additional use cases.</p> --SB
165
public class Marker {
167
private int position;
168
private int realPos = -1;
170
Marker(int position) {
171
this.position = position;
175
* Return the number of arguments that preceeded this marker.
177
* <p>The name of the executable - if set - is counted as the
178
* very first argument.</p>
180
public int getPosition() {
182
realPos = (executable == null ? 0 : 1);
183
for (int i=0; i<position; i++) {
184
Argument arg = (Argument) arguments.elementAt(i);
185
realPos += arg.getParts().length;
193
* Creates an argument object.
194
* Each commandline object has at most one instance of the argument class.
195
* @return the argument object.
197
public Argument createArgument() {
198
Argument argument = new Argument();
199
arguments.addElement(argument);
205
* Sets the executable to run.
207
public void setExecutable(String executable) {
208
if (executable == null || executable.length() == 0) return;
209
this.executable = executable.replace('/', File.separatorChar)
210
.replace('\\', File.separatorChar);
214
public String getExecutable() {
219
public void addArguments(String[] line) {
220
for (int i=0; i < line.length; i++) {
221
createArgument().setValue(line[i]);
226
* Returns the executable and all defined arguments.
228
public String[] getCommandline() {
229
final String[] args = getArguments();
230
if (executable == null) return args;
231
final String[] result = new String[args.length+1];
232
result[0] = executable;
233
System.arraycopy(args, 0, result, 1, args.length);
239
* Returns all arguments defined by <code>addLine</code>,
240
* <code>addValue</code> or the argument object.
242
public String[] getArguments() {
243
Vector result = new Vector(arguments.size()*2);
244
for (int i=0; i<arguments.size(); i++) {
245
Argument arg = (Argument) arguments.elementAt(i);
246
String[] s = arg.getParts();
247
for (int j=0; j<s.length; j++) {
248
result.addElement(s[j]);
252
String [] res = new String[result.size()];
253
result.copyInto(res);
258
public String toString() {
259
return toString(getCommandline());
263
* Put quotes around the given String if necessary.
265
* <p>If the argument doesn't include spaces or quotes, return it
266
* as is. If it contains double quotes, use single quotes - else
267
* surround the argument by double quotes.</p>
269
* @exception BuildException if the argument contains both, single
272
public static String quoteArgument(String argument) {
273
if (argument.indexOf("\"") > -1) {
274
if (argument.indexOf("\'") > -1) {
275
throw new BuildException("Can\'t handle single and double quotes in same argument");
277
return '\''+argument+'\'';
279
} else if (argument.indexOf("\'") > -1 || argument.indexOf(" ") > -1) {
280
return '\"'+argument+'\"';
286
public static String toString(String [] line) {
287
// empty path return empty string
288
if (line == null || line.length == 0) return "";
290
// path containing one or more elements
291
final StringBuffer result = new StringBuffer();
292
for (int i=0; i < line.length; i++) {
296
result.append(quoteArgument(line[i]));
298
return result.toString();
301
public static String[] translateCommandline(String to_process) {
302
if (to_process == null || to_process.length() == 0) {
303
return new String[0];
306
// parse with a simple finite state machine
308
final int normal = 0;
309
final int inQuote = 1;
310
final int inDoubleQuote = 2;
312
StringTokenizer tok = new StringTokenizer(to_process, "\"\' ", true);
313
Vector v = new Vector();
314
StringBuffer current = new StringBuffer();
316
while (tok.hasMoreTokens()) {
317
String nextTok = tok.nextToken();
320
if ("\'".equals(nextTok)) {
323
current.append(nextTok);
327
if ("\"".equals(nextTok)) {
330
current.append(nextTok);
334
if ("\'".equals(nextTok)) {
336
} else if ("\"".equals(nextTok)) {
337
state = inDoubleQuote;
338
} else if (" ".equals(nextTok)) {
339
if (current.length() != 0) {
340
v.addElement(current.toString());
341
current.setLength(0);
344
current.append(nextTok);
350
if (current.length() != 0) {
351
v.addElement(current.toString());
354
if (state == inQuote || state == inDoubleQuote) {
355
throw new BuildException("unbalanced quotes in " + to_process);
358
String[] args = new String[v.size()];
364
return getCommandline().length;
367
public Object clone() {
368
Commandline c = new Commandline();
369
c.setExecutable(executable);
370
c.addArguments(getArguments());
375
* Clear out the whole command line. */
376
public void clear() {
378
arguments.removeAllElements();
382
* Clear out the arguments but leave the executable in place for another operation.
384
public void clearArgs() {
385
arguments.removeAllElements();
391
* <p>This marker can be used to locate a position on the
392
* commandline - to insert something for example - when all
393
* parameters have been set.</p>
395
public Marker createMarker() {
396
return new Marker(arguments.size());