View Javadoc

1   package fr.ove.openmath.jome.ctrl.mml;
2   
3   import java.io.*;
4   import java.util.*;
5   
6   import fr.ove.openmath.jome.ctrl.linear.events.*;
7   import fr.ove.openmath.jome.ctrl.mml.*;
8   
9   /***
10  * A parser for MathML objects.
11  *
12  * @author © 2000 DIRAT Laurent
13  * @version 1.0 25/05/2000
14  */
15  public class MathMLParser extends BasicXMLParser implements Serializable, LinearParserEventSource {
16      /***
17      * The handler for the parser.
18      */
19      private MathMLParserHandler handler = new MathMLParserHandler();
20      
21      /***
22      * The list of listeners of the instance
23      */
24      private Vector listeners = new Vector();
25      
26      /***
27      * The constructor.
28      */
29      public MathMLParser() {
30          handler.setEventSource(this);
31      }
32      
33          
34      /***
35      * Registers another listener of the instance.
36      * @param linearParserListener the listener to add.
37      */
38      public synchronized void addLinearParserListener(LinearParserListener linearParserListener) {
39          listeners.addElement(linearParserListener);
40      }
41  
42      /***
43      * Removes a listener.
44      * @param linearParserListener the listener to remove.
45      */
46      public synchronized void removeLinearParserListener(LinearParserListener linearParserListener) {
47          listeners.removeElement(linearParserListener);
48      }
49  
50      public void parseMathMLObject(String mmlObject) {
51          ByteArrayInputStream in = new ByteArrayInputStream(mmlObject.getBytes());
52          try {
53              parse(in);
54          }
55          catch(IOException ioe) {
56              ioe.printStackTrace();
57          }
58      }
59      
60      /***
61      * Fires a LinearParserEvent event to registered listeners.
62      * @param linearParserEvent event encapsulating relevant information.
63      */
64      public void fireLinearParserEvent(LinearParserEvent linearParserEvent) {
65          for (int i = 0; i < listeners.size(); i++)
66            ((LinearParserListener) listeners.elementAt(i)).consumeLinearParserEvent(linearParserEvent);
67      }
68      
69      /***
70      * Called by the parser when an tag has been parsed.
71      * @param tag the parsed tag.
72      */
73      public void readTag(Tag tag) {
74          String name = tag.getName();
75          
76          if (tag.isStart()) {
77              if (name.equals("mathml"))
78                  handler.startObject();
79              else if (name.equals("apply"))
80                  handler.startApplication();
81              else if (name.equals("lambda"))
82                  handler.startLambda();
83              else if (name.equals("bvar"))
84                  handler.startBVars();
85              else
86                  handler.startElement(tag);
87          }
88          else {
89              if (name.equals("mathml"))
90                  handler.endObject();
91              else if (name.equals("apply"))
92                  handler.endApplication();
93              else if (name.equals("lambda"))
94                  handler.startLambda();
95              else if (name.equals("bvar"))
96                  handler.startBVars();
97              else
98                  handler.endElement(tag);
99          }
100     }
101     
102     /***
103     * Called by the parser when the value of an element has been parsed.
104     * @param vale the parsed value.
105     */
106     public void readValue(String value) {
107         handler.readValue(value);
108     }
109 }