KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > io > MathMLDocumentJSciImpl


1 package JSci.io;
2
3 import java.io.*;
4 import java.util.Iterator JavaDoc;
5 import JSci.maths.*;
6 import JSci.maths.matrices.AbstractComplexMatrix;
7 import JSci.maths.matrices.AbstractDoubleMatrix;
8 import JSci.maths.matrices.AbstractIntegerMatrix;
9 import JSci.maths.vectors.AbstractComplexVector;
10 import JSci.maths.vectors.AbstractDoubleVector;
11 import JSci.maths.vectors.AbstractIntegerVector;
12 import JSci.mathml.*;
13 import org.w3c.dom.*;
14 import org.w3c.dom.mathml.*;
15
16 /**
17  * The MathMLDocumentJSciImpl class encapsulates an entire MathML document.
18  * @version 0.6
19  * @author Mark Hale
20  */

21 public class MathMLDocumentJSciImpl extends MathMLDocumentImpl {
22         /**
23          * Constructs a MathML document.
24          */

25         public MathMLDocumentJSciImpl() {
26                 super();
27         }
28
29         /**
30          * Creates a MathML number element (<code>&lt;cn&gt;</code>).
31          */

32         public Element createNumber(double x) {
33                 final Element num=createElement("cn");
34                 num.appendChild(createTextNode(String.valueOf(x)));
35                 return num;
36         }
37         /**
38          * Creates a MathML number element (<code>&lt;cn&gt;</code>).
39          */

40         public Element createNumber(int i) {
41                 final Element num=createElement("cn");
42                 num.setAttribute("type","integer");
43                 num.appendChild(createTextNode(String.valueOf(i)));
44                 return num;
45         }
46         /**
47          * Creates a MathML number element (<code>&lt;cn&gt;</code>).
48          */

49         public Element createNumber(Complex z) {
50                 final Element num=createElement("cn");
51                 num.setAttribute("type","complex-cartesian");
52                 num.appendChild(createTextNode(String.valueOf(z.real())));
53                 num.appendChild(createElement("sep"));
54                 num.appendChild(createTextNode(String.valueOf(z.imag())));
55                 return num;
56         }
57
58         /**
59          * Creates a MathML variable element (<code>&lt;ci&gt;</code>).
60          */

61         public Element createVariable(Object JavaDoc obj) {
62                 final Element var=createElement("ci");
63                 var.appendChild(createTextNode(obj.toString()));
64                 return var;
65         }
66         /**
67          * Creates a MathML variable element (<code>&lt;ci&gt;</code>).
68          */

69         public Element createVariable(Object JavaDoc obj,String JavaDoc type) {
70                 final Element var=createElement("ci");
71                 var.setAttribute("type",type);
72                 var.appendChild(createTextNode(obj.toString()));
73                 return var;
74         }
75
76         /**
77          * Creates a MathML vector element (<code>&lt;vector&gt;</code>).
78          */

79         public Element createVector(AbstractDoubleVector v) {
80                 final Element vector=createElement("vector");
81                 for(int i=0;i<v.dimension();i++)
82                         vector.appendChild(createNumber(v.getComponent(i)));
83                 return vector;
84         }
85         /**
86          * Creates a MathML vector element (<code>&lt;vector&gt;</code>).
87          */

88         public Element createVector(AbstractIntegerVector v) {
89                 final Element vector=createElement("vector");
90                 for(int i=0;i<v.dimension();i++)
91                         vector.appendChild(createNumber(v.getComponent(i)));
92                 return vector;
93         }
94         /**
95          * Creates a MathML vector element (<code>&lt;vector&gt;</code>).
96          */

97         public Element createVector(AbstractComplexVector v) {
98                 final Element vector=createElement("vector");
99                 for(int i=0;i<v.dimension();i++)
100                         vector.appendChild(createNumber(v.getComponent(i)));
101                 return vector;
102         }
103
104         /**
105          * Creates a MathML matrix element (<code>&lt;matrix&gt;</code>).
106          */

107         public Element createMatrix(AbstractDoubleMatrix m) {
108                 final Element matrix=createElement("matrix");
109                 Element row;
110                 for(int j,i=0;i<m.rows();i++) {
111                         row=createElement("matrixrow");
112                         for(j=0;j<m.columns();j++)
113                                 row.appendChild(createNumber(m.getElement(i,j)));
114                         matrix.appendChild(row);
115                 }
116                 return matrix;
117         }
118         /**
119          * Creates a MathML matrix element (<code>&lt;matrix&gt;</code>).
120          */

121         public Element createMatrix(AbstractIntegerMatrix m) {
122                 final Element matrix=createElement("matrix");
123                 Element row;
124                 for(int j,i=0;i<m.rows();i++) {
125                         row=createElement("matrixrow");
126                         for(j=0;j<m.columns();j++)
127                                 row.appendChild(createNumber(m.getElement(i,j)));
128                         matrix.appendChild(row);
129                 }
130                 return matrix;
131         }
132         /**
133          * Creates a MathML matrix element (<code>&lt;matrix&gt;</code>).
134          */

135         public Element createMatrix(AbstractComplexMatrix m) {
136                 final Element matrix=createElement("matrix");
137                 Element row;
138                 for(int j,i=0;i<m.rows();i++) {
139                         row=createElement("matrixrow");
140                         for(j=0;j<m.columns();j++)
141                                 row.appendChild(createNumber(m.getElement(i,j)));
142                         matrix.appendChild(row);
143                 }
144                 return matrix;
145         }
146
147         /**
148          * Creates a MathML set element (<code>&lt;set&gt;</code>).
149          */

150         public Element createSet(FiniteSet s) {
151                 final Element set = createElement("set");
152                 Iterator JavaDoc iter = s.getElements().iterator();
153                 while(iter.hasNext())
154                         set.appendChild(createVariable(iter.next()));
155                 return set;
156         }
157
158         /**
159          * Creates a MathML operator/function element (<code>&lt;apply&gt;</code>).
160          * @param op a MathML tag name
161          * (<code>plus</code>, <code>minus</code>, <code>times</code>, <code>divide</code>, etc).
162          */

163         public Element createApply(String JavaDoc op,DocumentFragment args) {
164                 final Element apply=createElement("apply");
165                 apply.appendChild(createElement(op));
166                 apply.appendChild(args);
167                 return apply;
168         }
169         /**
170          * Creates a MathML operator/function element (<code>&lt;apply&gt;</code>).
171          * @param expr a MathMLExpression object.
172          */

173         public Element createApply(MathMLExpression expr) {
174                 final Element apply=createElement("apply");
175                 apply.appendChild(createElement(expr.getOperation()));
176                 for(int i=0;i<expr.length();i++) {
177                         Object JavaDoc arg=expr.getArgument(i);
178                         if(arg instanceof MathMLExpression)
179                                 apply.appendChild(createApply((MathMLExpression)arg));
180                         else if(arg instanceof MathDouble)
181                                apply.appendChild(createNumber(((MathDouble)arg).value()));
182                         else if(arg instanceof Complex)
183                                apply.appendChild(createNumber((Complex)arg));
184                         else if(arg instanceof AbstractDoubleVector)
185                                apply.appendChild(createVector((AbstractDoubleVector)arg));
186                         else if(arg instanceof AbstractDoubleMatrix)
187                                apply.appendChild(createMatrix((AbstractDoubleMatrix)arg));
188                         else
189                                 apply.appendChild(createVariable(arg));
190                 }
191                 return apply;
192         }
193
194         /**
195          * Prints this MathML document to a stream.
196          */

197         public void print(Writer out) throws IOException {
198                 out.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
199                 out.write("<!DOCTYPE "+getDoctype().getName()+" PUBLIC \""+getDoctype().getPublicId()+"\" \""+getDoctype().getSystemId()+"\">\n");
200                 printNode(out,getDocumentElement(),true);
201                 out.flush();
202         }
203         private void printNode(Writer out,Node n,boolean printNS) throws IOException {
204                 if(n.hasChildNodes()) {
205                         out.write("<"+n.getNodeName());
206                         if(printNS)
207                                 out.write(" xmlns=\""+n.getNamespaceURI()+"\"");
208                         final NamedNodeMap attr=n.getAttributes();
209                         for(int j=0;j<attr.getLength();j++)
210                                 out.write(" "+attr.item(j).getNodeName()+"=\""+attr.item(j).getNodeValue()+"\"");
211                         out.write(">");
212                         if(n.getFirstChild().getNodeType()!=Node.TEXT_NODE)
213                                 out.write("\n");
214                         final NodeList nl=n.getChildNodes();
215                         for(int i=0;i<nl.getLength();i++)
216                                 printNode(out,nl.item(i),false);
217                         out.write("</"+n.getNodeName()+">\n");
218                 } else {
219                         if(n.getNodeType()==Node.TEXT_NODE) {
220                                 out.write(n.getNodeValue());
221                         } else {
222                                 out.write("<"+n.getNodeName()+"/>");
223                                 if(n.getNextSibling().getNodeType()!=Node.TEXT_NODE)
224                                         out.write("\n");
225                         }
226                 }
227         }
228 }
229
230
Popular Tags