MathML parser/builder

The {@link com.singularsys.extensions.mathml.MathMLContentParser MathMLContentParser} parses XML input containing Content-MathML and produces a jep expression. The {@link com.singularsys.extensions.mathml.MathMLContentBuilder MathMLContentBuilder} converts Jep expressions to org.w3c.dom.Document objects containing Content-MathML which can be output to Files, String and other sources using a javax.xml.transform.Transformer.

  Jep jep = new Jep();
  MathMLContentParser mcp = MathMLContentParser(jep);
  MathMLContentBuilder mcb = new MathMLContentBuilder(jep);
  MathMLTransformerFactory mft = new MathMLTransformerFactory();

  // Convert xml to a jep node
  String xml="<math><apply><sin/><cn>1.23</cn></apply></math>";
  Node node = mcp.parseSingle(xml);

  // Convert a jep expression to xml
  Node eqn = jep.parse("sin(1.23)");
  Document doc = mcb.buildDocument(eqn);

  // Produce a string representation
  TransformerFactory ft = TransformerFactory.newInstance();
  Transformer t = tf.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new StringWriter());
  t.transform(source, result);
  String res = result.toString();

Other methods are available to parse/produce xml containing more than one equation; xml with namespace and dtd declerations.

The standard configuration for the builder parser which can be modified and extended.

Tranforming documents

The javax.xml.transform package contains classes to manage transformation of documents. TransformerFactory creates Transformer objects which perform the actual transformation. The Source and Result specifiy the source an destination of transformation, in particular the javax.xml.transform.dom.DOMSource is a source for DOM documents produced by the MathMLContentBuilder, the javax.xml.transform.stream.StreamResult is used to produce results to a File, OutputStream, or Writer. A simple usage is

  TransformerFactory ft = TransformerFactory.newInstance();
  Transformer t = tf.newTransformer();
  Document doc = contentBuilder.buildDocument(node);
  DOMSource source = new DOMSource(doc);
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  t.transform(source, result);
  String res = sr.toString();

To output to a File

  File f = new File(...);
  StreamResult result = new StreamResult(f);
  t.transform(source, result);

and similar for an OutputStream or Writer.

The output of the Transformer can be configure by using setOutputProperty(String key,String value) method. The javax.xml.transform.OutputKeys class contains valid keys and com.singularsys.extensions.mathml.MathMLConstants contains MathML related constants. A Transformer could be configured to use output the xml deceleration, the standard MathML DTD, and indent the output using

  t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"no");
  t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,MathMLConstants.MATHML_PUBLIC_ID);
  t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,MathMLConstants.MATHML_SYSTEM_ID);
  t.setOutputProperty(OutputKeys.INDENT,"yes"); 

Transformer can also be used to apply xslt style sheets, in particular an number of xslt style sheets are available which can convert Content MathML to presentation MathML. For example the mmlctop2_0.xsl from http://www.orcca.on.ca/MathML/software/mmlctop2_0.zip. To use this the document must be built using the mml namespace prefix and a Transformer built with the style sheet.

  Document doc = mcb.buildDocument(node,"mml");
  File sheet = new File(LOCAL_STYLESHEETS_DIR + "mmlctop2_0.xsl");
  StreamSource style = new StreamSource(sheet);
  Transformer t = tf.newTransformer(style);
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new StringWriter());
  t.transform(source, result);

Standard builder configuration

Math elements
The builder can produce:
Numeric constants
Numeric constants like "3.2" are typically converted to <cn> elements. Special cases:
Variables
Typically a jep variable "a" is converted to <ci>a</ci>. Some constant variables have special behaviour
Operators
Most operators like "x+1" are converted to <apply><plus/><ci>x<ci/><cn>1<cn/><apply/> in particular There are some special cases
Functions
Most functions "sin(x)" are converted to <apply><sin/><ci>x</ci></apply> In particular the folowing are converted to <apply> with the following first element The following have special behaviour: These jep functions are not currently converted to mathml: complex, sum, round, atan2, if, rand, avg, polar, binom, str

Standard parser configuration

Main math tag
Supported Token elements
Un-supported Token elements
Supported constant elements
Un-supported constant elements
Supported basic content elements
Un-supported basic content elements
Supported operator
Supported functions
Vectors, matricies lists and sets
All are converted to jep lists or lists of lists.
Un-supported operators/functions
Other un-supported elements
Other un-supported elements
Supported entities
@since Jep 3.5 / Extensions 2.0 @see MathML documentation