class CommandLineSupport {

  public static List<String> split(final String commandline) {
    if (commandline == null || commandline.length() == 0) {
      return new ArrayList<String>();
    }
    final List<String> args = new ArrayList<String>();
    final StringBuilder current = new StringBuilder();
    int mode = M_STRIPWHITESPACE;
    int endChar = BLANK;
    for (final char c : commandline.toCharArray()) {
      switch (mode) {
        case M_STRIPWHITESPACE:
          if (!Character.isWhitespace(c)) {
            if (c == QUOTE) {
              endChar = QUOTE;
            } else {
              current.append(c);
              endChar = BLANK;
            }
            mode = M_PARSEARGUMENT;
          }
          break;
        case M_PARSEARGUMENT:
          if (c == endChar) {
            addArgument(args, current);
            mode = M_STRIPWHITESPACE;
          } else if (c == SLASH) {
            current.append(SLASH);
            mode = M_ESCAPED;
          } else {
            current.append(c);
          }
          break;
        case M_ESCAPED:
          if (c == QUOTE || c == SLASH) {
            current.setCharAt(current.length() - 1, c);
          } else if (c == endChar) {
            addArgument(args, current);
            mode = M_STRIPWHITESPACE;
          } else {
            current.append(c);
          }
          mode = M_PARSEARGUMENT;
          break;
        default:
          continue;
      }
    }
    addArgument(args, current);
    return args;
  }

}