| 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
|---|
| 2 | //
|
|---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 4 | // you may not use this file except in compliance with the License.
|
|---|
| 5 | // You may obtain a copy of the License at
|
|---|
| 6 | //
|
|---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 8 | //
|
|---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
|---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 12 | // See the License for the specific language governing permissions and
|
|---|
| 13 | // limitations under the License.
|
|---|
| 14 |
|
|---|
| 15 | package de.ugoe.cs.util.console;
|
|---|
| 16 |
|
|---|
| 17 | import java.util.ArrayList;
|
|---|
| 18 | import java.util.List;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * <p>
|
|---|
| 22 | * Helper class to parse command strings and create parameters.
|
|---|
| 23 | * </p>
|
|---|
| 24 | *
|
|---|
| 25 | * @author Steffen Herbold
|
|---|
| 26 | * @version 1.0
|
|---|
| 27 | */
|
|---|
| 28 | public class CommandParser {
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * <p>
|
|---|
| 32 | * Name of the command.
|
|---|
| 33 | * </p>
|
|---|
| 34 | */
|
|---|
| 35 | private String commandName;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * <p>
|
|---|
| 39 | * Parameters of the command as a {@link List}. The parameters can either be
|
|---|
| 40 | * {@link String} or {@link String} arrays.
|
|---|
| 41 | * </p>
|
|---|
| 42 | */
|
|---|
| 43 | private List<Object> parameters;
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * <p>
|
|---|
| 47 | * Creates a new CommandParser.
|
|---|
| 48 | * </p>
|
|---|
| 49 | */
|
|---|
| 50 | public CommandParser() {
|
|---|
| 51 | commandName = "";
|
|---|
| 52 | parameters = new ArrayList<Object>();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * <p>
|
|---|
| 57 | * Returns the name of the command.
|
|---|
| 58 | * </p>
|
|---|
| 59 | *
|
|---|
| 60 | * @return name of the command
|
|---|
| 61 | */
|
|---|
| 62 | public String getCommandName() {
|
|---|
| 63 | return commandName;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * <p>
|
|---|
| 68 | * Returns the {@link List} of parameters
|
|---|
| 69 | * </p>
|
|---|
| 70 | *
|
|---|
| 71 | * @return {@link List} of parameters that were parsed.
|
|---|
| 72 | */
|
|---|
| 73 | public List<Object> getParameters() {
|
|---|
| 74 | return parameters;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * <p>
|
|---|
| 79 | * Parses a command after the following EBNF (mixed with natural language):
|
|---|
| 80 | * </p>
|
|---|
| 81 | * <code>
|
|---|
| 82 | * <command> :=
|
|---|
| 83 | * <commandname><whitespace>{<parameter>}<br>
|
|---|
| 84 | * <commandname> := String without whitespaces. Has to be a valid Java
|
|---|
| 85 | * class name<br>
|
|---|
| 86 | * <parameter> := <string>|<stringarray><br>
|
|---|
| 87 | * <string> :=
|
|---|
| 88 | * <stringwithoutwhitespaces>|<stringwithwhitespaces>
|
|---|
| 89 | * <stringwithoutwhitespaces> := a string without whitespaces<br>
|
|---|
| 90 | * <stringwithoutwhitespaces> := a string, that can have whitespaces,
|
|---|
| 91 | * but must be in double quotes<br>
|
|---|
| 92 | * <stringarray> :=
|
|---|
| 93 | * "["<string>{<whitespace><string>"]"
|
|---|
| 94 | * </code>
|
|---|
| 95 | *
|
|---|
| 96 | * @param command
|
|---|
| 97 | * the command as a string
|
|---|
| 98 | */
|
|---|
| 99 | public void parse(String command) {
|
|---|
| 100 | if (command == null || command.equals("")) {
|
|---|
| 101 | return;
|
|---|
| 102 | }
|
|---|
| 103 | String[] splitResult = command.split(" ");
|
|---|
| 104 | commandName = splitResult[0];
|
|---|
| 105 | char[] commandChars = command.substring(commandName.length())
|
|---|
| 106 | .toCharArray();
|
|---|
| 107 | boolean startParameter = true;
|
|---|
| 108 | boolean isArray = false;
|
|---|
| 109 | boolean startArrayparameter = false;
|
|---|
| 110 | boolean isString = false;
|
|---|
| 111 | int bufferPos = 0;
|
|---|
| 112 | char[] buffer = new char[1024];
|
|---|
| 113 | boolean quote = false;
|
|---|
| 114 | List<String> arrayBuffer = null;
|
|---|
| 115 | for (int i = 0; i < commandChars.length; i++) {
|
|---|
| 116 | if (i < commandChars.length && startParameter
|
|---|
| 117 | && commandChars[i] == '[') {
|
|---|
| 118 | isArray = true;
|
|---|
| 119 | startArrayparameter = true;
|
|---|
| 120 | arrayBuffer = new ArrayList<String>();
|
|---|
| 121 | startParameter = false;
|
|---|
| 122 | i++; // skip [
|
|---|
| 123 | }
|
|---|
| 124 | if (i < commandChars.length && startParameter
|
|---|
| 125 | && commandChars[i] == '\'') {
|
|---|
| 126 | isString = true;
|
|---|
| 127 | quote = true;
|
|---|
| 128 | startParameter = false;
|
|---|
| 129 | i++; // skip '
|
|---|
| 130 | }
|
|---|
| 131 | if (i < commandChars.length && startParameter
|
|---|
| 132 | && !Character.isWhitespace(commandChars[i])) {
|
|---|
| 133 | isString = true;
|
|---|
| 134 | startParameter = false;
|
|---|
| 135 | }
|
|---|
| 136 | if (isArray) {
|
|---|
| 137 | if (i < commandChars.length && commandChars[i] == ']') {
|
|---|
| 138 | if (bufferPos > 0) {
|
|---|
| 139 | buffer[bufferPos] = '\0';
|
|---|
| 140 | arrayBuffer.add((new String(buffer)).trim());
|
|---|
| 141 | }
|
|---|
| 142 | parameters.add(arrayBuffer.toArray(new String[0]));
|
|---|
| 143 | isArray = false;
|
|---|
| 144 | isString = false;
|
|---|
| 145 | bufferPos = 0;
|
|---|
| 146 | buffer = new char[128];
|
|---|
| 147 | startArrayparameter = false;
|
|---|
| 148 | startParameter = true;
|
|---|
| 149 | i++; // skip ]
|
|---|
| 150 | }
|
|---|
| 151 | if (i < commandChars.length && startArrayparameter
|
|---|
| 152 | && !Character.isWhitespace(commandChars[i])) {
|
|---|
| 153 | buffer = new char[128];
|
|---|
| 154 | bufferPos = 0;
|
|---|
| 155 | quote = commandChars[i] == '\'';
|
|---|
| 156 | if (quote) {
|
|---|
| 157 | i++; // skip '
|
|---|
| 158 | }
|
|---|
| 159 | startArrayparameter = false;
|
|---|
| 160 | }
|
|---|
| 161 | if (i < commandChars.length && quote && !startArrayparameter && commandChars[i] == '\'') {
|
|---|
| 162 | // end of parameter with '
|
|---|
| 163 | i++; // skip '
|
|---|
| 164 | startArrayparameter = true;
|
|---|
| 165 | buffer[bufferPos] = '\0';
|
|---|
| 166 | arrayBuffer.add((new String(buffer)).trim());
|
|---|
| 167 | }
|
|---|
| 168 | if (i < commandChars.length && !quote && !startArrayparameter
|
|---|
| 169 | && Character.isWhitespace(commandChars[i])) {
|
|---|
| 170 | startArrayparameter = true;
|
|---|
| 171 | buffer[bufferPos] = '\0';
|
|---|
| 172 | arrayBuffer.add((new String(buffer)).trim());
|
|---|
| 173 | }
|
|---|
| 174 | if (i < commandChars.length && !startArrayparameter
|
|---|
| 175 | && !startParameter) {
|
|---|
| 176 | buffer[bufferPos] = commandChars[i];
|
|---|
| 177 | bufferPos++;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | if (isString) {
|
|---|
| 181 | if ((quote && commandChars[i] == '\'')
|
|---|
| 182 | || (!quote && Character.isWhitespace(commandChars[i]))) {
|
|---|
| 183 | // end of parameter with '
|
|---|
| 184 | if (quote) {
|
|---|
| 185 | i++; // skip '
|
|---|
| 186 | }
|
|---|
| 187 | if (bufferPos > 0) {
|
|---|
| 188 | buffer[bufferPos] = '\0';
|
|---|
| 189 | }
|
|---|
| 190 | parameters.add((new String(buffer).trim()));
|
|---|
| 191 | isArray = false;
|
|---|
| 192 | isString = false;
|
|---|
| 193 | bufferPos = 0;
|
|---|
| 194 | buffer = new char[1024];
|
|---|
| 195 | startArrayparameter = false;
|
|---|
| 196 | startParameter = true;
|
|---|
| 197 | }
|
|---|
| 198 | if (!startParameter) {
|
|---|
| 199 | buffer[bufferPos] = commandChars[i];
|
|---|
| 200 | bufferPos++;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | if (bufferPos > 0) {
|
|---|
| 205 | if (isArray) {
|
|---|
| 206 | //arrayBuffer.add((new String(buffer)).trim());
|
|---|
| 207 | parameters.add(arrayBuffer.toArray(new String[0]));
|
|---|
| 208 | }
|
|---|
| 209 | if (isString) {
|
|---|
| 210 | parameters.add((new String(buffer)).trim());
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|