Here you can find the source of getCommandTokens(String commandString)
public static String[] getCommandTokens(String commandString)
//package com.java2s; /**/*from w w w.j a v a 2 s . c om*/ * Sahi - Web Automation and Test Tool * * Copyright 2006 V Narayan Raman * * 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; public class Main { public static String[] getCommandTokens(String commandString) { boolean escaped = false; ArrayList<String> tokens = new ArrayList<String>(); int length = commandString.length(); int startIx = 0; int endIx = length; final char NONE = 'x'; char startChar = NONE; for (int i = 0; i < length; i++) { char c = commandString.charAt(i); if (c == '\\') { escaped = !escaped; } if (!escaped) { if (c == ' ' && startChar == NONE) { endIx = i; if (startIx != endIx) // Happens just after quote tokens.add(commandString.substring(startIx, endIx + 1).trim()); startChar = NONE; // reset startIx = i + 1; } if (c == '"' || c == '\'') { if (startChar == NONE) { startChar = c; startIx = i; } else { if (c == startChar) { endIx = i; tokens.add(commandString.substring(startIx + 1, endIx).trim()); startChar = NONE; // reset startIx = i + 1; } } } } if (c != '\\') { escaped = false; } } if (startIx < length) { tokens.add(commandString.substring(startIx, length).trim()); } return (String[]) tokens.toArray(new String[0]); } }