Java String Split by Line splitArgumentsBackslashesAreLiteral(String commandLine)

Here you can find the source of splitArgumentsBackslashesAreLiteral(String commandLine)

Description

Splits a single string containing multiple command line arguments into separate arguments

License

Open Source License

Parameter

Parameter Description
commandLine original line

Return

separated arguments

Declaration

private static List<String> splitArgumentsBackslashesAreLiteral(String commandLine) 

Method Source Code


//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;
    }
}

Related

  1. split(String line, String separator)
  2. split(String line, String separator)
  3. split(String line, String seperator)
  4. Split(String line, String sptr)
  5. splitArguments(String commandLine, boolean backslashesAreLiteral)
  6. splitByBrackets(String line)
  7. splitBySeparator(String line, char sep)
  8. splitBySeparatorAndParen(String line, char sep)
  9. splitBySeparatorQuoteAndParen(String line, char sep)