/** * Builds a DOM sub-tree based on the currently parsed tokens, appending the results as * children of the given target root Element. This uses the default {@link DOMOutputOptions} * associated with this engine. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean buildDOMSubtree(final Element targetRoot) { return buildDOMSubtree(targetRoot, defaultDOMOutputOptions); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The default {@link DOMOutputOptions} specified in the {@link SnuggleEngine} will be * used. * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree() { return buildDOMSubtree(defaultDOMOutputOptions); }
/** * Builds a DOM sub-tree based on the currently parsed tokens, appending the results as * children of the given target root Element. This uses the default {@link DOMOutputOptions} * associated with this engine. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean buildDOMSubtree(final Element targetRoot) { return buildDOMSubtree(targetRoot, engine.getDefaultDOMOutputOptions()); }
/** * Builds a DOM sub-tree based on the currently parsed tokens, appending the results as * children of the given target root Element. This uses the default {@link DOMOutputOptions} * associated with this engine. * * @return true if completed successfully, false if the process was terminated by an error in the * input LaTeX and if the session was configured to fail on the first error. */ public boolean buildDOMSubtree(final Element targetRoot) { return buildDOMSubtree(targetRoot, engine.getDefaultDOMOutputOptions()); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The default {@link DOMOutputOptions} specified in the {@link SnuggleEngine} will be * used. * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree() { return buildDOMSubtree(engine.getDefaultDOMOutputOptions()); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The default {@link DOMOutputOptions} specified in the {@link SnuggleEngine} will be * used. * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree() { return buildDOMSubtree(engine.getDefaultDOMOutputOptions()); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The given {@link DOMOutputOptions} Object is used to configure the process, which must * not be null. * * @param options {@link DOMOutputOptions} to use, which must not be null * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree(final DOMOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "DOMOutputOptions"); Document document = XMLUtilities.createNSAwareDocumentBuilder().newDocument(); Element temporaryRoot = document.createElementNS(SnuggleConstants.SNUGGLETEX_NAMESPACE, "root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return temporaryRoot.getChildNodes(); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The given {@link DOMOutputOptions} Object is used to configure the process, which must * not be null. * * @param options {@link DOMOutputOptions} to use, which must not be null * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree(final DOMOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "DOMOutputOptions"); Document document = XMLUtilities.createNSAwareDocumentBuilder().newDocument(); Element temporaryRoot = document.createElementNS(SnuggleConstants.SNUGGLETEX_NAMESPACE, "root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return temporaryRoot.getChildNodes(); }
/** * Convenience method to build a DOM {@link NodeList} representing the converted Tokens. * These Nodes will belong to a "fake root" element in the * {@link SnuggleConstants#SNUGGLETEX_NAMESPACE} namespace called "root". * <p> * The given {@link DOMOutputOptions} Object is used to configure the process, which must * not be null. * * @param options {@link DOMOutputOptions} to use, which must not be null * * @return resulting {@link NodeList} if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public NodeList buildDOMSubtree(final DOMOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "DOMOutputOptions"); Document document = XMLUtilities.createNSAwareDocumentBuilder().newDocument(); Element temporaryRoot = document.createElementNS(SnuggleConstants.SNUGGLETEX_NAMESPACE, "root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return temporaryRoot.getChildNodes(); }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
private Node latexToNode(String latex){ SnuggleEngine engine = new SnuggleEngine(); try { SnuggleSession createSession = engine.createSession(); createSession.parseInput(new SnuggleInput(latex)); NodeList dst = createSession.buildDOMSubtree(); return dst.item(0); } catch (IOException e) { throw new RuntimeException(e); } }
void convert(Element latexElement) throws IOException { String rawInputLaTeX = latexElement.getTextContent(); String inputLaTeX = rawInputLaTeX.replaceAll("(\r\n|\r|\n)", "\n"); SnuggleEngine engine = createSnuggleEngine(); SnuggleSession session = engine.createSession(); SnuggleInput input = new SnuggleInput(inputLaTeX, "LaTeX Element"); try { session.parseInput(input); } catch (Exception e) { throw new IOException("Error while parsing: " + rawInputLaTeX + ": " + e.getMessage(), e); } while (latexElement.getChildNodes().getLength() != 0) latexElement.removeChild(latexElement.getFirstChild()); DOMOutputOptions options = new DOMOutputOptions(); options.setErrorOutputOptions(DOMOutputOptions.ErrorOutputOptions.XHTML); try { session.buildDOMSubtree(latexElement, options); } catch (Exception e) { throw new IOException("Error while building DOM for: " + rawInputLaTeX + ": " + e.getMessage(), e); } }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The given {@link XMLStringOutputOptions} Object is used to configure the process. * * @param options {@link XMLStringOutputOptions} to use, which must not be null. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString(final XMLStringOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "XMLStringOutputOptions"); DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, options); }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The given {@link XMLStringOutputOptions} Object is used to configure the process. * * @param options {@link XMLStringOutputOptions} to use, which must not be null. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString(final XMLStringOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "XMLStringOutputOptions"); DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, options); }
/** * Creates a well-formed external general parsed entity out of the currently parsed tokens. * <p> * The given {@link XMLStringOutputOptions} Object is used to configure the process. * * @param options {@link XMLStringOutputOptions} to use, which must not be null. * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. */ public String buildXMLString(final XMLStringOutputOptions options) { ConstraintUtilities.ensureNotNull(options, "XMLStringOutputOptions"); DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, options); }
/** * Convenience method to create a well-formed external general parsed entity out of the * currently parsed tokens. * <p> * The given {@link DOMOutputOptions} Object is used to configure the process. * * @param indent whether to indent the resulting XML or not * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. * * @deprecated Use {@link #buildXMLString(XMLStringOutputOptions)} instead. */ @Deprecated public String buildXMLString(final DOMOutputOptions options, final boolean indent) { DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } SerializationSpecifier serializationOptions = new SerializationOptions(); serializationOptions.setEncoding(XMLStringOutputOptions.DEFAULT_ENCODING); serializationOptions.setIndenting(indent); return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, serializationOptions); }
/** * Convenience method to create a well-formed external general parsed entity out of the * currently parsed tokens. * <p> * The given {@link DOMOutputOptions} Object is used to configure the process. * * @param indent whether to indent the resulting XML or not * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. * * @deprecated Use {@link #buildXMLString(XMLStringOutputOptions)} instead. */ @Deprecated public String buildXMLString(final DOMOutputOptions options, final boolean indent) { DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } SerializationSpecifier serializationOptions = new SerializationOptions(); serializationOptions.setEncoding(XMLStringOutputOptions.DEFAULT_ENCODING); serializationOptions.setIndenting(indent); return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, serializationOptions); }
public Element doMathInput(String mathModeInput, DOMOutputOptions domOutputOptions) { ConstraintUtilities.ensureNotNull(mathModeInput, "Math Mode Input"); String latexInput = "\\[" + mathModeInput + "\\]"; session.reset(); try { session.parseInput(new SnuggleInput(latexInput, "Math Mode Input")); } catch (IOException e) { throw new SnuggleLogicException("Unexpected IOException"); } NodeList nodeList = session.buildDOMSubtree(domOutputOptions); if (!session.getErrors().isEmpty()) { return null; } /* Extract and check <math> element */ Element result = null; if (nodeList.getLength()==1) { result = (Element) nodeList.item(0); if (!MathMLUtilities.isMathMLElement(result, "math")) { throw new IllegalArgumentException("Input did not yield exactly 1 <math> element"); } } else { throw new IllegalArgumentException("Input did not yield exactly 1 result element"); } return result; }
/** * Convenience method to create a well-formed external general parsed entity out of the * currently parsed tokens. * <p> * The given {@link DOMOutputOptions} Object is used to configure the process. * * @param indent whether to indent the resulting XML or not * * @return resulting XML if the process completed successfully, null if the process was * terminated by an error in the input LaTeX and if the session was configured to fail on * the first error. * * @deprecated Use {@link #buildXMLString(XMLStringOutputOptions)} instead. */ @Deprecated public String buildXMLString(final DOMOutputOptions options, final boolean indent) { DocumentBuilder documentBuilder = XMLUtilities.createNSAwareDocumentBuilder(); Document document = documentBuilder.newDocument(); Element temporaryRoot = document.createElement("root"); document.appendChild(temporaryRoot); if (!buildDOMSubtree(temporaryRoot, options)) { return null; } SerializationSpecifier serializationOptions = new SerializationOptions(); serializationOptions.setEncoding(XMLStringOutputOptions.DEFAULT_ENCODING); serializationOptions.setIndenting(indent); return XMLUtilities.serializeNodeChildren(getStylesheetManager(), temporaryRoot, serializationOptions); }
/** * Convert a tex math formula into a Symja expression. The SnuggleTeX engine first converts the TeX expression in a * MathML expression. This MathML expression is then converted to Symja. * * @param texStr * the tex math formula * @return */ public IExpr toExpression(String texStr) { SnuggleEngine engine = new SnuggleEngine(); SnuggleSession session = engine.createSession(); session.getConfiguration().setFailingFast(true); SnuggleInput input = new SnuggleInput("$$ " + texStr + " $$"); try { if (session.parseInput(input)) { NodeList nodes = session.buildDOMSubtree(); int[] position = new int[] { 0 }; return convert(nodes, position, null, 0); } List<InputError> errors = session.getErrors(); for (int i = 0; i < errors.size(); i++) { fEngine.printMessage(errors.get(i).toString()); } } catch (Exception e) { if (Config.SHOW_STACKTRACE) { e.printStackTrace(); } } return F.$Aborted; }