1 package fr.ove.openmath.mathematica.omparser;
2
3 import java.io.*;
4 import java.util.*;
5 import fr.inria.openmath.omapi.*;
6
7 /***
8 * Handler for an OpenMath parser. Keeps a copy of the object parsed.
9 *
10 */
11 public class PipeParserHandler extends ParserAdapter {
12 private Stack stackIndentation = new Stack();
13 private StringBuffer omObjectParsed = new StringBuffer();
14 private String indentation = "";
15
16 /***
17 * Returns the OpenMath object parsed.
18 */
19 public String getOmObjectParsed() {
20 return omObjectParsed.toString();
21 }
22
23 /***
24 * Throws an OpenMath exception.
25 * @param message the message to display.
26 * @param e a specific exception to throw.
27 * @see fr.inria.openmath.omapi.OMException
28 */
29 private void throwOME(String message, Exception e) throws OMException {
30 throw new OMException(message, e);
31 }
32
33 /***
34 * Throws an OpenMath exception.
35 * @param message the message to display.
36 * @param code the code of the excpetion thrown.
37 * @see fr.inria.openmath.omapi.OMException
38 */
39 private void throwOME(String message, short code) throws OMException {
40 throw new OMException(message, code);
41 }
42
43 /***
44 * Throws an OpenMath exception.
45 * @param message the message to display.
46 * @see fr.inria.openmath.omapi.OMException
47 */
48 private void throwOME(String message) throws OMException {
49 throwOME(message, OMException.OME_GENERIC);
50 }
51
52
53 /********************************************/
54
55
56
57 /********************************************/
58
59
60 /***
61 * Receives notification of the end of parse.
62 *
63 * <p>By default, do nothing. Application writers may override this
64 * method in a subclass.</p>
65 *
66 * <p>The OpenMath parser will invoke this method only once, and it will
67 * be the last method invoked during the parse. The parser shall
68 * not invoke this method until it has either abandoned parsing
69 * (because of an unrecoverable error) or reached the end of
70 * the input (no more OpenMath objects).</p>
71 *
72 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
73 */
74 public void endParse() throws OMException {
75 omObjectParsed.setLength(0);
76 }
77
78 /***
79 * Receives notification of the beginning of a object element.
80 *
81 * <p>By default, do nothing. Application writers may override this
82 * method in a subclass.</p>
83 *
84 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
85 * @see #endObject
86 */
87 public void startObject() throws OMException {
88 stackIndentation.removeAllElements();
89 omObjectParsed.setLength(0);
90 indentation = "";
91
92 omObjectParsed.append("<OMOBJ>\n");
93 stackIndentation.push(indentation);
94 indentation += " ";
95 }
96
97 /***
98 * Receives notification of the end of an object element.
99 *
100 * <p>By default, do nothing. Application writers may override this
101 * method in a subclass.</p>
102 *
103 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
104 * @see #startObject
105 */
106 public void endObject() throws OMException {
107 indentation = (String) stackIndentation.pop();
108 omObjectParsed.append(indentation + "</OMOBJ>");
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.append(indentation + "<OMA>\n");
122 stackIndentation.push(indentation);
123 indentation += " ";
124 }
125
126 /***
127 * Receives notification of the end of an application element.
128 *
129 * <p>By default, do nothing. Application writers may override this
130 * method in a subclass.</p>
131 *
132 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
133 * @see #startApplication
134 */
135 public void endApplication() throws OMException {
136 indentation = (String) stackIndentation.pop();
137 omObjectParsed.append(indentation + "</OMA>\n");
138 }
139
140
141 /***
142 * Receives notification of the beginning of a attribution element.
143 *
144 * <p>By default, do nothing. Application writers may override this
145 * method in a subclass.</p>
146 *
147 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
148 * @see #endAttribution
149 */
150 public void startAttribution() throws OMException {
151 omObjectParsed.append(indentation + "<OMATTR>\n");
152 stackIndentation.push(indentation);
153 indentation += " ";
154 }
155
156 /***
157 * Receives notification of the end of an attribution element.
158 *
159 * <p>By default, do nothing. Application writers may override this
160 * method in a subclass.</p>
161 *
162 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
163 * @see #startAttribution
164 */
165 public void endAttribution() throws OMException {
166 indentation = (String) stackIndentation.pop();
167 omObjectParsed.append(indentation + "</OMATTR>\n");
168 }
169
170 /***
171 * Receives notification of the beginning of an attribute-pairs element.
172 *
173 * <p>By default, do nothing. Application writers may override this
174 * method in a subclass.</p>
175 *
176 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
177 *
178 * @see #endAttributePairs
179 */
180 public void startAttributePairs() throws OMException {
181 omObjectParsed.append(indentation + "<OMATP>\n");
182 stackIndentation.push(indentation);
183 indentation += " ";
184 }
185
186 /***
187 * Receives notification of the end 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 * @see #startAttribution
194 */
195 public void endAttributePairs() throws OMException {
196 indentation = (String) stackIndentation.pop();
197 omObjectParsed.append(indentation + "</OMATP>\n");
198 }
199
200 /***
201 * Receives notification of the beginning of a Bind element.
202 *
203 * <p>By default, do nothing. Application writers may override this
204 * method in a subclass.</p>
205 *
206 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
207 *
208 * @see #endBind
209 */
210 public void startBind() throws OMException {
211 omObjectParsed.append(indentation + "<OMBIND>\n");
212 stackIndentation.push(indentation);
213 indentation += " ";
214 }
215
216 /***
217 * Receives notification of the beginning of a BVars element.
218 *
219 * <p>By default, do nothing. Application writers may override this
220 * method in a subclass.</p>
221 *
222 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
223 *
224 * @see #endBVars
225 */
226 public void startBVars() throws OMException {
227 omObjectParsed.append(indentation + "<OMBVAR>\n");
228 stackIndentation.push(indentation);
229 indentation += " ";
230 }
231
232 /***
233 * Receives notification of the beginning of a BVars element.
234 *
235 * <p>By default, do nothing. Application writers may override this
236 * method in a subclass.</p>
237 *
238 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
239 *
240 * @see #startBVars
241 */
242 public void endBVars() throws OMException {
243 indentation = (String) stackIndentation.pop();
244 omObjectParsed.append(indentation + "</OMBVAR>\n");
245 }
246
247
248 /***
249 * Receives notification of the beginning of a Bind element.
250 *
251 * <p>By default, do nothing. Application writers may override this
252 * method in a subclass.</p>
253 *
254 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
255 *
256 * @see #endBind
257 */
258 public void endBind() throws OMException {
259 indentation = (String) stackIndentation.pop();
260 omObjectParsed.append(indentation + "</OMBIND>\n");
261 }
262
263 /***
264 * Receives notification of the beginning of a error element.
265 *
266 * <p>By default, do nothing. Application writers may override this
267 * method in a subclass.</p>
268 *
269 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
270 *
271 * @see #endError
272 */
273 public void startError() throws OMException {
274 omObjectParsed.append(indentation + "<OME>\n");
275 stackIndentation.push(indentation);
276 indentation += " ";
277 }
278
279 /***
280 * Receives notification of the end of an error element.
281 *
282 * <p>By default, do nothing. Application writers may override this
283 * method in a subclass.</p>
284 *
285 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
286 * @see #startError
287 */
288 public void endError() throws OMException {
289 indentation = (String) stackIndentation.pop();
290 omObjectParsed.append(indentation + "</OME>\n");
291 }
292
293
294 /***
295 * Receives notification of an Integer element.
296 *
297 * <p>By default, do nothing. Application writers may override this
298 * method in a subclass.</p>
299 *
300 * @param value the value embeded in this Integer element. This is an infinite precision integer.
301 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
302 * @see fr.inria.openmath.omapi.BigInt
303 */
304 public void readInteger(BigInt value) throws OMException {
305 String strValue = (value.getSign() == 1) ? value.getDigits() : "-" + value.getDigits();
306 omObjectParsed.append(indentation + "<OMI>" + strValue + "</OMI>\n");
307 }
308
309 /***
310 * Receives notification of a floating-point number element.
311 *
312 * <p>By default, do nothing. Application writers may override this
313 * method in a subclass.</p>
314 *
315 * @param value the value embeded in this Float element.
316 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
317 *
318 */
319 public void readFloat(double value) throws OMException {
320 omObjectParsed.append(indentation + "<OMF dec = \"" + value + " \"/>\n");
321 }
322
323 /***
324 * Receives notification of a string element.
325 *
326 * <p>By default, do nothing. Application writers may override this
327 * method in a subclass.</p>
328 *
329 * @param value the value embeded in this String element.
330 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
331 *
332 */
333 public void readString(String value) throws OMException {
334 omObjectParsed.append(indentation + "<OMSTR>" + value + "</OMSTR>\n");
335 }
336
337 /***
338 * Receives notification of a variable element.
339 *
340 * <p>By default, do nothing. Application writers may override this
341 * method in a subclass.</p>
342 *
343 * @param name the name of this Variable element.
344 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
345 *
346 */
347 public void readVariable(String name) throws OMException {
348 omObjectParsed.append(indentation + "<OMV name=\""+ name +"\"/> \n");
349 }
350
351 /***
352 * Receives notification of a byteArray element.
353 *
354 * <p>By default, do nothing. Application writers may override this
355 * method in a subclass.</p>
356 *
357 * @param value the value embeded in this ByteArray element.
358 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
359 *
360 */
361 public void readByteArray(byte[] value) throws OMException {
362 }
363
364 /***
365 * Receives notification of a symbol element.
366 *
367 * <p>By default, do nothing. Application writers may override this
368 * method in a subclass.</p>
369 *
370 * @param value the value embeded in this Symbol element.
371 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
372 * @see Symbol
373 */
374 public void readSymbol(Symbol value) throws OMException {
375 omObjectParsed.append(indentation + "<OMS cd=\""+value.getCD()+"\" name=\""+value.getName()+"\"/> \n");
376 }
377
378 /***
379 * Receives notification of a comment element.
380 *
381 * <p>By default, do nothing. Application writers may override this
382 * method in a subclass.</p>
383 *
384 * @param value the value embeded in this Comment element.
385 * @exception fr.inria.openmath.omapi.OMException Any OpenMath exception.
386 * @see Comment
387 */
388 public void readComment(String value) throws OMException {}
389 }