Java String Split splitParameters(String parameterString)

Here you can find the source of splitParameters(String parameterString)

Description

Tokenise the comma-separated parameters of a command line, respecting commas in quoted sections.

License

Apache License

Parameter

Parameter Description
parameterString the parameterString to tokenise

Return

a String[] of parameters

Declaration

private static String[] splitParameters(String parameterString) 

Method Source Code

//package com.java2s;
/**//from  ww w . j a  v  a2 s  . c  om
 * Copyright 2013 Simon Curd <simoncurd@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static final char COMMA = ',';
    private static final char QUOTE = '\"';

    /**
     * Tokenise the comma-separated parameters of a command line, respecting
     * commas in quoted sections.
     * 
     * @param parameterString the parameterString to tokenise
     * @return a String[] of parameters
     */
    private static String[] splitParameters(String parameterString) {
        // if there's no commas to split on
        if (parameterString.indexOf(COMMA) == -1) {
            return new String[] { parameterString };
        }

        // or if there's no quotes to care about
        else if (parameterString.indexOf(QUOTE) == -1) {
            return parameterString.split(",");
        }

        List<String> resultList = new ArrayList<String>();

        // flags
        boolean inQuotedToken = false;
        int startPos = 0;

        // interpret the line char by char and tokenise the parameterString
        for (int pos = 0; pos != parameterString.length(); pos++) {
            char c = parameterString.charAt(pos);
            if (c == QUOTE && inQuotedToken == false) {
                inQuotedToken = true;
            } else if (c == QUOTE && inQuotedToken == true) {
                inQuotedToken = false;
            } else if (c == COMMA && inQuotedToken == false) {
                String token = getToken(parameterString, startPos, pos);
                resultList.add(token);
                startPos = pos + 1;
            }
        }

        // validate we're not left with an open quote
        if (inQuotedToken == true) {
            throw new RuntimeException("unterminated quote error ["
                    + getToken(parameterString, startPos, parameterString.length()) + "]");
        }

        // pickup the final token
        String token = getToken(parameterString, startPos, parameterString.length());
        resultList.add(token);

        // build the array of parameters
        String[] results = new String[resultList.size()];
        for (int index = 0; index != resultList.size(); index++) {
            results[index] = resultList.get(index);
        }

        return results;

    }

    /**
     * Extract a token from a String based on start and end positions
     * 
     * @param line The line to extract the token from
     * @param startPos The start position to extract from
     * @param endPos The end position to extract from
     * @return The token
     */
    private static String getToken(String line, int startPos, int endPos) {
        String token = line.substring(startPos, endPos);
        return token.trim();
    }
}

Related

  1. splitOrderedDurationsIntoIntervals(String[] durations, int numberOfIntervals)
  2. splitPackageName(String packageName)
  3. splitPackages(String packages)
  4. splitParagraphs(String value)
  5. splitParameters(String parameters)
  6. splitParameterString(String theInput, boolean theUnescapeComponents)
  7. splitPartitionColStats(String key)
  8. splitResponses(String text)
  9. splitScopes(String aActivityPath)