The MathML Extension package,
com.singularsys.extensions.mathml, provides
facilities to convert to and from the Content-MathML format.
By using suitable transformations Presentation-MathML can also be produced.
The MathMLContentBuilder converts Jep expressions to org.w3c.dom.Document objects containing Content-MathML.
Jep jep = new Jep(); MathMLContentBuilder mcb = new MathMLContentBuilder(jep); MathMLTransformerFactory mft = new MathMLTransformerFactory(); // Convert a jep expression to xml Node eqn = jep.parse("sin(1.23)"); Document doc = mcb.buildDocument(eqn);
The document can then be output to Files, String and other sources using a javax.xml.transform.Transformer
.
// 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();
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 MathMLConstants
contains MathML related constants. For example a Transformer could be configured to
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");
Presentation markup can be obtained by using the mmlctop2_0.xsl stylesheet.
Jep jep = new Jep(); MathMLContentBuilder mcb = new MathMLContentBuilder(jep); MathMLTransformerFactory mft = new MathMLTransformerFactory(); // Convert a jep expression to xml Node eqn = jep.parse("sin(1.23)"); Document doc = mcb.buildDocument(eqn); // Convert to presentation using a stylesheet File sheet = new File("mmlctop2_0.xsl"); // Insert path to local stylesheet here StreamSource style = new StreamSource(sheet); Transformer t = tf.newTransformer(style); String res = this.docToString(doc, t); System.out.println(res);
Note: The x=5
assignment does not have obvious representation
in MathML3. The standard setup uses the <equivalent/>
relation in Content markup.
This translates to <mo>≡</mo>
(≡)
in Presentation markup with
the standard style sheet. It could be changed with
mcb.getOperatorBuilder().put(jep.getOperatorTable().getAssign(), "eq");
Or by using a custom and MathMLFragmentBuilder in the second argument.
The MathMLContentParser parses XML input containing Content-MathML and produces a jep expression.
Jep jep = new Jep(); MathMLContentParser mcp = MathMLContentParser(jep); // Convert xml to a jep node String xml="<math><apply><sin/><cn>1.23</cn></apply></math>"; Node node = mcp.parseSingle(xml);
The parser can strip the mathml from the an xhtml document using the
MathMLContentParser.parseAllMath
.