Java XML Parse String parseArgumentString(String argStr)

Here you can find the source of parseArgumentString(String argStr)

Description

parse Argument String

License

Open Source License

Declaration

public static String[] parseArgumentString(String argStr) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] parseArgumentString(String argStr) {
        boolean quote = false;
        List<String> params = new ArrayList<>();
        List<String> temp = new ArrayList<>();
        String[] split = argStr.split(" ");
        for (int i = 0; i < split.length; i++) {
            String str = split[i];
            char first = str.isEmpty() ? '\u0000' : str.charAt(0);
            char last = str.isEmpty() ? '\u0000' : str.charAt(str.length() - 1);
            char beforeLast = str.length() < 2 ? '\u0000' : str.charAt(str.length() - 2);
            if (first == '"' && last == '"' && beforeLast != '\\') {
                if (quote)
                    throw new IllegalArgumentException("Erroneous quote in word " + i + ": " + str);
                params.add(str.replaceFirst("^\"(.*)\"$", "$1"));
            } else if (first == '"') {
                if (quote)
                    throw new IllegalArgumentException("Erroneous quote in word " + i + ": " + str);
                quote = true;//from  w  ww .  ja  va  2 s.c  om
                temp.add(str.replaceFirst("^\"", ""));
            } else if (last == '"' && beforeLast != '\\') {
                if (!quote)
                    throw new IllegalArgumentException("Erroneous quote in word " + i + ": " + str);
                quote = false;
                temp.add(str.replaceFirst("\"$", ""));
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < temp.size(); j++) {
                    sb.append(temp.get(j));
                    if (j < temp.size() - 1)
                        sb.append(' ');
                }
                params.add(sb.toString());
                temp.clear();
            } else if (quote) {
                temp.add(str);
            } else {
                params.add(str);
            }
        }
        if (quote)
            throw new IllegalArgumentException("Unclosed quotes in string: " + argStr);
        List<String> params2 = new ArrayList<>();
        for (String param : params) {
            if (!param.isEmpty())
                params2.add(param.replaceAll("\\\\\"", "\""));
        }
        return params2.toArray(new String[params2.size()]);
    }
}

Related

  1. parseArgsString(String args)
  2. parseArgsToVmArgs(String[] args, String prefixOfVmArgs)
  3. parseArguments(final String command)
  4. parseArguments(String args)
  5. parseArguments(String[] arguments, String delimiter)
  6. parseArgumentString(String command)
  7. parseArray(String array)
  8. parseArray(String array)
  9. parseArray(String inputArray)