1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 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. If a 99 * subString is set in quotes it won't the subString won't be escaped. 100 * 101 * @param arg 102 * @return 103 */ 104 public static String quote(String arg) 105 { 106 boolean needsQuoting = arg.indexOf(' ') >= 0 || arg.indexOf('"') >= 0; 107 if (!needsQuoting) 108 { 109 return arg; 110 } 111 StringBuilder buf = new StringBuilder(); 112 // buf.append('"'); 113 boolean escaped = false; 114 boolean quoted = false; 115 for (char c : arg.toCharArray()) 116 { 117 if (!quoted && !escaped && ((c == '"') || (c == ' '))) 118 { 119 buf.append("\\"); 120 } 121 // don't quote text in single quotes 122 if (!escaped && c == '\'') 123 { 124 quoted = !quoted; 125 } 126 escaped = (c == '\\'); 127 buf.append(c); 128 } 129 // buf.append('"'); 130 return buf.toString(); 131 } 132 133 @Override 134 public String toString() 135 { 136 StringBuilder buf = new StringBuilder(); 137 138 boolean delim = false; 139 for (String arg : args) 140 { 141 if (delim) 142 { 143 buf.append(' '); 144 } 145 buf.append(quote(arg)); 146 delim = true; 147 } 148 149 return buf.toString(); 150 } 151 }