@@ -45,41 +45,51 @@ public static String[] formatAndSplit(String src, Map<String, String> dict,
4545 }
4646
4747 // Split the resulting string in arguments
48- return quotedSplit (src , '"' , false );
48+ return quotedSplit (src , '"' );
4949 }
5050
51- public static String [] quotedSplit (String src , char escapeChar ,
52- boolean acceptEmptyArguments )
51+ // Split a string into a list of space-delimited arguments
52+ //
53+ // Arguments containing spaces can be escaped by quoting with 'quoteChar'.
54+ // Arguments entirely enclosed by 'quoteChar' will have quotes stripped,
55+ // arguments where quoteChar is inside another part of the argument string (ie -DNAME="VALUE PARTS")
56+ // will have the quotes retained.
57+ public static String [] quotedSplit (String src , char quoteChar )
5358 throws Exception {
54- String quote = "" + escapeChar ;
5559 List <String > res = new ArrayList <String >();
56- String escapedArg = null ;
57- boolean escaping = false ;
58- for (String i : src .split (" " )) {
59- if (!escaping ) {
60- if (!i .startsWith (quote )) {
61- if (i .trim ().length () != 0 || acceptEmptyArguments )
62- res .add (i );
63- continue ;
60+ String arg = "" ;
61+ boolean quoting = false ;
62+ boolean quoting_instr = false ;
63+
64+ for (char c : src .toCharArray ()) {
65+ if (quoting ) {
66+ if (c == quoteChar ) {
67+ if (quoting_instr )
68+ arg += quoteChar ;
69+ res .add (arg );
70+ arg = "" ;
71+ quoting = false ;
72+ } else {
73+ arg += c ;
6474 }
65-
66- escaping = true ;
67- i = i .substring (1 );
68- escapedArg = "" ;
6975 }
70-
71- if (!i .endsWith (quote )) {
72- escapedArg += i + " " ;
73- continue ;
76+ else { // not quoting
77+ if (c == ' ' ) {
78+ res .add (arg );
79+ arg = "" ;
80+ } else if (c == quoteChar ) {
81+ quoting = true ;
82+ quoting_instr = arg .length () > 0 ;
83+ if (quoting_instr )
84+ arg += quoteChar ;
85+ } else {
86+ arg += c ;
87+ }
7488 }
75-
76- escapedArg += i .substring (0 , i .length () - 1 );
77- if (escapedArg .trim ().length () != 0 || acceptEmptyArguments )
78- res .add (escapedArg );
79- escaping = false ;
8089 }
81- if (escaping )
82- throw new Exception ("Invalid quoting: no closing '" + escapeChar +
90+
91+ if (quoting )
92+ throw new Exception ("Invalid quoting: no closing '" + quoteChar +
8393 "' char found." );
8494 return res .toArray (new String [0 ]);
8595 }
0 commit comments