1 package net.sf.tomp.xtcl;
2
3 import net.sf.tomp.xtcl.command.Call;
4 import net.sf.tomp.xtcl.command.CatchCommand;
5 import net.sf.tomp.xtcl.command.Cd;
6 import net.sf.tomp.xtcl.command.Chain;
7 import net.sf.tomp.xtcl.command.Do;
8 import net.sf.tomp.xtcl.command.Doc;
9 import net.sf.tomp.xtcl.command.Dump;
10 import net.sf.tomp.xtcl.command.DumpContext;
11 import net.sf.tomp.xtcl.command.DumpContexts;
12 import net.sf.tomp.xtcl.command.Echo;
13 import net.sf.tomp.xtcl.command.End;
14 import net.sf.tomp.xtcl.command.Err;
15 import net.sf.tomp.xtcl.command.ExeCommand;
16 import net.sf.tomp.xtcl.command.Fil;
17 import net.sf.tomp.xtcl.command.FilterCommand;
18 import net.sf.tomp.xtcl.command.Function;
19 import net.sf.tomp.xtcl.command.Help;
20 import net.sf.tomp.xtcl.command.JavaCommand;
21 import net.sf.tomp.xtcl.command.JoostFilterCommand;
22 import net.sf.tomp.xtcl.command.JoostStyle;
23 import net.sf.tomp.xtcl.command.Label;
24 import net.sf.tomp.xtcl.command.Load;
25 import net.sf.tomp.xtcl.command.Ret;
26 import net.sf.tomp.xtcl.command.SAXSourceCommand;
27 import net.sf.tomp.xtcl.command.Save;
28 import net.sf.tomp.xtcl.command.SetCommand;
29 import net.sf.tomp.xtcl.command.Str;
30 import net.sf.tomp.xtcl.command.Style;
31 import net.sf.tomp.xtcl.command.TeeFilterCommand;
32 import net.sf.tomp.xtcl.command.Template;
33 import net.sf.tomp.xtcl.command.Transform;
34 import net.sf.tomp.xtcl.command.TransformerFilterCommand;
35 import net.sf.tomp.xtcl.command.TryBlock;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.StringTokenizer;
40
41 /***
42 * Reads the commands from the underlying CommandLineReader, within a Context
43 * and produces a Command (usually a Sequence)
44 *
45 * @author tomp
46 */
47 public class XTCompiler extends CompilerBase {
48 /***
49 * The main compile method. <br>
50 * Compiles commands from a String[] followed by a CommandLineReader into a
51 * command Sequence
52 *
53 * @param cl String[] - array with one input line parsed into tokens
54 * @param clr CommandLineReader - the sources for compilation first the
55 * String[], then the CLR
56 * @return DOCUMENT ME!
57 * @throws Exception DOCUMENT ME!
58 * @throws IllegalArgumentException DOCUMENT ME!
59 */
60 public Command compile(String[] cl, CommandLineReader clr) throws Exception {
61
62
63 Command commandToReturn = null;
64 Context context = getContext();
65
66 if (cl.length == 0) {
67 return null;
68 }
69
70 int i = 0;
71 String ci = cl[i++];
72
73
74 if (ci.equalsIgnoreCase("LOAD")) {
75 Load command = new Load();
76
77 command.setVar(cl[i++]);
78 command.setFile(cl[i++]);
79
80
81 commandToReturn = command;
82
83
84 } else if (ci.equalsIgnoreCase("SAVE")) {
85 Save command = new Save();
86
87 command.setVar(cl[i++]);
88 command.setFile(cl[i++]);
89
90
91 commandToReturn = command;
92
93
94 } else if (ci.equalsIgnoreCase("SET")) {
95 SetCommand command = new SetCommand();
96
97 command.setVar(cl[i++]);
98 command.setSrcVar(cl[i++]);
99
100
101 commandToReturn = command;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 } else if (ci.equalsIgnoreCase("DUMP")) {
119 Dump command = new Dump();
120
121 if (i < cl.length) {
122 command.setVar(cl[i++]);
123 }
124
125
126 commandToReturn = command;
127
128
129 } else if (ci.equalsIgnoreCase("CONTEXT")) {
130 DumpContext command = new DumpContext();
131
132 command.setDeep((i < cl.length)
133 && (cl[i].equals("DEEP") || cl[i].startsWith("VAR")));
134
135
136 i++;
137 commandToReturn = command;
138
139
140 } else if (ci.equalsIgnoreCase("CONTEXTS")) {
141 DumpContexts command = new DumpContexts();
142
143 command.setDeep((i < cl.length)
144 && (cl[i].equals("DEEP") || cl[i].startsWith("VAR")));
145
146
147 i++;
148 commandToReturn = command;
149
150
151 } else if (ci.equalsIgnoreCase("TRANSFORM")) {
152 Transform command = new Transform();
153
154
155 command.setSourceRef(cl[i++]);
156 command.setStyleRef(cl[i++]);
157 command.setResultRef(cl[i++]);
158
159 while (i < cl.length) {
160 String param = cl[i++];
161 StringTokenizer pst = new StringTokenizer(param, "=");
162 String name = pst.nextToken();
163 Object value = pst.nextToken();
164
165 command.setParameter(name, value);
166 }
167
168
169
170 commandToReturn = command;
171
172
173
174 } else if (ci.equalsIgnoreCase("CHAIN")) {
175 Chain command = new Chain();
176
177
178 command.setSourceRef(cl[i++]);
179
180 while (i < (cl.length - 1)) {
181 String styleRef = cl[i++];
182
183 command.addFilterStyleRef(styleRef);
184 }
185
186 command.setResultRef(cl[i++]);
187
188
189
190 commandToReturn = command;
191
192
193 } else if (ci.equalsIgnoreCase("JAVA")) {
194 JavaCommand command = new JavaCommand();
195 List params = new ArrayList();
196
197
198 if (i < cl.length) {
199 command.setClassName(cl[i++]);
200 }
201
202
203 while (i < cl.length) {
204 params.add(cl[i++]);
205 }
206
207 command.setArgs((String[]) params.toArray(new String[0]));
208
209
210 commandToReturn = command;
211
212
213 } else if (ci.equalsIgnoreCase("RUN")) {
214 ExeCommand command = new ExeCommand();
215 List params = new ArrayList();
216
217
218 while (i < cl.length) {
219 params.add(cl[i++]);
220 }
221
222 command.setCmdArray((String[]) params.toArray(new String[0]));
223
224
225 commandToReturn = command;
226
227
228 } else if (ci.equalsIgnoreCase("SAXSOURCE")) {
229 SAXSourceCommand command = new SAXSourceCommand();
230
231 command.setVar(cl[i++]);
232
233
234 command.setClassName(cl[i++]);
235
236
237 command.setInputSource(cl[i++]);
238
239
240 while (i < cl.length) {
241 String param = cl[i++];
242 StringTokenizer pst = new StringTokenizer(param, "=");
243 String name = pst.nextToken();
244 Object value = pst.nextToken();
245
246 command.setParameter(name, value);
247 }
248
249
250 commandToReturn = command;
251
252
253 } else if (ci.equalsIgnoreCase("FILTER")) {
254 FilterCommand command = new FilterCommand();
255 List params = new ArrayList();
256
257 command.setVar(cl[i++]);
258
259
260 command.setClassName(cl[i++]);
261
262
263 while (i < cl.length) {
264 String param = cl[i++];
265 StringTokenizer pst = new StringTokenizer(param, "=");
266 String name = pst.nextToken();
267 Object value = pst.nextToken();
268
269 command.setParameter(name, value);
270 }
271
272
273 commandToReturn = command;
274
275
276
277 } else if (ci.equalsIgnoreCase("TRANSFILTER")
278 || ci.equalsIgnoreCase("STYLEFILTER")) {
279 TransformerFilterCommand command = new TransformerFilterCommand();
280 List params = new ArrayList();
281
282 command.setVar(cl[i++]);
283
284
285 command.setStyleRef(cl[i++]);
286
287
288 while (i < cl.length) {
289 String param = cl[i++];
290 StringTokenizer pst = new StringTokenizer(param, "=");
291 String name = pst.nextToken();
292 Object value = pst.nextToken();
293
294 command.setParameter(name, value);
295 }
296
297
298 commandToReturn = command;
299
300
301
302 } else if (ci.equalsIgnoreCase("JOOSTFILTER")) {
303 JoostFilterCommand command = new JoostFilterCommand();
304 List params = new ArrayList();
305
306 command.setVar(cl[i++]);
307
308
309 command.setStyleRef(cl[i++]);
310
311
312 while (i < cl.length) {
313 String param = cl[i++];
314 StringTokenizer pst = new StringTokenizer(param, "=");
315 String name = pst.nextToken();
316 Object value = pst.nextToken();
317
318 command.setParameter(name, value);
319 }
320
321
322 commandToReturn = command;
323
324
325
326 } else if (ci.equalsIgnoreCase("TEECHAIN")
327 || ci.equalsIgnoreCase("TEE")) {
328 TeeFilterCommand command = new TeeFilterCommand();
329
330
331 command.setVar(cl[i++]);
332
333
334 while (i < (cl.length - 1)) {
335 String styleRef = cl[i++];
336
337 command.addFilterStyleRef(styleRef);
338 }
339
340
341 command.setResultRef(cl[i++]);
342
343
344 commandToReturn = command;
345
346
347 } else if (ci.equalsIgnoreCase("RETURN")) {
348 Ret command = new Ret();
349 List params = new ArrayList();
350
351
352 while (i < cl.length) {
353 params.add(cl[i++]);
354 }
355
356 command.setReturns((String[]) params.toArray(new String[0]));
357
358
359 commandToReturn = command;
360
361
362 } else if (ci.equalsIgnoreCase("DO")) {
363 Do command = new Do();
364 List params = new ArrayList();
365
366
367 command.setTemplateName(cl[i++]);
368
369
370 while (i < cl.length) {
371 String param = cl[i++];
372 StringTokenizer pst = new StringTokenizer(param, "=");
373 String name = pst.nextToken();
374 Object value = pst.nextToken();
375
376 command.setParameter(name, value);
377 }
378
379
380 commandToReturn = command;
381
382
383 } else if (ci.equalsIgnoreCase("INCLUDE")) {
384
385 String scriptName = cl[i++];
386
387 commandToReturn = compile(context.getFile(scriptName));
388
389
390
391 } else if (ci.equalsIgnoreCase("CALL")) {
392 Call command = new Call();
393 String name = cl[i++];
394 List params = new ArrayList();
395
396
397 while (i < cl.length) {
398 params.add(cl[i++]);
399 }
400
401 command.setName(name);
402 command.setPasses((String[]) params.toArray(new String[0]));
403
404
405 commandToReturn = command;
406
407
408
409 } else if (ci.equalsIgnoreCase("FUNCTION")) {
410 Function command = new Function();
411 String name = cl[i++];
412 List params = new ArrayList();
413
414
415 while (i < cl.length) {
416 params.add(cl[i++]);
417 }
418
419 command.setName(name);
420 command.setParams((String[]) params.toArray(new String[0]));
421
422 command.setCommand(compile(clr));
423
424
425 context.put(name, command);
426 commandToReturn = null;
427
428
429
430 } else if (ci.equalsIgnoreCase("TEMPLATE")) {
431 Template command = new Template();
432 String name = cl[i++];
433
434 command.setName(name);
435
436 StringBuffer tempBodyComp = new StringBuffer();
437 String line = clr.nextCommandLine();
438
439 while ((line != null)
440 && ((line.length() < 3) || !line.trim().substring(0, 3)
441 .equalsIgnoreCase("END"))) {
442 tempBodyComp.append(line);
443 tempBodyComp.append("\n");
444 line = clr.nextCommandLine();
445 }
446
447 command.setBody(tempBodyComp.toString());
448
449
450 context.put(name, command);
451 commandToReturn = null;
452
453
454 } else if (ci.equalsIgnoreCase("ERROR")) {
455 Err command = new Err();
456
457 if (i < cl.length) {
458 command.setError(Integer.parseInt(cl[i++]));
459 }
460
461
462 commandToReturn = command;
463
464
465 } else if (ci.equalsIgnoreCase("END")) {
466 End command = new End();
467
468
469 commandToReturn = command;
470
471
472 } else if (ci.equalsIgnoreCase("CATCH")) {
473 CatchCommand command = new CatchCommand();
474
475
476 commandToReturn = command;
477
478
479 } else if (ci.equalsIgnoreCase("TRY")) {
480 TryBlock command = new TryBlock();
481 Compiler blockBodyComp = new XTCompiler();
482
483 context.addCompiler(blockBodyComp);
484
485 command.setCommand(blockBodyComp.compile(clr));
486
487
488 commandToReturn = command;
489
490
491 } else if (ci.equalsIgnoreCase("HELP")) {
492 Help command = new Help();
493
494
495 commandToReturn = command;
496
497
498 } else if (ci.equalsIgnoreCase("ECHO")) {
499 Echo command = new Echo();
500
501 command.setEcho(cl[i++]);
502
503
504 commandToReturn = command;
505
506
507 } else if (ci.equalsIgnoreCase("CD")) {
508 Cd command = new Cd();
509
510 command.setFile(cl[i++]);
511
512
513 commandToReturn = command;
514
515
516 } else if (ci.equalsIgnoreCase("DOCUMENT")
517 || ci.equalsIgnoreCase("DOC")) {
518 Doc command = new Doc();
519
520 command.setVar(cl[i++]);
521
522
523 commandToReturn = command;
524
525
526 } else if (ci.equalsIgnoreCase("STRING")) {
527 Str command = new Str();
528
529 command.setVar(cl[i++]);
530
531 if (i < cl.length) {
532 command.setValue(cl[i++]);
533 }
534
535
536 commandToReturn = command;
537
538
539 } else if (ci.equalsIgnoreCase("FILE")) {
540 Fil command = new Fil();
541
542 command.setVar(cl[i++]);
543 command.setFile(cl[i++]);
544
545
546 commandToReturn = command;
547
548
549 } else if (ci.equalsIgnoreCase("STYLE")) {
550 Style command = new Style();
551
552 command.setVar(cl[i++]);
553 command.setStyleRef(cl[i++]);
554
555
556 commandToReturn = command;
557
558
559 } else if (ci.equalsIgnoreCase("JOOSTSTYLE")
560 || ci.equalsIgnoreCase("STXSTYLE")) {
561 JoostStyle command = new JoostStyle();
562
563 command.setVar(cl[i++]);
564 command.setStyleRef(cl[i++]);
565
566
567 commandToReturn = command;
568
569
570 } else if (ci.equalsIgnoreCase("LABEL")) {
571 Label lab = new Label();
572
573 lab.setName(cl[i]);
574
575
576 context.put(cl[i], lab);
577 i++;
578 commandToReturn = lab;
579
580
581
582 } else if (ci.equalsIgnoreCase("COMPILER")) {
583 String compClassName = cl[i++];
584 Compiler comp = CompilerFactory.createCompiler(compClassName,
585 context);
586
587 if (context.isVerbose()) {
588 System.out.println("Switching to compiler " + compClassName);
589 }
590
591 commandToReturn = comp.compile(clr);
592 context.removeCompiler();
593
594 if (context.isVerbose()) {
595 System.out.println("Back from compiler " + compClassName);
596 }
597
598
599 } else if (ci.startsWith(" ") || ci.startsWith("#")) {
600 commandToReturn = null;
601 } else {
602 return compileByNextCompiler(ci, cl, clr);
603 }
604
605 assertWholeLineRead(ci, i, cl, commandToReturn);
606
607
608 return commandToReturn;
609 }
610 }
611
612
613
614
615
616
617
618
619
620
621