From a05efd43d4ffe005f66144656e62496fce1016c2 Mon Sep 17 00:00:00 2001 From: Thomas Becker Date: Mon, 10 Jun 2013 18:01:07 +0200 Subject: 408904 Enhance CommandlineBuilder to not escape strings inside single quotes --- .../java/org/eclipse/jetty/start/CommandLineBuilder.java | 11 +++++++++-- .../org/eclipse/jetty/start/CommandLineBuilderTest.java | 16 +++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/jetty-start/src/main/java/org/eclipse/jetty/start/CommandLineBuilder.java b/jetty-start/src/main/java/org/eclipse/jetty/start/CommandLineBuilder.java index 16242ff5aa..d50a4b70c1 100644 --- a/jetty-start/src/main/java/org/eclipse/jetty/start/CommandLineBuilder.java +++ b/jetty-start/src/main/java/org/eclipse/jetty/start/CommandLineBuilder.java @@ -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); } diff --git a/jetty-start/src/test/java/org/eclipse/jetty/start/CommandLineBuilderTest.java b/jetty-start/src/test/java/org/eclipse/jetty/start/CommandLineBuilderTest.java index 967e3691fd..562d408f87 100644 --- a/jetty-start/src/test/java/org/eclipse/jetty/start/CommandLineBuilderTest.java +++ b/jetty-start/src/test/java/org/eclipse/jetty/start/CommandLineBuilderTest.java @@ -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)); } } -- cgit v1.2.3