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