1 //
2 // ========================================================================
3 // Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.start;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 public class CommandLineBuilder
25 {
26 private List<String> args;
27
28 public CommandLineBuilder(String bin)
29 {
30 args = new ArrayList<String>();
31 args.add(bin);
32 }
33
34 /**
35 * Add a simple argument to the command line.
36 * <p>
37 * Will quote arguments that have a space in them.
38 *
39 * @param arg
40 * the simple argument to add
41 */
42 public void addArg(String arg)
43 {
44 if (arg != null)
45 args.add(quote(arg));
46 }
47
48 /**
49 * Similar to {@link #addArg(String)} but concats both name + value with an "=" sign, quoting were needed, and excluding the "=" portion if the value is
50 * undefined or empty.
51 * <p>
52 *
53 * <pre>
54 * addEqualsArg("-Dname", "value") = "-Dname=value"
55 * addEqualsArg("-Djetty.home", "/opt/company inc/jetty (7)/") = "-Djetty.home=/opt/company\ inc/jetty\ (7)/"
56 * addEqualsArg("-Djenkins.workspace", "/opt/workspaces/jetty jdk7/") = "-Djenkins.workspace=/opt/workspaces/jetty\ jdk7/"
57 * addEqualsArg("-Dstress", null) = "-Dstress"
58 * addEqualsArg("-Dstress", "") = "-Dstress"
59 * </pre>
60 *
61 * @param name
62 * the name
63 * @param value
64 * the value
65 */
66 public void addEqualsArg(String name, String value)
67 {
68 if (value != null && value.length() > 0)
69 {
70 args.add(quote(name + "=" + value));
71 }
72 else
73 {
74 args.add(quote(name));
75 }
76 }
77
78 /**
79 * Add a simple argument to the command line.
80 * <p>
81 * Will <b>NOT</b> quote/escape arguments that have a space in them.
82 *
83 * @param arg
84 * the simple argument to add
85 */
86 public void addRawArg(String arg)
87 {
88 if (arg != null)
89 args.add(arg);
90 }
91
92 public List<String> getArgs()
93 {
94 return args;
95 }
96
97 /**
98 * Perform an optional quoting of the argument, being intelligent with spaces and quotes as needed.
99 *
100 * @param arg
101 * @return
102 */
103 public static String quote(String arg)
104 {
105 boolean needsQuoting = arg.indexOf(' ') >= 0 || arg.indexOf('"') >= 0;
106 if (!needsQuoting)
107 {
108 return arg;
109 }
110 StringBuilder buf = new StringBuilder();
111 // buf.append('"');
112 boolean escaped = false;
113 for (char c : arg.toCharArray())
114 {
115 if (!escaped && ((c == '"') || (c == ' ')))
116 {
117 buf.append("\\");
118 }
119 escaped = (c == '\\');
120 buf.append(c);
121 }
122 // buf.append('"');
123 return buf.toString();
124 }
125
126 @Override
127 public String toString()
128 {
129 StringBuilder buf = new StringBuilder();
130
131 boolean delim = false;
132 for (String arg : args)
133 {
134 if (delim)
135 {
136 buf.append(' ');
137 }
138 buf.append(quote(arg));
139 delim = true;
140 }
141
142 return buf.toString();
143 }
144 }