View Javadoc

1   package fr.ove.openmath.jome.ctrl.mml;
2   
3   import java.util.*;
4   import fr.ove.openmath.jome.ctrl.linear.events.*;
5   import fr.ove.openmath.jome.ctrl.amto.*;
6   import fr.ove.openmath.jome.ctrl.mml.*;
7   
8   /***
9   * A handler for the MathML objects parser.
10  *
11  * @author © 2000 DIRAT Laurent
12  * @version 1.0 25/05/2000
13  */
14  public class MathMLParserHandler {
15      private MathMLParserResourcesManager resourcesManager;
16      
17      private AbstractMathTreeObject amto;
18      private MathExpression mathExp;
19      
20      /***
21      * A stack for symbol management.
22      */
23      private Stack stackSymbol = new Stack(); // Le sommet de cette pile reprÈsente le symbol
24      
25      /***
26      * The constructor.<BR>
27      * formula must be the root of an empty one.
28      */
29      public MathMLParserHandler() {
30          resourcesManager = new MathMLParserResourcesManager("fr.ove.openmath.jome.ctrl.mml.MathMLParserResources");
31          mathExp = new MathExpression();
32      }    
33      
34      /***
35      * Sets the event source.<BR>
36      * Even if it is the instance which fires the events during the parsing, the instance (as a handler), just...
37      * handles the parser. Basically, in a common sense, it is the parser which fires the events. We try to respect
38      * the common sense by setting the right event source : the parser.
39      * @param eventSource the event source
40      */
41      public void setEventSource(LinearParserEventSource eventSource) {
42          mathExp.setEventSource(eventSource);
43      }
44      
45      /***
46      * Receives notification of the beginning of a object element.
47      */
48      public void startObject() {
49          System.out.println("Starting object");
50          mathExp.removeAll();
51          amto = mathExp;
52          
53          stackSymbol.push(new Tag("mathml"));
54      }
55  
56      /***
57      * Receives notification of the end of an object element.
58      */
59      public void endObject() {
60          System.out.println("ending object");
61          mathExp.flush();
62          
63          Tag tag = (Tag) stackSymbol.pop();
64          if (!tag.getName().equals("mathml"))
65              System.out.println("Dommage :o(");
66      }
67  
68      /***
69      * Receives notification of the beginning of a application element.
70      */
71      public void startApplication() {
72          System.out.println("Starting application");
73          stackSymbol.push(new Tag("apply"));
74      }
75  
76      /***
77      * Receives notification of the end of an application element.
78      */
79      public void endApplication() {
80          System.out.println("ending application");
81          Tag tag  = (Tag) stackSymbol.peek();        
82          if (!tag.getName().equals("apply"))
83              System.out.println("Dommage :o(");
84           else
85              amto = amto.getFather();
86      }
87  
88      /***
89      * Receives notification of the beginning of a lambda element.
90      */
91      public void startLambda() {
92          System.out.println("Starting lambda");
93          stackSymbol.push("lambda");
94      }
95  
96      /***
97      * Receives notification of the beginning of a bvar element.
98      */
99      public void startBVars() {
100         System.out.println("Starting bound variables");
101         stackSymbol.push("bvar");
102     }
103 
104     /***
105     * Receives notification of the end of a bvar element.
106     */
107     public void endBVars() {
108         System.out.println("Ending bound variables");
109         Tag tag  = (Tag) stackSymbol.peek();        
110         if (!tag.equals("bvar"))
111             System.out.println("Fuck you.");
112     }
113 
114     /***
115     * Receives notification of the end of a lambda element.
116      */
117     public void endLambda() {
118         System.out.println("Ending lambda");
119         Tag tag  = (Tag) stackSymbol.peek();        
120         if (!tag.equals("lambda"))
121             System.out.println("Fuck you.");
122     }
123 
124     /***
125     * Receives notification of the beginning of an element.
126     */
127     public void startElement(Tag tag) {
128         System.out.println("Starting elements : " + tag.toString());
129         if (!tag.isEmpty())
130             stackSymbol.push(tag);
131             
132         String name = tag.getName();
133         
134         if (name.equals("cn")) {
135             amto = amto.add("integer", null);
136         }
137         else if (name.equals("ci")) {
138             amto = amto.add("variable", null);
139         }
140         else {
141             String identifier = resourcesManager.getIdentifier(name);
142             String val = resourcesManager.getValue(name);
143             if (val.equals("null"))
144                 val = null;
145                 
146             amto = amto.add(identifier, val);
147         }
148     }
149 
150     /***
151     * Receives notification of the end of an element.
152     */
153     public void endElement(Tag tag) {
154         System.out.println("Ending elements : " + tag.toString());
155         Tag topSymbol = (Tag) stackSymbol.pop();        
156         if (!tag.isEndTag(topSymbol))
157             System.out.println("Fuck you.");
158         else
159             amto = amto.getFather();
160     }
161     
162     /***
163     * Receives notification of the read of an element value.
164     */
165     public void readValue(String value) {
166         System.out.println("read value : " + value);
167         amto.setParameter(value);
168     }
169 }