View Javadoc

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