1 package fr.ove.openmath.jome.formaters.mml;
2
3 import java.util.*;
4 import fr.ove.utils.*;
5 import fr.ove.openmath.jome.model.*;
6 import fr.ove.openmath.jome.formaters.mml.MathMLFormaterResoucesManager;
7
8 import java.awt.*;
9 import java.awt.event.*;
10 import fr.ove.openmath.jome.ctrl.linear.*;
11
12 /***
13 * Formats the formula tree structure as its corresponding MathML object.
14 *
15 * @author © 2000 DIRAT Laurent
16 * @version 1.0 23/05/2000
17 */
18 public class MathMLFormater extends FormaterRepository {
19 /***
20 * The resources manager of the formater
21 */
22 MathMLFormaterResoucesManager resourcesManager = new MathMLFormaterResoucesManager("fr.ove.openmath.jome.formaters.mml.MathMLFormaterResources");
23
24 /***
25 * The repository of formaters
26 */
27 private Hashtable repository = new Hashtable();
28
29 /***
30 * Returns the formater with the specified identifier.
31 * @param objectId the object (to format) identifier.
32 */
33 public Formater getFormater(String objectId) {
34 String formaterName = resourcesManager.getFormater(objectId);
35 formaterName = (formaterName == null) ? resourcesManager.getFormater("defaultFormater") : formaterName;
36
37 Formater formater = (Formater) repository.get(formaterName);
38 if (formater == null) {
39 formater = (Formater) Factory.getClassInstance(formaterName);
40 repository.put(formaterName, formater);
41 }
42
43 return formater;
44 }
45
46 /***
47 * Returns the specified object formatted as a string.
48 * @param formatedObject the formatted object (for structured object, could represents the beginning).
49 * @param repository where the different other formaters are.
50 * @param obj the object to format.
51 */
52 public String format(String formatedObject, FormaterRepository formaterRepository, Object obj) {
53 String objectId = ((FormulaTreeStructure) obj).getResourceIdentifier();
54 Formater formater = getFormater(objectId);
55 return formater.format(formatedObject, formaterRepository, obj);
56 }
57
58 /***
59 * Returns the element name of the specified property.
60 * @param objectId the object (to format) identifier.
61 */
62 public String getElementName(String objectId) {
63 return resourcesManager.getElementName(objectId);
64 }
65
66 /***
67 * Returns the MathML object of the specified formula tree structure.
68 * @param fts the specified formula tree structure.
69 */
70 public String getMathML(FormulaTreeStructure fts) {
71 return format("", this, fts);
72 }
73
74
75
76
77
78 /***
79 * Writes the MathML start object tag.
80 * @return the <mathml> tag.
81 */
82 public String writeStartObject() {
83 incIndent();
84 return "<mathml>\n";
85 }
86
87 /***
88 * Writes the MathML end object tag.
89 * @return the </mathml> tag.
90 */
91 public String writeEndObject() {
92 decIndent();
93 return "</mathml>\n";
94 }
95
96 /***
97 * Writes the MathML start application tag.
98 * @return the <apply> tag.
99 */
100 public String writeStartApplication() {
101 String indent = getIndent();
102 incIndent();
103 return indent + "<apply>\n";
104 }
105
106 /***
107 * Writes the MathML end application tag.
108 * @return the </apply> tag.
109 */
110 public String writeEndApplication() {
111 decIndent();
112 return getIndent() + "</apply>\n";
113 }
114
115 /***
116 * Writes the MathML start lambda tag.
117 * @return the <lambda> tag.
118 */
119 public String writeStartLambda() {
120 String indent = getIndent();
121 incIndent();
122 return indent + "<lambda>\n";
123 }
124
125 /***
126 * Writes the MathML end lambda tag.
127 * @return the </lambda> tag.
128 */
129 public String writeEndLambda() {
130 decIndent();
131 return getIndent() + "</lambda>\n";
132 }
133
134 /***
135 * Writes the MathML start bound variables tag.
136 * @return the <bvar> tag.
137 */
138 public String writeStartBoundVariables() {
139 String indent = getIndent();
140 incIndent();
141 return indent + "<bvar>\n";
142 }
143
144 /***
145 * Writes the MathML end bound variables tag.
146 * @return the </bvar> tag.
147 */
148 public String writeEndBoundVariables() {
149 decIndent();
150 return getIndent() + "</bvar>\n";
151 }
152
153 /***
154 * Writes a float (decimal format, not hexa).
155 * @param value the float value.
156 */
157 public String writeFloat(String value) {
158 return getIndent() + "<cn>" + value + "</cn>\n";
159 }
160
161 /***
162 * Writes an integer (decimal format, not hexa)
163 * @param value the integer value.
164 */
165 public String writeInteger(String value) {
166 return getIndent() + "<cn>" + value + "</cn>\n";
167 }
168
169 /***
170 * Writes an identifier.
171 * @param value the identifier value.
172 */
173 public String writeIdentifier(String value) {
174 return getIndent() + "<ci>" + value + "</ci>\n";
175 }
176
177 /***
178 * Writes the symbol element with the specified identifier.
179 * @param elementName the name of the element to write.
180 */
181 public String writeSymbol(String objectId) {
182 return getIndent() + "<"+resourcesManager.getElementName(objectId)+"/>\n";
183 }
184
185 /***
186 * Writes the empty element with the specified name.
187 * @param elementName the name of the element to write.
188 */
189 public String writeEmptyElement(String elementName) {
190 return getIndent() + "<"+elementName+"/>\n";
191 }
192
193 /***
194 * Writes the start element with the specified name.
195 * @param elementName the name of the element to write.
196 */
197 public String writeStartElement(String elementName) {
198 return getIndent() + "<"+elementName+">\n";
199 }
200
201 /***
202 * Writes the end element with the specified name.
203 * @param elementName the name of the element to write.
204 */
205 public String writeEndElement(String elementName) {
206 return getIndent() + "</"+elementName+">\n";
207 }
208
209 /***
210 * Test.
211 */
212 public static void main(String args[]) {
213 Frame f = new Frame();
214 f.setLayout(new BorderLayout());
215
216 TextArea ta = new TextArea();
217 f.add(ta, "Center");
218
219 TextField tf = new TextField();
220 tf.addActionListener(new TfAdapter(ta));
221 f.add(tf, "South");
222
223 f.pack();
224 f.setSize(f.getPreferredSize());
225 f.setVisible(true);
226 }
227 }
228
229 class TfAdapter implements ActionListener {
230 Formula f = new Formula();
231 LinearParser lp;
232 TextArea ta;
233 MathMLFormater omf = new MathMLFormater();
234
235 public TfAdapter(TextArea ta) {
236 this.ta = ta;
237 lp = new LinearParser();
238 lp.addLinearParserListener(f);
239 }
240
241 public void actionPerformed(ActionEvent e) {
242 TextField src = (TextField) e.getSource();
243 lp.parse(src.getText());
244
245 System.out.println(f.getLinear());
246
247 ta.setText(omf.getMathML(f));
248 }
249 }