408904 Enhance CommandlineBuilder to not escape strings inside single quotes

This commit is contained in:
Thomas Becker 2013-06-10 18:01:07 +02:00 committed by Joakim Erdfelt
parent 00744e42fc
commit a05efd43d4
2 changed files with 20 additions and 7 deletions

View File

@ -95,7 +95,8 @@ public class CommandLineBuilder
}
/**
* Perform an optional quoting of the argument, being intelligent with spaces and quotes as needed.
* Perform an optional quoting of the argument, being intelligent with spaces and quotes as needed. If a
* subString is set in quotes it won't the subString won't be escaped.
*
* @param arg
* @return
@ -110,12 +111,18 @@ public class CommandLineBuilder
StringBuilder buf = new StringBuilder();
// buf.append('"');
boolean escaped = false;
boolean quoted = false;
for (char c : arg.toCharArray())
{
if (!escaped && ((c == '"') || (c == ' ')))
if (!quoted && !escaped && ((c == '"') || (c == ' ')))
{
buf.append("\\");
}
// don't quote text in single quotes
if (!escaped && c == '\'')
{
quoted = !quoted;
}
escaped = (c == '\\');
buf.append(c);
}

View File

@ -18,12 +18,12 @@
package org.eclipse.jetty.start;
import static org.hamcrest.Matchers.is;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class CommandLineBuilderTest
{
private CommandLineBuilder cmd = new CommandLineBuilder("java");
@ -38,7 +38,7 @@ public class CommandLineBuilderTest
@Test
public void testSimpleCommandline()
{
Assert.assertThat(cmd.toString(),is("java -Djava.io.tmpdir=/home/java/temp\\ dir/ --version"));
assertThat(cmd.toString(), is("java -Djava.io.tmpdir=/home/java/temp\\ dir/ --version"));
}
@Test
@ -65,9 +65,15 @@ public class CommandLineBuilderTest
System.out.println(cmd.toString());
}
@Test
public void testQuoteQuotationMarks()
{
assertQuoting("-XX:OnOutOfMemoryError='kill -9 %p'","-XX:OnOutOfMemoryError='kill -9 %p'");
}
private void assertQuoting(String raw, String expected)
{
String actual = CommandLineBuilder.quote(raw);
Assert.assertThat("Quoted version of [" + raw + "]",actual,is(expected));
assertThat("Quoted version of [" + raw + "]", actual, is(expected));
}
}