@Test public void testDoubleQuotedSingleTokenWithEscapes() throws Exception { List<String> command = CommandTokeniser.tokeniseCommandLine("\"Testing \\' \\\" 123\""); assertThat(command.size(), is(equalTo(1))); assertThat(command.get(0), is(equalTo("Testing \\' \\\" 123"))); }
public static List<String> tokeniseCommandLine(String commandLine) throws IOException { List<String> cmd = new LinkedList<String>(); boolean inQuotes = false; char last = '\0'; char quote = '\0'; StringBuilder token = new StringBuilder(); for (char c : commandLine.toCharArray()) { if (inQuotes) { token.append(c); if (c == quote && last != '\\') { inQuotes = false; quote = '\0'; } } else { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } } else { token.append(c); if (c == '\'' || c == '"') { inQuotes = true; quote = c; } } } last = c; } if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } if (inQuotes) { throw new IOException("The command '" + commandLine + "' failed to terminate a quoted token!"); } return cmd; }
CommandTokeniser { public static List<String> tokeniseCommandLine(String commandLine) throws IOException { List<String> cmd = new LinkedList<String>(); boolean inQuotes = false; char last = '\0'; char quote = '\0'; StringBuilder token = new StringBuilder(); for (char c : commandLine.toCharArray()) { if (inQuotes) { token.append(c); if (c == quote && last != '\\') { inQuotes = false; quote = '\0'; } } else { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } } else { token.append(c); if (c == '\'' || c == '"') { inQuotes = true; quote = c; } } } last = c; } if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } if (inQuotes) { throw new IOException("The command '" + commandLine + "' failed to terminate a quoted token!"); } return cmd; } }
CommandTokeniser { public static List<String> tokeniseCommandLine(String commandLine) throws IOException { List<String> cmd = new LinkedList<String>(); boolean inQuotes = false; char last = '\0'; char quote = '\0'; StringBuilder token = new StringBuilder(); for (char c : commandLine.toCharArray()) { if (inQuotes) { token.append(c); if (c == quote && last != '\\') { inQuotes = false; quote = '\0'; } } else { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } } else { token.append(c); if (c == '\'' || c == '"') { inQuotes = true; quote = c; } } } last = c; } if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } if (inQuotes) { throw new IOException("The command '" + commandLine + "' failed to terminate a quoted token!"); } return cmd; }  }
CommandTokeniser { public static List<String> tokeniseCommandLine(String commandLine) throws IOException { List<String> cmd = new LinkedList<String>(); boolean inQuotes = false; char last = '\0'; char quote = '\0'; StringBuilder token = new StringBuilder(); for (char c : commandLine.toCharArray()) { if (inQuotes) { token.append(c); if (c == quote && last != '\\') { inQuotes = false; quote = '\0'; } } else { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } } else { token.append(c); if (c == '\'' || c == '"') { inQuotes = true; quote = c; } } } last = c; } if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } if (inQuotes) { throw new IOException("The command '" + commandLine + "' failed to terminate a quoted token!"); } return cmd; }  static List<String> tokeniseCommandLine(String commandLine); }
CommandTokeniser { public static List<String> tokeniseCommandLine(String commandLine) throws IOException { List<String> cmd = new LinkedList<String>(); boolean inQuotes = false; char last = '\0'; char quote = '\0'; StringBuilder token = new StringBuilder(); for (char c : commandLine.toCharArray()) { if (inQuotes) { token.append(c); if (c == quote && last != '\\') { inQuotes = false; quote = '\0'; } } else { if (c == ' ' || c == '\t' || c == '\r' || c == '\n') { if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } } else { token.append(c); if (c == '\'' || c == '"') { inQuotes = true; quote = c; } } } last = c; } if (token.length() > 0) { cmd.add(stripQuotes(token.toString())); token = new StringBuilder(); } if (inQuotes) { throw new IOException("The command '" + commandLine + "' failed to terminate a quoted token!"); } return cmd; }  static List<String> tokeniseCommandLine(String commandLine);  }