IO Package

The IO package contains classes for reading and writing math objects to various I/O streams.

Class descriptions

MathMLDocumentJSciImpl

MathMLDocumentJSciImpl will translate JSci objects into MathML code. The MathML code can then be used to construct a MathML document, which can be printed to any output stream.

Sample code to create a MathML document:

import JSci.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
.
.
MathMLDocumentJSciImpl doc = new MathMLDocumentJSciImpl();
Element root = doc.getDocumentElement();
root.appendChild(doc.createNumber(2.3));
Element aNode = doc.createElement("a tag name");
aNode.appendChild(doc.createVector(aVector));
aNode.appendChild(doc.createNumber(4));
root.appendChild(aNode);

Use doc.print(writer) to write the document to an output stream.

It is also possible to create DocumentFragments by using doc.createDocumentFragment(). A DocumentFragment represents a fragment of MathML code to which elements can be appended. In turn, it can be appended to a MathML document. Some of the MathMLDocumentJSciImpl.createXXX() methods require a DocumentFragment as an argument.

DocumentFragment sample code:

DocumentFragment frag = doc.createDocumentFragment();
frag.appendChild(node1);
frag.appendChild(node2);
doc.appendChild(frag);

MathMLParser

This class uses the Apache Xerces XML parser. MathMLParser will translate a MathML document into JSci objects or JSci Java code.

Sample code to parse a MathML document:

import JSci.io.*;
.
.
MathMLParser parser = new MathMLParser();
try {
        parser.parse("doc.xml");
} catch(Exception e) {
        e.printStackTrace();
}
Object parseList[] = parser.translateToJSciObjects();

The parseList is an array containing parsed MathML lines. A MathML line may consist of a mathematical expression or just a single math object. The instanceof operator should be used to determine which. If parseList[i] is an instance of MathNumber, MathVector, etc, then the line just contains that object. However, if it is an instance of MathMLExpression then this object will specify a math operation and arguments to which it is to be applied. The arguments may either be math objects or other MathMLExpressions. The evaluate() method can be used to evaluate a MathMLExpression to a single math object.


Return to the Developer's Guide contents.