1 package fr.ove.openmath.mfd2.omparser;
2
3 import java.io.*;
4 import java.util.*;
5 import fr.inria.openmath.omapi.*;
6 import fr.ove.openmath.exceptions.*;
7
8 /***
9 * A handler for the OpenMath parser of the Mfd2 application.
10 *
11 */
12 public class Mfd2ParserHandler extends ParserAdapter {
13 private Stack stackIndentation = new Stack();
14 private String omObjectParsed = new String();
15 private String indentation = "";
16
17 private boolean isError = false;
18 private String errorObjects = new String();
19 private Symbol errorSymbol;
20
21 private ErrorMessageException eme = null;
22
23 /***
24 * Returns the OpenMath object parsed.
25 */
26 public String getOmObjectParsed() {
27 return omObjectParsed;
28 }
29
30 /***
31 * Throws an OpenMath exception.
32 * @param message the message to display.
33 * @param e a specific exception to throw.
34 * @see fr.inria.openmath.omapi.OMException
35 */
36 private void throwOME(String message, Exception e) throws OMException {
37 throw new OMException(message, e);
38 }
39
40 /***
41 * Throws an OpenMath exception.
42 * @param message the message to display.
43 * @param code the code of the excpetion thrown.
44 * @see fr.inria.openmath.omapi.OMException
45 */
46 private void throwOME(String message, short code) throws OMException {
47 throw new OMException(message, code);
48 }
49
50 /***
51 * Throws an OpenMath exception.
52 * @param message the message to display.
53 * @see fr.inria.openmath.omapi.OMException
54 */
55 private void throwOME(String message) throws OMException {
56 throwOME(message, OMException.OME_GENERIC);
57 }
58
59
60 /********************************************/
61
62
63
64 /********************************************/
65
66 /***
67 * Receives notification of the beginning of a object element.
68 *
69 * <p>By default, do nothing. Application writers may override this
70 * method in a subclass.</p>
71 *
72 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
73 * @see #endObject
74 */
75 public void startObject() throws OMException {
76 stackIndentation.removeAllElements();
77 omObjectParsed = "";
78 indentation = "";
79 isError = false;
80 errorObjects = "";
81 errorSymbol = null;
82 eme = null;
83
84 omObjectParsed += indentation + "<OMOBJ>\n";
85 stackIndentation.push(indentation);
86 indentation += " ";
87 }
88
89 /***
90 * Receives notification of the end of an object element.
91 *
92 * <p>By default, do nothing. Application writers may override this
93 * method in a subclass.</p>
94 *
95 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
96 * @see #startObject
97 */
98 public void endObject() throws OMException {
99 indentation = (String) stackIndentation.pop();
100
101 omObjectParsed += indentation + "</OMOBJ>";
102
103
104
105 if (eme != null)
106 throw new OMException(eme);
107 else if (isError)
108 throwErrorMessageException();
109 }
110
111 /***
112 * Receives notification of the beginning of a application element.
113 *
114 * <p>By default, do nothing. Application writers may override this
115 * method in a subclass.</p>
116 *
117 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
118 * @see #endApplication
119 */
120 public void startApplication() throws OMException {
121 omObjectParsed += indentation + "<OMA>\n";
122 stackIndentation.push(indentation);
123
124 if (isError)
125 errorObjects += indentation + "<OMA>\n";
126
127 indentation += " ";
128 }
129
130 /***
131 * Receives notification of the end of an application element.
132 *
133 * <p>By default, do nothing. Application writers may override this
134 * method in a subclass.</p>
135 *
136 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
137 * @see #startApplication
138 */
139 public void endApplication() throws OMException {
140 indentation = (String) stackIndentation.pop();
141
142 omObjectParsed += indentation + "</OMA>\n";
143
144 if (isError)
145 errorObjects += indentation + "</OMA>\n";
146 }
147
148
149 /***
150 * Receives notification of the beginning of a attribution element.
151 *
152 * <p>By default, do nothing. Application writers may override this
153 * method in a subclass.</p>
154 *
155 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
156 * @see #endAttribution
157 */
158 public void startAttribution() throws OMException {
159 omObjectParsed += indentation + "<OMATTR>\n";
160 stackIndentation.push(indentation);
161
162 if (isError)
163 errorObjects += indentation + "<OMATTR>\n";
164
165 indentation += " ";
166 }
167
168 /***
169 * Receives notification of the end of an attribution element.
170 *
171 * <p>By default, do nothing. Application writers may override this
172 * method in a subclass.</p>
173 *
174 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
175 * @see #startAttribution
176 */
177 public void endAttribution() throws OMException {
178 indentation = (String) stackIndentation.pop();
179
180 omObjectParsed += indentation + "</OMATTR>\n";
181
182 if (isError)
183 errorObjects += indentation + "</OMATTR>\n";
184 }
185
186 /***
187 * Receives notification of the beginning of an attribute-pairs element.
188 *
189 * <p>By default, do nothing. Application writers may override this
190 * method in a subclass.</p>
191 *
192 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
193 *
194 * @see #endAttributePairs
195 */
196 public void startAttributePairs() throws OMException {
197 omObjectParsed += indentation + "<OMATP>\n";
198 stackIndentation.push(indentation);
199
200 if (isError)
201 errorObjects += indentation + "<OMATP>\n";
202
203 indentation += " ";
204 }
205
206 /***
207 * Receives notification of the end of an attribute-pairs element.
208 *
209 * <p>By default, do nothing. Application writers may override this
210 * method in a subclass.</p>
211 *
212 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
213 * @see #startAttribution
214 */
215 public void endAttributePairs() throws OMException {
216 indentation = (String) stackIndentation.pop();
217
218 omObjectParsed += indentation + "</OMATP>\n";
219
220 if (isError)
221 errorObjects += indentation + "</OMATP>\n";
222 }
223
224 /***
225 * Receives notification of the beginning of a Bind element.
226 *
227 * <p>By default, do nothing. Application writers may override this
228 * method in a subclass.</p>
229 *
230 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
231 *
232 * @see #endBind
233 */
234 public void startBind() throws OMException {
235 omObjectParsed += indentation + "<OMBIND>\n";
236 stackIndentation.push(indentation);
237
238 if (isError)
239 errorObjects += indentation + "<OMBIND>\n";
240
241 indentation += " ";
242 }
243
244 /***
245 * Receives notification of the beginning of a BVars element.
246 *
247 * <p>By default, do nothing. Application writers may override this
248 * method in a subclass.</p>
249 *
250 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
251 *
252 * @see #endBVars
253 */
254 public void startBVars() throws OMException {
255 omObjectParsed += indentation + "<OMBVAR>\n";
256 stackIndentation.push(indentation);
257
258 if (isError)
259 errorObjects += indentation + "<OMBVAR>\n";
260
261 indentation += " ";
262 }
263
264 /***
265 * Receives notification of the beginning of a BVars element.
266 *
267 * <p>By default, do nothing. Application writers may override this
268 * method in a subclass.</p>
269 *
270 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
271 *
272 * @see #startBVars
273 */
274 public void endBVars() throws OMException {
275 indentation = (String) stackIndentation.pop();
276
277 omObjectParsed += indentation + "</OMBVAR>\n";
278
279 if (isError)
280 errorObjects += indentation + "</OMBVAR>\n";
281 }
282
283
284 /***
285 * Receives notification of the beginning of a Bind element.
286 *
287 * <p>By default, do nothing. Application writers may override this
288 * method in a subclass.</p>
289 *
290 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
291 *
292 * @see #endBind
293 */
294 public void endBind() throws OMException {
295 indentation = (String) stackIndentation.pop();
296
297 omObjectParsed += indentation + "</OMBIND>\n";
298
299 if (isError)
300 errorObjects += indentation + "</OMBIND>\n";
301 }
302
303 /***
304 * Receives notification of the beginning of a error element.
305 *
306 * <p>By default, do nothing. Application writers may override this
307 * method in a subclass.</p>
308 *
309 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
310 *
311 * @see #endError
312 */
313 public void startError() throws OMException {
314 omObjectParsed += indentation + "<OME>\n";
315 stackIndentation.push(indentation);
316
317 if (isError)
318 errorObjects += indentation + "<OMATTR>\n";
319 else
320 isError = true;
321
322 indentation += " ";
323 }
324
325 /***
326 * Receives notification of the end of an error element.
327 *
328 * <p>By default, do nothing. Application writers may override this
329 * method in a subclass.</p>
330 *
331 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
332 * @see #startError
333 */
334 public void endError() throws OMException {
335 indentation = (String) stackIndentation.pop();
336
337 omObjectParsed += indentation + "</OME>\n";
338
339 if (isError)
340 errorObjects += indentation + "</OME>\n";
341 }
342
343
344 /***
345 * Receives notification of an Integer element.
346 *
347 * <p>By default, do nothing. Application writers may override this
348 * method in a subclass.</p>
349 *
350 * @param value the value embeded in this Integer element. This is an infinite precision integer.
351 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
352 * @see fr.inria.openmath.omapi.BigInt
353 */
354 public void readInteger(BigInt value) throws OMException {
355 String strValue = (value.getSign() == 1) ? value.getDigits() : "-" + value.getDigits();
356
357 omObjectParsed += indentation + "<OMI> " + strValue + " </OMI>\n";
358
359 if (isError)
360 errorObjects += indentation + "<OMI> " + strValue + " </OMI>\n";
361 }
362
363 /***
364 * Receives notification of a floating-point number element.
365 *
366 * <p>By default, do nothing. Application writers may override this
367 * method in a subclass.</p>
368 *
369 * @param value the value embeded in this Float element.
370 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
371 *
372 */
373 public void readFloat(double value) throws OMException {
374 omObjectParsed += indentation + "<OMF dec = \"" + value + " \"/>\n";
375
376 if (isError)
377 errorObjects += indentation + "<OMF dec = \"" + value + " \"/>\n";
378 }
379
380 /***
381 * Receives notification of a string element.
382 *
383 * <p>By default, do nothing. Application writers may override this
384 * method in a subclass.</p>
385 *
386 * @param value the value embeded in this String element.
387 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
388 *
389 */
390 public void readString(String value) throws OMException {
391 omObjectParsed += indentation + "<OMSTR> " + value + " </OMSTR>\n";
392
393 if (isError)
394 errorObjects += indentation + "<OMSTR> " + value + " </OMSTR>\n";
395 }
396
397 /***
398 * Receives notification of a variable element.
399 *
400 * <p>By default, do nothing. Application writers may override this
401 * method in a subclass.</p>
402 *
403 * @param name the name of this Variable element.
404 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
405 *
406 */
407 public void readVariable(String name) throws OMException {
408 omObjectParsed += indentation + "<OMV name=\""+ name +"\"/> \n";
409
410 if (isError)
411 errorObjects += indentation + "<OMV name=\""+ name +"\"/> \n";
412 }
413
414 /***
415 * Receives notification of a byteArray element.
416 *
417 * <p>By default, do nothing. Application writers may override this
418 * method in a subclass.</p>
419 *
420 * @param value the value embeded in this ByteArray element.
421 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
422 *
423 */
424 public void readByteArray(byte[] value) throws OMException {
425 }
426
427 /***
428 * Receives notification of a symbol element.
429 *
430 * <p>By default, do nothing. Application writers may override this
431 * method in a subclass.</p>
432 *
433 * @param value the value embeded in this Symbol element.
434 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
435 * @see Symbol
436 */
437 public void readSymbol(Symbol value) throws OMException {
438 byte errorCode;
439
440 omObjectParsed += indentation + "<OMS cd=\""+value.getCD()+"\" name=\""+value.getName()+"\"/> \n";
441
442 if (isError) {
443 if (errorSymbol == null)
444 errorSymbol = value;
445 else
446 errorObjects += indentation + "<OMS cd=\""+value.getCD()+"\" name=\""+value.getName()+"\"/> \n";
447
448 return;
449 }
450 else
451 filterSymbol(value);
452 }
453
454 /***
455 * Receives notification of a comment element.
456 *
457 * <p>By default, do nothing. Application writers may override this
458 * method in a subclass.</p>
459 *
460 * @param value the value embeded in this Comment element.
461 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
462 * @see Comment
463 */
464 public void readComment(String value) throws OMException {}
465
466 private void filterSymbol(Symbol value) throws OMException {
467
468 if (value.getCD().equals("mfd2")) {
469 if (value.getName().equals("dontknow"))
470 eme = new Mfd2Exception("dontknow");
471 else if (value.getName().equals("wrongRequest"))
472 eme = new Mfd2Exception("wrongRequest");
473 return;
474 }
475
476
477 if (value.getCD().equals("control")) {
478 eme = new ControlException(value.getName());
479 return;
480 }
481
482 }
483
484 private void throwErrorMessageException() throws OMException {
485 if (errorSymbol.getCD().equals("mfd2")) {
486
487 throw new OMException(new Mfd2Exception(errorSymbol.getName()));
488 }
489 else if (errorSymbol.getCD().equals("control"))
490 throw new OMException(new ControlException(errorSymbol.getName(), errorObjects));
491 }
492
493 /***
494 * error must be : unhandledSymbol, or unknownCD or unknownSymbol
495 */
496 private void throwControlException(Symbol symbol, String error) throws OMException {
497 ControlException controlException = new ControlException(error, symbol);
498 throw new OMException(controlException);
499 }
500 }