1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.jrobin.cmd;
27
28 import org.jrobin.core.RrdException;
29
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.LinkedList;
33 import java.util.List;
34
35 class RrdCmdScanner {
36 private LinkedList<String> words = new LinkedList<String>();
37 private StringBuffer buff;
38
39 RrdCmdScanner(String command) throws RrdException {
40 String cmd = command.trim();
41
42 char activeQuote = 0;
43 for (int i = 0; i < cmd.length(); i++) {
44 char c = cmd.charAt(i);
45 if ((c == '"' || c == '\'') && activeQuote == 0) {
46
47 initWord();
48 activeQuote = c;
49 continue;
50 }
51 if (c == activeQuote) {
52
53 activeQuote = 0;
54 continue;
55 }
56 if (isSeparator(c) && activeQuote == 0) {
57
58 finishWord();
59 continue;
60 }
61 if (c == '\\' && activeQuote == '"' && i + 1 < cmd.length()) {
62
63 char c2 = cmd.charAt(i + 1);
64 if (c2 == '\\' || c2 == '"') {
65 appendWord(c2);
66 i++;
67 continue;
68 }
69 }
70
71 appendWord(c);
72 }
73 if (activeQuote != 0) {
74 throw new RrdException("End of command reached but " + activeQuote + " expected");
75 }
76 finishWord();
77 }
78
79 String getCmdType() {
80 if (words.size() > 0) {
81 return words.get(0);
82 }
83 else {
84 return null;
85 }
86 }
87
88 private void appendWord(char c) {
89 if (buff == null) {
90 buff = new StringBuffer("");
91 }
92 buff.append(c);
93 }
94
95 private void finishWord() {
96 if (buff != null) {
97 words.add(buff.toString());
98 buff = null;
99 }
100 }
101
102 private void initWord() {
103 if (buff == null) {
104 buff = new StringBuffer("");
105 }
106 }
107
108 void dump() {
109 for (String word : words) {
110 System.out.println(word);
111 }
112 }
113
114 String getOptionValue(String shortForm, String longForm, String defaultValue)
115 throws RrdException {
116 String value = null;
117 if (shortForm != null) {
118 value = getOptionValue("-" + shortForm);
119 }
120 if (value == null && longForm != null) {
121 value = getOptionValue("--" + longForm);
122 }
123 if (value == null) {
124 value = defaultValue;
125 }
126 return value;
127 }
128
129 String getOptionValue(String shortForm, String longForm)
130 throws RrdException {
131 return getOptionValue(shortForm, longForm, null);
132 }
133
134 private String getOptionValue(String fullForm) throws RrdException {
135 Iterator<String> iter = words.listIterator();
136 while (iter.hasNext()) {
137 String word = iter.next();
138 if (word.equals(fullForm)) {
139
140 if (iter.hasNext()) {
141 iter.remove();
142 String value = iter.next();
143 iter.remove();
144 return value;
145 }
146 else {
147 throw new RrdException("Value for option " + fullForm + " expected but not found");
148 }
149 }
150 if (word.startsWith(fullForm)) {
151 int pos = fullForm.length();
152 if (word.charAt(pos) == '=') {
153
154 pos++;
155 }
156 iter.remove();
157 return word.substring(pos);
158 }
159 }
160 return null;
161 }
162
163 boolean getBooleanOption(String shortForm, String longForm) {
164 Iterator<String> iter = words.listIterator();
165 while (iter.hasNext()) {
166 String word = iter.next();
167 if ((shortForm != null && word.equals("-" + shortForm)) ||
168 (longForm != null && word.equals("--" + longForm))) {
169 iter.remove();
170 return true;
171 }
172 }
173 return false;
174 }
175
176 String[] getMultipleOptions(String shortForm, String longForm) throws RrdException {
177 List<String> values = new ArrayList<String>();
178 for (; ;) {
179 String value = getOptionValue(shortForm, longForm, null);
180 if (value == null) {
181 break;
182 }
183 values.add(value);
184 }
185 return values.toArray(new String[values.size()]);
186 }
187
188 String[] getRemainingWords() {
189 return words.toArray(new String[words.size()]);
190 }
191
192 boolean isSeparator(char c) {
193 return Character.isWhitespace(c);
194 }
195 }