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