package de.ugoe.cs.util.console; import java.util.ArrayList; import java.util.List; /** *

* Helper class to parse command strings and create parameters. *

* * @author Steffen Herbold */ public class CommandParser { /** *

* Name of the command. *

*/ private String commandName; /** *

* Parameters of the command as a {@link List}. The parameters can either be * {@link String} or {@link String} arrays. *

*/ private List parameters; /** *

* Creates a new CommandParser. *

*/ public CommandParser() { commandName = ""; parameters = new ArrayList(); } /** *

* Returns the name of the command. *

* * @return name of the command */ public String getCommandName() { return commandName; } /** *

* Returns the {@link List} of parameters *

* * @return */ public List getParameters() { return parameters; } /** *

* Parses a command after the following EBNF (mixed with natural language): *

* * <command> := * <commandname><whitespace>{<parameter>}
* <commandname> := String without whitespaces. Has to be a valid Java * class name
* <parameter> := <string>|<stringarray>
* <string> := * <stringwithoutwhitespaces>|<stringwithwhitespaces> * <stringwithoutwhitespaces> := a string without whitespaces
* <stringwithoutwhitespaces> := a string, that can have whitespaces, * but must be in double quotes
* <stringarray> := * "["<string>{<whitespace><string>"]" *
* * @param command * the command as a string */ public void parse(String command) { if (command == null || command.equals("")) { return; } String[] splitResult = command.split(" "); commandName = splitResult[0]; char[] commandChars = command.substring(commandName.length()) .toCharArray(); boolean startParameter = true; boolean isArray = false; boolean startArrayparameter = false; boolean isString = false; int bufferPos = 0; char[] buffer = new char[1024]; boolean quote = false; List arrayBuffer = null; for (int i = 0; i < commandChars.length; i++) { if (i < commandChars.length && startParameter && commandChars[i] == '[') { isArray = true; startArrayparameter = true; arrayBuffer = new ArrayList(); startParameter = false; i++; // skip [ } if (i < commandChars.length && startParameter && commandChars[i] == '\'') { isString = true; quote = true; startParameter = false; i++; // skip ' } if (i < commandChars.length && startParameter && !Character.isWhitespace(commandChars[i])) { isString = true; startParameter = false; } if (isArray) { if (i < commandChars.length && commandChars[i] == ']') { if (bufferPos > 0) { buffer[bufferPos] = '\0'; arrayBuffer.add((new String(buffer)).trim()); } parameters.add(arrayBuffer.toArray(new String[0])); isArray = false; isString = false; bufferPos = 0; buffer = new char[128]; startArrayparameter = false; startParameter = true; i++; // skip ] } if (i < commandChars.length && startArrayparameter && !Character.isWhitespace(commandChars[i])) { buffer = new char[128]; bufferPos = 0; quote = commandChars[i] == '\''; if (quote) { i++; // skip ' } startArrayparameter = false; } if (i < commandChars.length && quote && commandChars[i] == '\'') { // end of parameter with ' i++; // skip ' startArrayparameter = true; buffer[bufferPos] = '\0'; arrayBuffer.add((new String(buffer)).trim()); } if (i < commandChars.length && !quote && Character.isWhitespace(commandChars[i])) { startArrayparameter = true; buffer[bufferPos] = '\0'; arrayBuffer.add((new String(buffer)).trim()); } if (i < commandChars.length && !startArrayparameter && !startParameter) { buffer[bufferPos] = commandChars[i]; bufferPos++; } } if (isString) { if ((quote && commandChars[i] == '\'') || (!quote && Character.isWhitespace(commandChars[i]))) { // end of parameter with ' if (quote) { i++; // skip ' } if (bufferPos > 0) { buffer[bufferPos] = '\0'; } parameters.add((new String(buffer).trim())); isArray = false; isString = false; bufferPos = 0; buffer = new char[128]; startArrayparameter = false; startParameter = true; } if (!startParameter) { buffer[bufferPos] = commandChars[i]; bufferPos++; } } } if (bufferPos > 0) { if (isArray) { arrayBuffer.add((new String(buffer)).trim()); parameters.add(arrayBuffer); } if (isString) { parameters.add((new String(buffer)).trim()); } } } }