/** * Extracts a SnuggleTeX annotation from a MathML <tt>math</tt> element, if found. This allows * you to extract the input LaTeX from a MathML element created by SnuggleTeX, provided that * {@link DOMOutputOptions#isAddingMathSourceAnnotations()} returned true when the element was * generated. * * @param mathmlElement * @return SnuggleTeX encoding, or null if not present. */ public static String extractSnuggleTeXAnnotation(Element mathmlElement) { return MathMLUtilities.extractAnnotationString(mathmlElement, SnuggleConstants.SNUGGLETEX_MATHML_SOURCE_ANNOTATION_ENCODING); } }
public static Element extractFirstSemanticsBranch(final Element mathElement) { ensureMathMLContainer(mathElement); /* Look for semantics child then annotation child with encoding set appropriately */ Node search = mathElement.getFirstChild(); if (search == null || !isMathMLElement(search, "semantics")) { /* Didn't get <semantics/> as first and only child so not parallel markup */ return null; } Element semantics = (Element) search; NodeList childNodes = semantics.getChildNodes(); return childNodes.getLength() == 0 ? null : ((Element) childNodes.item(0)); }
/** * Extracts the first XML-based annotation having the given encoding attribute from the given MathML * <tt>math</tt> element, if such an annotation is found. * <p> * This assumes the following structure: * <pre><![CDATA[ * <math> * <semantics> * ... * <annotation-xml encoding="...">XML annotation</annotation> * </semantics> * </math> * ]]></pre> * * @param mathElement * @return DOM NodeList corresponding to the child Nodes of the first matching * <tt>annotation-xml</tt> element, or null if no such annotation found. */ public static NodeList extractAnnotationXML(final Element mathElement, final String encodingAttribute) { Element annotationElement = extractAnnotationElement(mathElement, ANNOTATION_XML_LOCAL_NAME, encodingAttribute); return annotationElement!=null ? annotationElement.getChildNodes() : null; }
NodeList annotationContents = extractAnnotationXML(mathElement, encodingAttribute); return annotationContents!=null ? isolateDescendant(mathElement, annotationContents) : null;
Element firstSemantics = extractFirstSemanticsBranch(mathElement); return firstSemantics!=null ? isolateDescendant(mathElement, firstSemantics) : null;
/** * Convenience method that serializes the given DOM Document as a String (encoded in UTF-8), * indenting the results and omitting the XML declaration, which is a reasonable way of * serializing a MathML document. * * @param document DOM document to serialize, must not be null */ public static String serializeDocument(final Document document) { ConstraintUtilities.ensureNotNull(document, "document"); return serializeNode(document, "UTF-8", true, true); }
/** * Convenience method to test whether the given DOM {@link Node} node is a MathML element * having any localName. * * @param node Node to test * @throws IllegalArgumentException if node is null. */ public static boolean isMathMLElement(final Node node) { return isMathMLElement(node, null); }
protected LinkedHashMap<String, String> unwrapMathmlElement(final Element mathElement) { final LinkedHashMap<String, String> result = new LinkedHashMap<String, String>(); /* These options are used to serialize MathML that might get used, i.e. no entities */ final SerializationSpecifier serializationOptions = new SerializationOptions(); serializationOptions.setIndenting(true); /* These options are used to serialize MathML that will only be displayed as source */ final SerializationSpecifier sourceOptions = new SerializationOptions(); sourceOptions.setIndenting(true); sourceOptions.setUsingNamedEntities(true); /* Isolate various annotations from the result */ final Document pmathSemanticDocument = MathMLUtilities.isolateFirstSemanticsBranch(mathElement); final Document pmathBracketedDocument = MathMLUtilities.isolateAnnotationXML(mathElement, MathMLUpConverter.BRACKETED_PRESENTATION_MATHML_ANNOTATION_NAME); final Document cmathDocument = MathMLUtilities.isolateAnnotationXML(mathElement, MathMLUpConverter.CONTENT_MATHML_ANNOTATION_NAME); final String maximaAnnotation = MathMLUtilities.extractAnnotationString(mathElement, MathMLUpConverter.MAXIMA_ANNOTATION_NAME); final Document contentFailuresAnnotation = MathMLUtilities.isolateAnnotationXML(mathElement, MathMLUpConverter.CONTENT_FAILURES_ANNOTATION_NAME); final Document maximaFailuresAnnotation = MathMLUtilities.isolateAnnotationXML(mathElement, MathMLUpConverter.MAXIMA_FAILURES_ANNOTATION_NAME); /* Build up result */ maybeAddResult(result, "pmathParallel", mathElement, sourceOptions); maybeAddResult(result, "pmathSemantic", pmathSemanticDocument, sourceOptions); maybeAddResult(result, "pmathBracketed", pmathBracketedDocument, serializationOptions); maybeAddResult(result, "cmath", cmathDocument, sourceOptions); maybeAddResult(result, "maxima", maximaAnnotation); maybeAddResult(result, "cmathFailures", contentFailuresAnnotation, sourceOptions); maybeAddResult(result, "maximaFailures", maximaFailuresAnnotation, sourceOptions); return result; }
/** * Version of {@link #isolateAnnotationXML(Element, String)} that works on an * {@link UnwrappedParallelMathMLDOM}. * * @return new MathML Document with the given structure or null if the given wrapper does * not have the required annotation. * @throws IllegalArgumentException if passed null. */ public static Document isolateAnnotationXML(final UnwrappedParallelMathMLDOM unwrappedDOM, final String encodingAttribute) { ConstraintUtilities.ensureNotNull(unwrappedDOM, "UnwrappedParallelMathMLDOM"); NodeList annotationContents = unwrappedDOM.getXmlAnnotations().get(encodingAttribute); return annotationContents != null ? isolateDescendant(unwrappedDOM.getMathElement(), annotationContents) : null; }
/** * Checks that the given DOM {@link Document} contains a single MathML <tt>math</tt> * Element. If so, the Element is returned. Otherwise, an {@link IllegalArgumentException} is * thrown. */ public static Element ensureMathMLDocument(final Document document) { Element result = document.getDocumentElement(); if (result==null) { throw new IllegalArgumentException("Document does not have a document element"); } ensureMathMLContainer(result); return result; }
private void maybeAddResult(final Map<String, String> resultBuilder, final String key, final Element value, final SerializationSpecifier serializationSpecifier) { if (value!=null) { resultBuilder.put(key, MathMLUtilities.serializeElement(value, serializationSpecifier)); } }
/** * Convenience method that serializes the given DOM Element as a String, indenting if * specified and omitting the XML declaration which is a reasonable way of serializing MathML. * * @param element DOM element to serialize * @param indent whether to indent or not */ public static String serializeElement(final Element element, final boolean indent) { ConstraintUtilities.ensureNotNull(element, "element"); return serializeNode(element, null, indent, true); }
/** * Convenience method to test whether the given DOM {@link Node} node is a MathML element * having any localName. * * @param node Node to test * * @throws IllegalArgumentException if node is null. */ public static boolean isMathMLElement(final Node node) { return isMathMLElement(node, null); }
/** * "Isolates" the XML annotation having the given encoding by producing a copy of the MathML * element with only the given annotation contents as children. * <p> * For example: * <pre> * <math> * <semantics> * <mi>x</mi> * <annotation-xml encoding='blah'><x/></annotation-xml> * </semantics> * </math> * </pre> * results in: * <pre> * <math> * <x/> * </math> * </pre> * * @return new MathML Document with the given structure or null if the given <tt>math</tt> * element is not annotated or does not have the required annotation. * @throws IllegalArgumentException if passed null or the given element is not a * <tt>math</tt> element. */ public static Document isolateAnnotationXML(final Element mathElement, final String encodingAttribute) { NodeList annotationContents = extractAnnotationXML(mathElement, encodingAttribute); return annotationContents != null ? isolateDescendant(mathElement, annotationContents) : null; }
Element firstSemantics = extractFirstSemanticsBranch(mathElement); return firstSemantics != null ? isolateDescendant(mathElement, firstSemantics) : null;
/** * Version of {@link #isolateAnnotationXML(Element, String)} that works on an * {@link UnwrappedParallelMathMLDOM}. * * @return new MathML Document with the given structure or null if the given wrapper does * not have the required annotation. * * @throws IllegalArgumentException if passed null. */ public static Document isolateAnnotationXML(final UnwrappedParallelMathMLDOM unwrappedDOM, final String encodingAttribute) { ConstraintUtilities.ensureNotNull(unwrappedDOM, "UnwrappedParallelMathMLDOM"); NodeList annotationContents = unwrappedDOM.getXmlAnnotations().get(encodingAttribute); return annotationContents!=null ? isolateDescendant(unwrappedDOM.getMathElement(), annotationContents) : null; }
/** * Checks that the given DOM {@link Document} contains a single MathML <tt>math</tt> * Element. If so, the Element is returned. Otherwise, an {@link IllegalArgumentException} is * thrown. */ public static Element ensureMathMLDocument(final Document document) { Element result = document.getDocumentElement(); if (result == null) { throw new IllegalArgumentException("Document does not have a document element"); } ensureMathMLContainer(result); return result; }
public String doMathInput(String mathModeInput, XMLStringOutputOptions xmlStringOutputOptions) { Element mathmlElement = doMathInput(mathModeInput, (DOMOutputOptions) xmlStringOutputOptions); return mathmlElement!=null ? MathMLUtilities.serializeElement(mathmlElement, xmlStringOutputOptions) : null; }
public static Element extractFirstSemanticsBranch(final Element mathElement) { ensureMathMLContainer(mathElement); /* Look for semantics child then annotation child with encoding set appropriately */ Node search = mathElement.getFirstChild(); if (search==null || !isMathMLElement(search, "semantics")) { /* Didn't get <semantics/> as first and only child so not parallel markup */ return null; } Element semantics = (Element) search; NodeList childNodes = semantics.getChildNodes(); return childNodes.getLength()==0 ? null : ((Element) childNodes.item(0)); }
/** * Convenience method that serializes the given DOM Document as a String, using the * specified encoding, indenting the results and omitting the XML declaration, * which is a reasonable way of serializing a MathML document. * * @param document DOM document to serialize, must not be null * @param encoding desired encoding, null is interpreted as UTF-8. */ public static String serializeDocument(final Document document, final String encoding) { ConstraintUtilities.ensureNotNull(document, "document"); return serializeNode(document, encoding, true, true); }