source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/CommandParser.java

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