View Javadoc

1   package fr.ove.openmath.jome.formaters.om;
2   
3   import java.util.*;
4   import fr.ove.utils.*;
5   import fr.ove.openmath.jome.model.*;
6   import fr.ove.openmath.jome.formaters.om.OpenMathFormaterResoucesManager;
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 OpenMath object.
14  *
15  * @author © 2000 DIRAT Laurent
16  * @version 1.0 07/01/2000
17  */
18  public class OpenMathFormater extends FormaterRepository {
19      /***
20      * The resources manager of the formater
21      */
22      OpenMathFormaterResoucesManager resourcesManager = new OpenMathFormaterResoucesManager("fr.ove.openmath.jome.formaters.om.OpenMathFormaterResources");
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); // En fait, le formatage ? proprement parlÈ commence l?
56      }
57      
58      /***
59      * Returns the cd name of the specified property.
60      * @param objectId the object (to format) identifier.
61      */
62      public String getCdName(String objectId) {
63          return resourcesManager.getCdName(objectId);        
64      }
65      
66      /***
67      * Returns the symbol name of the specified property.
68      * @param objectId the object (to format) identifier.
69      */
70      public String getSymbolName(String objectId) {
71          return resourcesManager.getSymbolName(objectId);        
72      }
73      
74      /***
75      * Returns the OpenMath object of the specified formula tree structure.
76      * @param fts the specified formula tree structure.
77      */
78      public String getOpenMath(FormulaTreeStructure fts) {
79          return format("", this, fts);
80      }
81      
82      /*
83      * Methods to write OpenMath
84      */
85      
86      /***
87      * Writes the OpenMath start object tag.
88      * @return the <OMOBJ> tag.
89      */
90      public String writeStartObject() {
91          incIndent();
92          return "<OMOBJ>\n";
93      }
94      
95      /***
96      * Writes the OpenMath end object tag.
97      * @return the </OMOBJ> tag.
98      */
99      public String writeEndObject() {
100         decIndent();
101         return "</OMOBJ>\n";
102     }
103     
104     /***
105     * Writes the OpenMath start application tag.
106     * @return the <OMA> tag.
107     */
108     public String writeStartApplication() {
109         String indent = getIndent();
110         incIndent();
111         return indent + "<OMA>\n";
112     }
113     
114     /***
115     * Writes the OpenMath end application tag.
116     * @return the </OMA> tag.
117     */
118     public String writeEndApplication() {
119         decIndent();
120         return getIndent() + "</OMA>\n";
121     }
122     
123     /***
124     * Writes the OpenMath start error tag.
125     * @return the <OMe> tag.
126     */
127     public String writeStartError() {
128         String indent = getIndent();
129         incIndent();
130         return indent + "<OME>\n";
131     }
132     
133     /***
134     * Writes the OpenMath end error tag.
135     * @return the </OME> tag.
136     */
137     public String writeEndError() {
138         decIndent();
139         return getIndent() + "</OME>\n";
140     }
141     
142     /***
143     * Writes the OpenMath start binding tag.
144     * @return the <OMBIND> tag.
145     */
146     public String writeStartBinding() {
147         String indent = getIndent();
148         incIndent();
149         return indent + "<OMBIND>\n";
150     }
151     
152     /***
153     * Writes the OpenMath end binding tag.
154     * @return the </OMBIND> tag.
155     */
156     public String writeEndBinding() {
157         decIndent();
158         return getIndent() + "</OMBIND>\n";
159     }
160     
161     /***
162     * Writes the OpenMath start bound variables tag.
163     * @return the <OMBVAR> tag.
164     */
165     public String writeStartBoundVariables() {
166         String indent = getIndent();
167         incIndent();
168         return indent + "<OMBVAR>\n";
169     }
170     
171     /***
172     * Writes the OpenMath end bound variables tag.
173     * @return the </OMBVAR> tag.
174     */
175     public String writeEndBoundVariables() {
176         decIndent();
177         return getIndent() + "</OMBVAR>\n";
178     }
179     
180     /***
181     * Writes the OpenMath start attribution tag.
182     * @return the <OMATP> tag.
183     */
184     public String writeStartAttribution() {
185         String indent = getIndent();
186         incIndent();
187         return indent + "<OMATP>\n";
188     }
189     
190     /***
191     * Writes the OpenMath end attribution tag.
192     * @return the </OMATP> tag.
193     */
194     public String writeEndAttribution() {
195         decIndent();
196         return getIndent() + "</OMATP>\n";
197     }
198     
199     /***
200     * Writes the OpenMath start attribute pair tag.
201     * @return the <OMATTR> tag.
202     */
203     public String writeStartAttributePair() {
204         String indent = getIndent();
205         incIndent();
206         return getIndent() + "<OMATTR>\n";
207     }
208     
209     /***
210     * Writes the OpenMath end attribute pair tag.
211     * @return the </OMATTR> tag.
212     */
213     public String writeEndAttributePair() {
214         decIndent();
215         return getIndent() + "</OMATTR>\n";
216     }
217     
218     /***
219     * Writes a float (decimal format, not hexa).
220     * @param value the float value.
221     */
222     public String writeFloat(String value) {
223         return getIndent() + "<OMF dec = \"" + value + "\"/>\n";
224     }
225     
226     /***
227     * Writes an integer (decimal format, not hexa)
228     * @param value the integer value.
229     */
230     public String writeInteger(String value) {
231         return getIndent() + "<OMI>" + value + "</OMI>\n";
232     }
233     
234     /***
235     * Writes a variable.
236     * @param value the variable value.
237     */
238     public String writeVariable(String value) {
239         return getIndent() + "<OMV name=\""+ value +"\"/>\n";
240     }
241     
242     /***
243     * Writes the symbol with the specified identifier.
244     * @param objectId the identifier of the symbol to write.
245     */
246     public String writeSymbol(String objectId) {
247         return getIndent() + "<OMS cd=\""+getCdName(objectId)+"\" name=\""+getSymbolName(objectId)+"\"/>\n";
248     }
249     
250     /***
251     * Writes the specified symbol.
252     * @param cdName the name of the symbol CD.
253     * @param symbName thename of the symbol.
254     */
255     public String writeSymbol(String cdName, String symbName) {
256         return getIndent() + "<OMS cd=\""+cdName+"\" name=\""+symbName+"\"/>\n";
257     }
258     
259     /***
260     * Test.
261     */
262     public static void main(String args[]) {
263         Frame f = new Frame();
264         f.setLayout(new BorderLayout());
265         
266         TextArea ta = new TextArea();
267         f.add(ta, "Center");
268         
269         TextField tf = new TextField();
270         tf.addActionListener(new TfAdapter(ta));
271         f.add(tf, "South");
272         
273         f.pack();
274         f.setSize(f.getPreferredSize());
275         f.setVisible(true);
276     }
277 }
278     
279     class TfAdapter implements ActionListener {
280         Formula f = new Formula();
281         LinearParser lp;
282         TextArea ta;
283         OpenMathFormater omf = new OpenMathFormater();
284         
285         public TfAdapter(TextArea ta) {
286             this.ta = ta;
287             lp = new LinearParser();
288             lp.addLinearParserListener(f);
289         }
290         
291         public void actionPerformed(ActionEvent e) {
292             TextField src = (TextField) e.getSource();
293             lp.parse(src.getText());
294             
295             System.out.println(f.getLinear());
296             
297             ta.setText(omf.getOpenMath(f));
298         }
299     }