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 * @author tomp TODO To change the template for this generated type comment go
18 * to Window - Preferences - Java - Code Style - Code Templates
19 */
20 public abstract class CompilerBase implements Compiler {
21 private Context context;
22
23 /***
24 * Compiles commands from a CommandLineReader into a command Sequence
25 *
26 * @param clr CommandLineReader - the source for compilation
27 * @return DOCUMENT ME!
28 * @throws Exception DOCUMENT ME!
29 */
30 public Command compile(CommandLineReader clr) throws Exception {
31 String line = clr.nextCommandLine();
32 List lines = new ArrayList();
33
34 while (line != null) {
35 Command command = compile(line, clr);
36
37 if (command instanceof End) {
38 break;
39 }
40
41 if (command != null) {
42 lines.add(command);
43
44
45 }
46
47 line = clr.nextCommandLine();
48 }
49
50 Sequence c = new Sequence();
51
52 c.setCommands((Command[]) lines.toArray(new Command[0]));
53
54 return c;
55 }
56
57 /***
58 * Compiles commands from a String
59 *
60 * @param f String - the source from which it compiles
61 * @return DOCUMENT ME!
62 * @throws Exception DOCUMENT ME!
63 */
64 public Command compile(File f) throws Exception {
65 return compile(new CommandLineReader(f));
66 }
67
68 /***
69 * Compiles commands from a String
70 *
71 * @param s String - the source from which it compiles
72 * @return DOCUMENT ME!
73 * @throws Exception DOCUMENT ME!
74 */
75 public Command compile(String s) throws Exception {
76 return compile(new CommandLineReader(s));
77 }
78
79 /***
80 * Compiles commands from a String followed by a CommandLineReader into a
81 * command Sequence
82 *
83 * @param cl String - will be tokenized and passed further to
84 * compile(String[], CommandLineReader)
85 * @param clr CommandLineReader - the sources for compilation first the
86 * String, then the CLR
87 * @return DOCUMENT ME!
88 * @throws Exception DOCUMENT ME!
89 */
90 public Command compile(String cl, CommandLineReader clr) throws Exception {
91 List com = new ArrayList();
92
93 StringTokenizer st = new Tokenizer(cl.trim());
94
95 while (st.hasMoreTokens()) {
96 com.add(st.nextToken());
97 }
98
99 return compile((String[]) com.toArray(new String[0]), clr);
100 }
101
102 /***
103 * @param ci
104 * @param i
105 * @param cl
106 * @param commandToReturn
107 */
108 protected void assertWholeLineRead(String ci, int i, String[] cl,
109 Command commandToReturn) {
110 if ((commandToReturn != null) && (i != cl.length)) {
111 throw new IllegalArgumentException(getClass().getName()
112 + " - invalid parameters for command " + ci);
113 }
114 }
115
116 /***
117 * @param ci
118 * @param cl
119 * @param clr
120 * @return @throws Exception
121 */
122 protected Command compileByNextCompiler(String ci, String[] cl,
123 CommandLineReader clr) throws Exception {
124 Compiler next = getNextCompiler();
125
126 if (next == null) {
127 throw new UnknownCommandException(ci);
128 } else {
129 return next.compile(cl, clr);
130 }
131 }
132
133 /***
134 * @return Returns the context.
135 */
136 public Context getContext() {
137 return context;
138 }
139
140 /***
141 * @return Returns the parent.
142 */
143 public Compiler getNextCompiler() {
144 return context.getNextCompiler(this);
145 }
146
147 /***
148 * @param context The context to set.
149 */
150 public void setContext(Context context) {
151 this.context = context;
152 }
153 }
154
155 /***
156 * Tokenizer for parsing parameters from the String from the line
157 */
158
159 class Tokenizer extends StringTokenizer {
160 /***
161 * Creates a new Tokenizer object.
162 *
163 * @param s DOCUMENT ME!
164 */
165 public Tokenizer(String s) {
166 super(s);
167 }
168
169
170
171
172
173 /***
174 * DOCUMENT ME!
175 *
176 * @return the next token
177 */
178 public String nextToken() {
179 StringBuffer token = new StringBuffer();
180
181 String n = super.nextToken();
182
183 token.append(n.replaceAll("//\"", ""));
184
185 if (n.indexOf('"') >= 0) {
186 do {
187 token.append(' ');
188 n = super.nextToken();
189 token.append(n.replaceAll("//\"", ""));
190 } while (n.indexOf('"') < 0);
191 }
192
193 return token.toString();
194 }
195 }