JSci.io.MathMLDocumentJSciImpl Maven / Gradle / Ivy
package JSci.io; import java.io.*; import java.util.Iterator; import JSci.maths.*; import JSci.maths.matrices.AbstractComplexMatrix; import JSci.maths.matrices.AbstractDoubleMatrix; import JSci.maths.matrices.AbstractIntegerMatrix; import JSci.maths.vectors.AbstractComplexVector; import JSci.maths.vectors.AbstractDoubleVector; import JSci.maths.vectors.AbstractIntegerVector; import JSci.mathml.*; import org.w3c.dom.*; import org.w3c.dom.mathml.*; /** * The MathMLDocumentJSciImpl class encapsulates an entire MathML document. * @version 0.6 * @author Mark Hale */ public class MathMLDocumentJSciImpl extends MathMLDocumentImpl { /** * Constructs a MathML document. */ public MathMLDocumentJSciImpl() { super(); } /** * Creates a MathML number element (
). */ public Element createVector(AbstractIntegerVector v) { final Element vector=createElement("vector"); for(int i=0;i<cn>
). */ public Element createNumber(double x) { final Element num=createElement("cn"); num.appendChild(createTextNode(String.valueOf(x))); return num; } /** * Creates a MathML number element (<cn>
). */ public Element createNumber(int i) { final Element num=createElement("cn"); num.setAttribute("type","integer"); num.appendChild(createTextNode(String.valueOf(i))); return num; } /** * Creates a MathML number element (<cn>
). */ public Element createNumber(Complex z) { final Element num=createElement("cn"); num.setAttribute("type","complex-cartesian"); num.appendChild(createTextNode(String.valueOf(z.real()))); num.appendChild(createElement("sep")); num.appendChild(createTextNode(String.valueOf(z.imag()))); return num; } /** * Creates a MathML variable element (<ci>
). */ public Element createVariable(Object obj) { final Element var=createElement("ci"); var.appendChild(createTextNode(obj.toString())); return var; } /** * Creates a MathML variable element (<ci>
). */ public Element createVariable(Object obj,String type) { final Element var=createElement("ci"); var.setAttribute("type",type); var.appendChild(createTextNode(obj.toString())); return var; } /** * Creates a MathML vector element (<vector>
). */ public Element createVector(AbstractDoubleVector v) { final Element vector=createElement("vector"); for(int i=0;i<vector> <vector>). */ public Element createVector(AbstractComplexVector v) { final Element vector=createElement("vector"); for(int i=0;i <matrix>). */ public Element createMatrix(AbstractDoubleMatrix m) { final Element matrix=createElement("matrix"); Element row; for(int j,i=0;i <matrix>). */ public Element createMatrix(AbstractIntegerMatrix m) { final Element matrix=createElement("matrix"); Element row; for(int j,i=0;i <matrix>). */ public Element createMatrix(AbstractComplexMatrix m) { final Element matrix=createElement("matrix"); Element row; for(int j,i=0;i <set>). */ public Element createSet(FiniteSet s) { final Element set = createElement("set"); Iterator iter = s.getElements().iterator(); while(iter.hasNext()) set.appendChild(createVariable(iter.next())); return set; } /** * Creates a MathML operator/function element ( <apply>
). * @param op a MathML tag name * (plus
,minus
,times
,divide
, etc). */ public Element createApply(String op,DocumentFragment args) { final Element apply=createElement("apply"); apply.appendChild(createElement(op)); apply.appendChild(args); return apply; } /** * Creates a MathML operator/function element (<apply>
). * @param expr a MathMLExpression object. */ public Element createApply(MathMLExpression expr) { final Element apply=createElement("apply"); apply.appendChild(createElement(expr.getOperation())); for(int i=0;i\n"); out.write("\n"); printNode(out,getDocumentElement(),true); out.flush(); } private void printNode(Writer out,Node n,boolean printNS) throws IOException { if(n.hasChildNodes()) { out.write("<"+n.getNodeName()); if(printNS) out.write(" xmlns=\""+n.getNamespaceURI()+"\""); final NamedNodeMap attr=n.getAttributes(); for(int j=0;j "); if(n.getFirstChild().getNodeType()!=Node.TEXT_NODE) out.write("\n"); final NodeList nl=n.getChildNodes(); for(int i=0;i \n"); } else { if(n.getNodeType()==Node.TEXT_NODE) { out.write(n.getNodeValue()); } else { out.write("<"+n.getNodeName()+"/>"); if(n.getNextSibling().getNodeType()!=Node.TEXT_NODE) out.write("\n"); } } } }