Here you can find the source of splitArgumentsBackslashesAreLiteral(String commandLine)
Parameter | Description |
---|---|
commandLine | original line |
private static List<String> splitArgumentsBackslashesAreLiteral(String commandLine)
//package com.java2s; import java.util.ArrayList; import java.util.List; public class Main { /**// ww w .j a v a2s . c o m * Splits a single string containing multiple command line arguments * into separate arguments * * @param commandLine original line * * @return separated arguments */ private static List<String> splitArgumentsBackslashesAreLiteral(String commandLine) { List<String> result = null; if (commandLine != null) { result = new ArrayList<String>(); char[] c = commandLine.toCharArray(); boolean readingQuotes = false; StringBuilder buffer = new StringBuilder(); for (int i = 0; i < c.length; i++) { char cc = c[i]; if (cc == '\"') { buffer.append(cc); boolean endOfString = false; // the if and else if below are to detect we are really out // of the current double quote region and not an inner quote // for example "foo="abc"" should not end at the quote after // the equals sign, but rather after the second quote after // the c character. To ensure we are out of a double quote // region, see if we are either at the end of the total string // or the next character after this "second" double quote // is whitespace or more text. if (i == (c.length - 1)) { endOfString = true; readingQuotes = false; } else if ((readingQuotes) && (Character.isWhitespace(c[i + 1]))) { endOfString = true; readingQuotes = false; } else // start of quoting or internal quote { readingQuotes = true; } if (endOfString) { result.add(buffer.toString()); buffer.delete(0, buffer.length()); } } else if ((!readingQuotes) && (Character.isWhitespace(cc))) { if (buffer.length() > 0) // end of normal argument { result.add(buffer.toString()); buffer.delete(0, buffer.length()); } } else { buffer.append(cc); } } if (buffer.length() > 0) { result.add(buffer.toString()); buffer.delete(0, buffer.length()); } } return result; } }