This is an automated email from the ASF dual-hosted git repository.
lkishalmi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 0d9e6cc [NETBEANS-3648] Support single quotes in parameter parsing
0d9e6cc is described below
commit 0d9e6cc6dfc456a40a5cd9d335c92f5820d81b3b
Author: Laszlo Kishalmi <[email protected]>
AuthorDate: Sun Dec 29 09:24:57 2019 -0800
[NETBEANS-3648] Support single quotes in parameter parsing
---
.../src/org/openide/util/BaseUtilities.java | 172 ++++++++-------------
.../src/org/openide/util/BaseUtilitiesTest.java | 61 +++++++-
2 files changed, 121 insertions(+), 112 deletions(-)
diff --git a/platform/openide.util/src/org/openide/util/BaseUtilities.java
b/platform/openide.util/src/org/openide/util/BaseUtilities.java
index 9dfb1f7..2fda5b5 100644
--- a/platform/openide.util/src/org/openide/util/BaseUtilities.java
+++ b/platform/openide.util/src/org/openide/util/BaseUtilities.java
@@ -1036,12 +1036,11 @@ widthcheck: {
* @return an array of parameters
*/
public static String[] parseParameters(String s) {
- int NULL = 0x0; // STICK + whitespace or NULL + non_"
- int INPARAM = 0x1; // NULL + " or STICK + " or INPARAMPENDING + "\ //
NOI18N
- int INPARAMPENDING = 0x2; // INPARAM + \
- int STICK = 0x4; // INPARAM + " or STICK + non_" // NOI18N
- int STICKPENDING = 0x8; // STICK + \
- ArrayList<String> params = new ArrayList<String>(5);
+ final int NULL = 0x0;
+ final int IN_PARAM = 0x1;
+ final int IN_DOUBLE_QUOTE = 0x2;
+ final int IN_SINGLE_QUOTE = 0x3;
+ ArrayList<String> params = new ArrayList<>(5);
char c;
int state = NULL;
@@ -1050,117 +1049,72 @@ widthcheck: {
for (int i = 0; i < slength; i++) {
c = s.charAt(i);
-
- if (Character.isWhitespace(c)) {
- if (state == NULL) {
- if (buff.length() > 0) {
- params.add(buff.toString());
- buff.setLength(0);
+ switch (state) {
+ case NULL:
+ switch (c) {
+ case '\'':
+ state = IN_SINGLE_QUOTE;
+ break;
+ case '"':
+ state = IN_DOUBLE_QUOTE;
+ break;
+ default:
+ if (!Character.isWhitespace(c)) {
+ buff.append(c);
+ state = IN_PARAM;
+ }
}
- } else if (state == STICK) {
- params.add(buff.toString());
- buff.setLength(0);
- state = NULL;
- } else if (state == STICKPENDING) {
- buff.append('\\');
- params.add(buff.toString());
- buff.setLength(0);
- state = NULL;
- } else if (state == INPARAMPENDING) {
- state = INPARAM;
- buff.append('\\');
- buff.append(c);
- } else { // INPARAM
- buff.append(c);
- }
-
- continue;
- }
-
- if (c == '\\') {
- if (state == NULL) {
- ++i;
-
- if (i < slength) {
- char cc = s.charAt(i);
-
- if ((cc == '"') || (cc == '\\')) {
- buff.append(cc);
- } else if (Character.isWhitespace(cc)) {
- buff.append(c);
- --i;
- } else {
- buff.append(c);
- buff.append(cc);
- }
+ break;
+ case IN_SINGLE_QUOTE:
+ if (c != '\'') {
+ buff.append(c);
} else {
- buff.append('\\');
-
- break;
+ state = IN_PARAM;
}
-
- continue;
- } else if (state == INPARAM) {
- state = INPARAMPENDING;
- } else if (state == INPARAMPENDING) {
- buff.append('\\');
- state = INPARAM;
- } else if (state == STICK) {
- state = STICKPENDING;
- } else if (state == STICKPENDING) {
- buff.append('\\');
- state = STICK;
- }
-
- continue;
- }
-
- if (c == '"') {
- if (state == NULL) {
- state = INPARAM;
- } else if (state == INPARAM) {
- state = STICK;
- } else if (state == STICK) {
- state = INPARAM;
- } else if (state == STICKPENDING) {
- buff.append('"');
- state = STICK;
- } else { // INPARAMPENDING
- buff.append('"');
- state = INPARAM;
- }
-
- continue;
- }
-
- if (state == INPARAMPENDING) {
- buff.append('\\');
- state = INPARAM;
- } else if (state == STICKPENDING) {
- buff.append('\\');
- state = STICK;
+ break;
+ case IN_DOUBLE_QUOTE:
+ switch (c) {
+ case '\\':
+ char peek = (i < slength - 1) ? s.charAt(i+1) :
Character.MIN_VALUE;
+ if (peek == '"' || peek =='\\') {
+ buff.append(peek);
+ i++;
+ } else {
+ buff.append(c);
+ }
+ break;
+ case '"':
+ state = IN_PARAM;
+ break;
+ default:
+ buff.append(c);
+ }
+ break;
+ case IN_PARAM:
+ switch (c) {
+ case '\'':
+ state = IN_SINGLE_QUOTE;
+ break;
+ case '"':
+ state = IN_DOUBLE_QUOTE;
+ break;
+ default:
+ if (Character.isWhitespace(c)) {
+ params.add(buff.toString());
+ buff.setLength(0);
+ state = NULL;
+ } else {
+ buff.append(c);
+ }
+ }
+ break;
}
-
- buff.append(c);
}
-
- // collect
- if (state == INPARAM) {
+ if (buff.length() > 0) {
params.add(buff.toString());
- } else if ((state & (INPARAMPENDING | STICKPENDING)) != 0) {
- buff.append('\\');
- params.add(buff.toString());
- } else { // NULL or STICK
-
- if (buff.length() != 0) {
- params.add(buff.toString());
- }
}
- String[] ret = new String[params.size()];
- params.toArray(ret);
-
- return ret;
+ return params.toArray(new String[params.size()]);
}
/** Complementary method to parseParameters
diff --git
a/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
b/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
index a27fdc2..f5116c1 100644
---
a/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
+++
b/platform/openide.util/test/unit/src/org/openide/util/BaseUtilitiesTest.java
@@ -22,9 +22,6 @@ package org.openide.util;
import java.io.File;
import java.net.URI;
import java.util.Locale;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertTrue;
import org.netbeans.junit.NbTestCase;
/**
@@ -146,4 +143,62 @@ public class BaseUtilitiesTest extends NbTestCase {
assertEquals(f, BaseUtilities.toFile(u));
}
+ public void testParseParameters1() {
+ String[] args = BaseUtilities.parseParameters("\"c:\\program
files\\jdk\\bin\\java\" -Dmessage=\"Hello /\\\\/\\\\ there!\" -Xmx128m");
+ assertEquals(3, args.length);
+ assertEquals("c:\\program files\\jdk\\bin\\java", args[0]);
+ assertEquals("-Dmessage=Hello /\\/\\ there!", args[1]);
+ assertEquals("-Xmx128m", args[2]);
+ }
+
+ public void testParseParameters2() {
+ String[] args = BaseUtilities.parseParameters("c:\\program
files\\jdk\\bin\\java -Xmx128m");
+ assertEquals(3, args.length);
+ assertEquals("c:\\program", args[0]);
+ assertEquals("files\\jdk\\bin\\java", args[1]);
+ assertEquals("-Xmx128m", args[2]);
+ }
+
+ public void testParseParameters3() {
+ String[] args = BaseUtilities.parseParameters("\"-Xmx128m");
+ assertEquals(1, args.length);
+ assertEquals("-Xmx128m", args[0]);
+ }
+
+ public void testParseParameters4() {
+ String[] args = BaseUtilities.parseParameters("'-Xmx128m");
+ assertEquals(1, args.length);
+ assertEquals("-Xmx128m", args[0]);
+ }
+
+ public void testParseParameters5() {
+ String[] args = BaseUtilities.parseParameters("-Dmessage='Hello
\"NetBeans\"'");
+ assertEquals(1, args.length);
+ assertEquals("-Dmessage=Hello \"NetBeans\"", args[0]);
+ }
+
+ public void testParseParameters6() {
+ String[] args = BaseUtilities.parseParameters("'c:\\program
files\\jdk\\bin\\java'\n-Dmessage='Hello /\\/\\ there!' \t -Xmx128m");
+ assertEquals(3, args.length);
+ assertEquals("c:\\program files\\jdk\\bin\\java", args[0]);
+ assertEquals("-Dmessage=Hello /\\/\\ there!", args[1]);
+ assertEquals("-Xmx128m", args[2]);
+ }
+
+ public void testParseParameters7() {
+ String[] args = BaseUtilities.parseParameters("-Dmessage=\"NetBeans\"
\"\" 'third\narg'");
+ assertEquals(3, args.length);
+ assertEquals("-Dmessage=NetBeans", args[0]);
+ assertEquals("", args[1]);
+ assertEquals("third\narg", args[2]);
+ }
+
+ public void testParseParameters8() {
+ String[] args = BaseUtilities.parseParameters("-Dmessage=\"NetBeans\"
\"\" \"third\\narg\"");
+ assertEquals(3, args.length);
+ assertEquals("-Dmessage=NetBeans", args[0]);
+ assertEquals("", args[1]);
+ assertEquals("third\\narg", args[2]);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists