Here you can find the source of splitArguments(String commandLine, boolean backslashesAreLiteral)
Parameter | Description |
---|---|
commandLine | original line |
backslashesAreLiteral | whether backslashes are to be treated as literals (true) or as escape characters (false) |
public static List<String> splitArguments(String commandLine, boolean backslashesAreLiteral)
//package com.java2s; import java.util.ArrayList; import java.util.List; public class Main { /**/*from w w w . j a v a2s.c o m*/ * Splits a single string containing multiple command line arguments * into separate arguments * * @param commandLine original line * @param backslashesAreLiteral whether backslashes are to be treated as * literals (true) or as escape characters * (false) * * @return separated arguments */ public static List<String> splitArguments(String commandLine, boolean backslashesAreLiteral) { List<String> result = null; if (backslashesAreLiteral) { result = splitArgumentsBackslashesAreLiteral(commandLine); } else { result = splitArgumentsBackslashesAreNotLiteral(commandLine); } return result; } /** * 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; } /** * Splits a single string containing multiple command line arguments * into separate arguments * * @param commandLine original line * * @return separated arguments */ private static List<String> splitArgumentsBackslashesAreNotLiteral(String commandLine) { List<String> result = null; if (commandLine != null) { result = new ArrayList<String>(); char[] c = commandLine.toCharArray(); boolean readingQuotes = false; boolean readingSlash = false; StringBuilder buffer = new StringBuilder(); for (int i = 0; i < c.length; i++) { char cc = c[i]; if (cc == '\\') { readingSlash = !readingSlash; buffer.append(cc); } else { if ((!readingSlash) && (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); } readingSlash = false; } } if (buffer.length() > 0) { result.add(buffer.toString()); buffer.delete(0, buffer.length()); } } return result; } }