View Javadoc

1   package fr.ove.openmath.compounder;
2   
3   import java.util.*;
4   import java.io.*;
5   import fr.inria.openmath.omapi.*;
6   import fr.inria.openmath.omapi.implementation.*;
7   import fr.ove.openmath.OpenMathizable;
8   import fr.ove.openmath.compounder.OMCompounderParserHandler;
9   
10  /***
11  * An OpenMath objects compounder.<BR>
12  * The elements to compound are identified by name. These elements are compounded
13  * thanks to a "pattern" OpenMath object (the source) where the different elements
14  * are identified by means of variables whose values are the identifiers of the elements.
15  * <P>
16  * For example, lets assume the source is the following OpenMath object :
17  * <OMOBJ>
18  * <OMA>
19  * <OMS cd="arith1" name="plus"/>
20  * <OMV name="el1"/>
21  * <OMV name="el2"/>
22  * </OMA>
23  * </OMOBJ>
24  * with the element <OMOBJ><OMI>1</OMI></OMOBJ> identified by "el1" and
25  * the element <OMOBJ><OMI>2</OMI></OMOBJ> identified by "el2".<BR>
26  * The compounded OpenMath object is :
27  * <OMOBJ>
28  * <OMA>
29  * <OMS cd="arith1" name="plus"/>
30  * <OMI>1</OMI>
31  * <OMI>2</OMI>
32  * </OMA>
33  * </OMOBJ>
34  *
35  * @author © 2000 DIRAT Laurent
36  * @version 1.0 11/11/2000
37  */
38  public class OMCompounder {
39      /***
40      * The OpenMath object source.
41      */
42      private String source;
43  
44      /***
45      * The compounded OpenMath object.
46      */
47      private String compounded;
48  
49      /***
50      * The list of the elements to compose
51      */
52      private Hashtable elements;
53  
54      /***
55      * The parser handler.
56      */
57      private OMCompounderParserHandler parserHandler;
58  
59      /***
60      * The parser
61      */
62      private Parser parser;
63  
64      public OMCompounder() {
65          elements = new Hashtable();
66          parserHandler = new OMCompounderParserHandler(elements);
67          parser = new XMLParser(parserHandler);
68      }
69  
70      /***
71      * Adds an element to compound.
72      * @param id the identifier of the element.
73      * @param elem the element.
74      */
75      public void addElement(String id, OpenMathizable elem) {
76          elements.put(id, elem);
77      }
78  
79      /***
80      * Removes the element with the specified identifier.
81      * @param id the identifier of the element.
82      */
83      public void removeElement(String id) {
84          elements.remove(id);
85      }
86  
87      /***
88      * Gets the compounded OpenMath object.
89      */
90      public String getCompounded() {
91          return compounded;
92      }
93  
94      /***
95      * Builds the compounded OpenMath object.
96      */
97      public void compound() {
98          if ((source != null) && (source.startsWith("<OMOBJ>"))) {
99      	    ByteArrayInputStream in = new ByteArrayInputStream(source.getBytes());
100 
101             try {
102                 parser.initParse(in);
103                 // Setup done, now call the parser main loop.
104                 parser.parseObject(in);
105                 compounded = parserHandler.getCompounded();
106             }
107             catch (Exception e) {
108                 e.printStackTrace();
109             }
110         }
111     }
112 
113     /***
114     * Sets the source OpenMath object.
115     * @param source the source.
116     */
117     public void setSource(String source) {
118         this.source = source;
119     }
120 
121     /***
122     * Returns thesource OpenMath object.
123     */
124     public String getSource() {
125         return source;
126     }
127 }