Here you can find the source of splitCommandLine(String str, boolean extract)
private static String[] splitCommandLine(String str, boolean extract)
//package com.java2s; /*//from www .j a v a 2 s . c o m * NOTE: This copyright does *not* cover user programs that use HQ * program services by normal system calls through the application * program interfaces provided as part of the Hyperic Plug-in Development * Kit or the Hyperic Client Development Kit - this is merely considered * normal use of the program, and does *not* fall under the heading of * "derived work". * * Copyright (C) [2004-2008], Hyperic, Inc. * This file is part of HQ. * * HQ is free software; you can redistribute it and/or modify * it under the terms version 2 of the GNU General Public License as * published by the Free Software Foundation. This program is distributed * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ import java.util.ArrayList; import java.util.List; public class Main { private static final char QUOTE = '\''; private static final char DOUBLEQUOTE = '"'; private static final char BACKSLASH = '\\'; private static String[] splitCommandLine(String str, boolean extract) { List list = new ArrayList(); int slen; char c; int i = 0; int tokstart = 0; if ((str == null) || ((str = str.trim()).length() == 0)) { return new String[0]; } slen = str.length(); while (true) { if (i < slen) { c = str.charAt(i); } else { c = '\0'; } if (c == BACKSLASH) { i++; if (i < slen) { i++; } } else if (c == QUOTE) { i = skipSingleQuoted(str, slen, ++i); } else if (c == DOUBLEQUOTE) { i = skipDoubleQuoted(str, slen, ++i); } else if ((c == '\0') || spctabnl(c)) { String token = str.substring(tokstart, i); if (extract) { token = extractQuoted(token); } list.add(token); while ((i < slen) && spctabnl(str.charAt(i))) { i++; } if (i < slen) { tokstart = i; } else { break; } } else { i++; } } return (String[]) list.toArray(new String[list.size()]); } private static int skipSingleQuoted(String str, int slen, int sind) { int i = sind; while ((i < slen) && str.charAt(i) != QUOTE) { i++; } if (i < slen) { i++; } return i; } private static int skipDoubleQuoted(String str, int slen, int sind) { int i = sind; int pass_next = 0; while (i < slen) { char c = str.charAt(i); if (pass_next != 0) { pass_next = 0; i++; } else if (c == BACKSLASH) { pass_next++; i++; } else if (c != DOUBLEQUOTE) { i++; } else { break; } } if (i < slen) { i++; } return i; } private static boolean spctabnl(char c) { return (c == ' ') || (c == '\t') || (c == '\n'); } public static String extractQuoted(String str) { if (str.length() == 0) { return str; } if (str.charAt(0) == QUOTE) { str = extractSingleQuoted(str); } else if (str.charAt(0) == DOUBLEQUOTE) { str = extractDoubleQuoted(str); } return str; } private static String extractSingleQuoted(String str) { char first = str.charAt(0); char last = str.charAt(str.length() - 1); if (first == QUOTE) { if (last == QUOTE) { return str.substring(1, str.length() - 1); } else { throw new IllegalArgumentException("Unbalanced quotation marks"); } } else { return str; } } private static String extractDoubleQuoted(String str) { int slen = str.length(); int i = 0; int pass_next = 0; int dquote = 0; StringBuffer temp = new StringBuffer(slen); while (i < slen) { char c = str.charAt(i); if (pass_next != 0) { if (dquote == 0) { temp.append('\\'); } pass_next = 0; temp.append(c); } else if (c == BACKSLASH) { pass_next++; } else if (c != DOUBLEQUOTE) { temp.append(c); } else { dquote ^= 1; } i++; } if (dquote != 0) { throw new IllegalArgumentException("Unbalanced quotation marks"); } return temp.toString(); } }