package org.matheclipse.core.eval;

import java.io.Writer;

import org.matheclipse.core.convert.AST2Expr;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.form.mathml.MathMLFormFactory;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.parser.client.Parser;
import org.matheclipse.parser.client.ast.ASTNode;

/**
 * Convert an expression into presentation MathML output
 * 
 * See Chracters ordered by Unicode
 * 
 */
public class MathMLUtilities {
	protected EvalEngine fEvalEngine;

	protected MathMLFormFactory fMathMLFactory;

	/**
	 * MS Internet Explorer client ?
	 */
	boolean fMSIE;

	/**
	 * Print MathML header in output
	 */
	boolean fMathMLHeader;
	// Parser fParser;

	static {
		// initialize the global available symbols
		F.initSymbols(null, null, true);
	}

	/**
	 * Constructor for an object which converts an expression into presentation MathML output
	 * 
	 * @param evalEngine
	 * @param mathMTagPrefix
	 *            if set to true use "m:" as tag prefix for the MathML output.
	 * @param mathMLHeader
	 *            print MathML header in output
	 */
	public MathMLUtilities(final EvalEngine evalEngine, final boolean mathMTagPrefix, final boolean mathMLHeader) {
		fEvalEngine = evalEngine;
		// set the thread local instance
		startRequest();
		if (mathMTagPrefix) {
			fMathMLFactory = new MathMLFormFactory("m:");
		} else {
			fMathMLFactory = new MathMLFormFactory();
		}
		// fParser = new Parser(relaxedSyntax);
		fMSIE = mathMTagPrefix;
		fMathMLHeader = mathMLHeader;
	}

	/**
	 * Converts the inputExpression string into a MathML expression and writes the result to the given Writer
	 * 
	 * @param inputExpression
	 * @param out
	 */
	synchronized public void toMathML(final String inputExpression, final Writer out) {
		IExpr parsedExpression = null;
		ASTNode node;
		if (inputExpression != null) {
			try {
				node = fEvalEngine.parseNode(inputExpression);
				parsedExpression = AST2Expr.CONST.convert(node);
			} catch (final Throwable e) {
				return;
				// parsedExpression == null ==> fError occured
			}
		}
		toMathML(parsedExpression, out);
	}

	/**
	 * Converts the objectExpression into a MathML expression and writes the result to the given Writer
	 * 
	 * @param objectExpression
	 * @param out
	 */
	synchronized public void toMathML(final IExpr objectExpression, final Writer out) {
		final StringBuffer buf = new StringBuffer();

		if (objectExpression != null) {
			fMathMLFactory.convert(buf, objectExpression, 0);
			try {
				if (fMSIE) {
					out.write("");
					out.write(buf.toString());
					out.write("");
				} else {
					// out.write("");

					// out.write("");
					if (fMathMLHeader) {
						out.write("\n"
								+ "\n"
								+ "\n");
					} else {
						out.write("");
					}

					out.write(buf.toString());
					out.write("");
				}
			} catch (final Throwable e) {
				// parsedExpression == null ==> fError occured
			}
		}
	}

	synchronized public void toJava(final String inputExpression, final Writer out, boolean strictJava) {
		IExpr parsedExpression = null;
		ASTNode node;
		if (inputExpression != null) {
			try {
				node = fEvalEngine.parseNode(inputExpression);
				parsedExpression = AST2Expr.CONST.convert(node);
				out.write(parsedExpression.internalFormString(strictJava, 0));
			} catch (final Throwable e) {
				return;
				// parsedExpression == null ==> fError occured
			}
		}
	}

	/**
	 * Assign the associated EvalEngine to the current thread. Every subsequent action evaluation in this thread affects the
	 * EvalEngine in this class.
	 */
	public void startRequest() {
		EvalEngine.set(fEvalEngine);
	}

	/**
	 * Stop the current evaluation thread.
	 */
	public void stopRequest() {
		fEvalEngine.stopRequest();
	}
}

 

Ads help maintain this website.