1
2
3
4
5 package net.sf.tomp.xtcl;
6
7 import net.sf.tomp.xtcl.command.End;
8 import net.sf.tomp.xtcl.command.Sequence;
9
10 import java.io.File;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.StringTokenizer;
15
16 /***
17 * Abstract base for a Compiler.
18 *
19 * @author tomp
20 */
21 public abstract class CompilerBase implements Compiler {
22
23 private Context context;
24
25 /***
26 * Compiles commands from a CommandLineReader into a command Sequence
27 *
28 * @param clr
29 * CommandLineReader - the source for compilation
30 * @return compiled Command
31 * @throws Exception
32 * if compilation fails
33 */
34 public Command compile(CommandLineReader clr) throws Exception {
35 String line = clr.nextCommandLine();
36 List lines = new ArrayList();
37
38 while (line != null) {
39 Command command = compile(line, clr);
40
41 if (command instanceof End) {
42 break;
43 }
44
45 if (command != null) {
46 lines.add(command);
47 }
48
49 line = clr.nextCommandLine();
50 }
51
52 Sequence c = new Sequence();
53
54 c.setCommands((Command[]) lines.toArray(new Command[0]));
55
56 return c;
57 }
58
59 /***
60 * Compiles commands from a String
61 *
62 * @param f
63 * String - the source from which it compiles
64 * @return compiled Command
65 * @throws Exception
66 * if compilation fails
67 */
68 public Command compile(File f) throws Exception {
69 return compile(new CommandLineReader(f));
70 }
71
72 /***
73 * Compiles commands from a String
74 *
75 * @param s
76 * String - the source from which it compiles
77 * @return compiled Command
78 * @throws Exception
79 * if compilation fails
80 */
81 public Command compile(String s) throws Exception {
82 return compile(new CommandLineReader(s));
83 }
84
85 /***
86 * Compiles commands from a String followed by a CommandLineReader into a
87 * command Sequence
88 *
89 * @param cl
90 * String - will be tokenized and passed further to
91 * compile(String[], CommandLineReader)
92 * @param clr
93 * CommandLineReader - the sources for compilation first the
94 * String, then the CLR
95 * @return compiled Command
96 * @throws Exception
97 * if compilation fails
98 */
99 public Command compile(String cl, CommandLineReader clr) throws Exception {
100 List com = new ArrayList();
101
102 StringTokenizer st = new Tokenizer(cl.trim());
103
104 while (st.hasMoreTokens()) {
105 com.add(st.nextToken());
106 }
107
108 return compile((String[]) com.toArray(new String[0]), clr);
109 }
110
111 /***
112 * @param ci
113 * Command name - used just for error reporting if it happens
114 * @param i current position in the cl fields (parameters) array
115 * @param cl fields (parameters) array
116 * @param commandToReturn
117 */
118 protected void assertWholeLineRead(String ci, int i, String[] cl,
119 Command commandToReturn) {
120 if ((commandToReturn != null) && (i != cl.length)) {
121 throw new IllegalArgumentException(getClass().getName()
122 + " - invalid parameters for command " + ci);
123 }
124 }
125
126 /***
127 * @param ci
128 * @param cl
129 * @param clr
130 * @return
131 * @throws Exception
132 */
133 protected Command compileByNextCompiler(String ci, String[] cl,
134 CommandLineReader clr) throws Exception {
135 Compiler next = getNextCompiler();
136
137 if (next == null) {
138 throw new UnknownCommandException(ci);
139 } else {
140 return next.compile(cl, clr);
141 }
142 }
143
144 /***
145 * Reads and sets all the parameters to the ParametrizedCommand
146 * @param cl parameter array
147 * @param i starting position in the cl array
148 * @param command the ParametrizedCommand
149 * @return position in the cl parameter array after parsing
150 */
151 protected int setParameters(String[] cl, int i, ParametrizedCommand command) {
152
153 while (i < cl.length) {
154 String param = cl[i++];
155
156 if (param.startsWith("@")) {
157
158 command.addParameterPropertiesReference(param.substring(1));
159 } else {
160
161 String[] ss = param.split("(?<!////)//=");
162
163 if (ss.length > 0) {
164 command.setParameter(ss[0], ss.length == 1 ? "" : ss[1]);
165 }
166 }
167 }
168 return i;
169 }
170
171 /***
172 * @return Returns the context.
173 */
174 public Context getContext() {
175 return context;
176 }
177
178 /***
179 * @return Returns the parent.
180 */
181 public Compiler getNextCompiler() {
182 return context.getNextCompiler(this);
183 }
184
185 /***
186 * @param context
187 * The context to set.
188 */
189 public void setContext(Context context) {
190 this.context = context;
191 }
192 }
193
194 /***
195 * Tokenizer for parsing parameters from the String from the line
196 */
197
198 class Tokenizer extends StringTokenizer {
199 /***
200 * Creates a new Tokenizer object.
201 *
202 * @param s the Tokenizer will be filled with this String
203 */
204 public Tokenizer(String s) {
205 super(s);
206 }
207
208 /***
209 * Gets the next Token
210 *
211 * @return the next token
212 */
213 public String nextToken() {
214 StringBuffer token = new StringBuffer();
215
216 String n = super.nextToken();
217
218 token.append(n.replaceAll("//\"", ""));
219
220 if (n.indexOf('"') >= 0) {
221 do {
222 token.append(' ');
223 n = super.nextToken();
224 token.append(n.replaceAll("//\"", ""));
225 } while (n.indexOf('"') < 0);
226 }
227
228 return token.toString();
229 }
230 }