Top Description Fields Constructors Methods
uk.ac.ed.ph.snuggletex.utilities

public final Class MathMLUtilities

extends Object
Class Inheritance
Imports
org.w3c.dom.Document, .Element, .Node, .NodeList, org.xml.sax.InputSource, .SAXException, uk.ac.ed.ph.snuggletex.SerializationSpecifier, uk.ac.ed.ph.snuggletex.definitions.W3CConstants, uk.ac.ed.ph.snuggletex.internal.util.ConstraintUtilities, .XMLUtilities, java.io.IOException, .StringReader

Some general utility methods for manipulating MathML via the DOM.
Author
David McKain
Version
$Revision$

Field Summary

Modifier and TypeField and Description
public static final String
public static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static void
ensureMathMLContainer(final Element mathElement)

public static Element
ensureMathMLDocument(final Document document)

Checks that the given DOM Document contains a single MathML math Element.

private static Element
extractAnnotationElement(final Element mathmlElement, final String annotationElementLocalName, String encodingAttribute)

public static String

Returns:

first matching annotation, or null if not present.
extractAnnotationString
(final Element mathElement, final String encodingAttribute)

Extracts the first textual annotation found having the given encoding attribute from the given MathML math element, if such an annotation is found.

public static NodeList

Returns:

DOM NodeList corresponding to the child Nodes of the first matching annotation-xml element, or null if no such annotation found.
extractAnnotationXML
(final Element mathElement, final String encodingAttribute)

Extracts the first XML-based annotation having the given encoding attribute from the given MathML math element, if such an annotation is found.

public static Element
public static boolean
isMathMLElement(final Node
Node to test
node
)

Convenience method to test whether the given DOM Node node is a MathML element having any localName.

public static boolean
isMathMLElement(final Node
Node to test
node
,
final String
MathML local name to test for, such as mi or math, or null to indicate "any name in the MathML namespace".
localName
)

Convenience method to test whether the given DOM Node node is a MathML element having the given localName.

public static Document

Returns:

new MathML Document with the given structure or null if the given math element is not annotated or does not have the required annotation.
isolateAnnotationXML
(final Element mathElement, final String encodingAttribute)

"Isolates" the XML annotation having the given encoding by producing a copy of the MathML element with only the given annotation contents as children.

public static Document

Returns:

new MathML Document with the given structure or null if the given wrapper does not have the required annotation.
isolateAnnotationXML
(final UnwrappedParallelMathMLDOM unwrappedDOM, final String encodingAttribute)

Version of isolateAnnotationXML(Element, String) that works on an UnwrappedParallelMathMLDOM.

private static Document
isolateDescendant(final Element mathElement, final NodeList descendants)

private static Document
isolateDescendant(final Element mathElement, final Element descendant)

public static Document

Returns:

new MathML Document with the given structure or null if the given math element is not annotated.
isolateFirstSemanticsBranch
(final Element mathElement)

"Isolates" the first <semantics/> branch of an annotation MathML element by producing a copy of the MathML element with a single child containing only the first child of the <semantics/> element.

public static Document

Returns:

new MathML Document with the given structure or null if the given wrapper has no first branch.
isolateFirstSemanticsBranch
(final UnwrappedParallelMathMLDOM unwrappedDOM)

Version of isolateFirstSemanticsBranch(Element) that works on an UnwrappedParallelMathMLDOM.

public static Document
parseMathMLDocumentString(final String mathmlDocument)

Convenience method that parses a MathML document specified as a UTF-8-encoded String.

public static String
serializeDocument(final Document
DOM document to serialize, must not be null
document
)

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.

public static String
serializeDocument(final Document
DOM document to serialize, must not be null
document
,
final SerializationSpecifier
desired SerializationOptions, may be null.
serializationOptions
)

Convenience method that serializes the given DOM Document as a String, using the specified SerializationSpecifier, creating any XSLT stylesheet required for serialization on an ad hoc basis.

public static String
serializeDocument(final Document
DOM document to serialize, must not be null
document
,
final SerializationSpecifier
desired SerializationOptions, may be null.
serializationOptions
,
StylesheetManager stylesheetManager)

Convenience method that serializes the given DOM Document as a String, using the specified SerializationSpecifier and given StylesheetManager to manage any XSLT stylesheets required to serialize properly.

public static String
serializeDocument(final Document
DOM document to serialize, must not be null
document
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
)

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.

public static String
serializeDocument(final Document
DOM document to serialize, must not be null
document
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
,
final boolean indent)

Convenience method that serializes the given DOM Document as a String, using the specified encoding and indentation options and omitting the XML declaration, which is a reasonable way of serializing a MathML document.

public static String
serializeDocument(final Document
DOM element to serialize
document
,
final boolean
whether to indent the results or not
indent
,
final boolean
whether to omit the XML declaration or not.
omitXMLDeclaration
)
Deprecated Use one of the alternative methods, including serializeDocument(Document, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Document as a String (encoded in UTF-8).

public static String
serializeDocument(final Document
DOM element to serialize
document
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
,
final boolean
whether to indent the results or not
indent
,
final boolean
whether to omit the XML declaration or not.
omitXMLDeclaration
)
Deprecated Use one of the alternative methods, including serializeDocument(Document, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Document as a String, using the specified encoding.

public static String
serializeElement(final Element
DOM element to serialize, must not be null.
element
)

Convenience method that serializes the given DOM Element as a String (encoded in UTF-8), indenting the results and omitting the XML declaration, which is a reasonable way of serializing MathML.

public static String
serializeElement(final Element
DOM element to serialize, must not be null.
element
,
final SerializationSpecifier serializationOptions)

Convenience method that serializes the given DOM Element as a String, using the given SerializationSpecifier, creating any XSLT stylesheet required for serialization on an ad hoc basis.

public static String
serializeElement(final Element
DOM element to serialize, must not be null.
element
,
final SerializationSpecifier serializationOptions, StylesheetManager
StylesheetManager, must not be null
stylesheetManager
)

Convenience method that serializes the given DOM Element as a String, using the given SerializationSpecifier and given StylesheetManager to manage any XSLT stylesheets required to serialize properly.

public static String
serializeElement(final Element
DOM element to serialize
element
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
)

Convenience method that serializes the given DOM Element as a String, using the given encoding, indenting the results and omitting the XML declaration, which is a reasonable way of serializing MathML.

public static String
serializeElement(final Element
DOM element to serialize
element
,
final boolean
whether to indent or not
indent
)

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.

public static String
serializeElement(final Element
DOM element to serialize
element
,
final boolean
whether to indent the results or not
indent
,
final boolean
whether to omit the XML declaration or not.
omitXMLDeclaration
)
Deprecated Use one of the alternative methods, including serializeElement(Element, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Element as a String (encoded in UTF-8).

public static String
serializeElement(final Element
DOM element to serialize
element
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
,
final boolean
whether to indent the results or not
indent
,
final boolean
whether to omit the XML declaration or not.
omitXMLDeclaration
)

Convenience method that serializes the given DOM Element as a String, using the given parameters.

public static String
serializeElement(final Element
DOM element to serialize
element
,
final String
desired encoding, null is interpreted as UTF-8.
encoding
,
final boolean
whether to indent the results or not
indent
)

Convenience method that serializes the given DOM Element as a String, using the given parameters.

private static String
serializeNode(final Node node, final SerializationSpecifier serializationOptions)

private static String
serializeNode(final Node node, final String encoding, final boolean indent, final boolean omitXMLDeclaration)

private static String
serializeNode(StylesheetManager stylesheetManager, final Node node, final SerializationSpecifier serializationOptions)

public static UnwrappedParallelMathMLDOM
unwrapParallelMathMLDOM(final Element mathElement)

Convenience method to unwrap at MathML DOM Object containing top-level parallel markup, as defined Section 5.3.1 of the MathML 2.0 specification.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ANNOTATION_LOCAL_NAMEback to summary
public static final String ANNOTATION_LOCAL_NAME
ANNOTATION_XML_LOCAL_NAMEback to summary
public static final String ANNOTATION_XML_LOCAL_NAME

Constructor Detail

MathMLUtilitiesback to summary
public MathMLUtilities()

Method Detail

ensureMathMLContainerback to summary
public static void ensureMathMLContainer(final Element mathElement)
ensureMathMLDocumentback to summary
public static Element ensureMathMLDocument(final Document document)

Checks that the given DOM Document contains a single MathML math Element. If so, the Element is returned. Otherwise, an IllegalArgumentException is thrown.

extractAnnotationElementback to summary
private static Element extractAnnotationElement(final Element mathmlElement, final String annotationElementLocalName, String encodingAttribute)
extractAnnotationStringback to summary
public static String extractAnnotationString(final Element mathElement, final String encodingAttribute)

Extracts the first textual annotation found having the given encoding attribute from the given MathML math element, if such an annotation is found.

This assumes the following structure:

<math>
  <semantics>
    ...
    <annotation encoding="...">text</annotation>
  </semantics>
</math>
Returns:String

first matching annotation, or null if not present.

extractAnnotationXMLback to summary
public static NodeList extractAnnotationXML(final Element mathElement, final String encodingAttribute)

Extracts the first XML-based annotation having the given encoding attribute from the given MathML math element, if such an annotation is found.

This assumes the following structure:

<math>
  <semantics>
    ...
    <annotation-xml encoding="...">XML annotation</annotation>
  </semantics>
</math>
Returns:NodeList

DOM NodeList corresponding to the child Nodes of the first matching annotation-xml element, or null if no such annotation found.

extractFirstSemanticsBranchback to summary
public static Element extractFirstSemanticsBranch(final Element mathElement)
isMathMLElementback to summary
public static boolean isMathMLElement(final Node node)

Convenience method to test whether the given DOM Node node is a MathML element having any localName.

Parameters
node:Node

Node to test

Exceptions
IllegalArgumentException:
if node is null.
isMathMLElementback to summary
public static boolean isMathMLElement(final Node node, final String localName)

Convenience method to test whether the given DOM Node node is a MathML element having the given localName.

Parameters
node:Node

Node to test

localName:String

MathML local name to test for, such as mi or math, or null to indicate "any name in the MathML namespace".

Exceptions
IllegalArgumentException:
if node is null.
isolateAnnotationXMLback to summary
public static Document isolateAnnotationXML(final Element mathElement, final String encodingAttribute)

"Isolates" the XML annotation having the given encoding by producing a copy of the MathML element with only the given annotation contents as children.

For example:

  <math>
    <semantics>
      <mi>x</mi>
      <annotation-xml encoding='blah'><x/></annotation-xml>
    </semantics>
  </math>
  
results in:
  <math>
    <x/>
  </math>
  
Returns:Document

new MathML Document with the given structure or null if the given math element is not annotated or does not have the required annotation.

Exceptions
IllegalArgumentException:
if passed null or the given element is not a math element.
isolateAnnotationXMLback to summary
public static Document isolateAnnotationXML(final UnwrappedParallelMathMLDOM unwrappedDOM, final String encodingAttribute)

Version of isolateAnnotationXML(Element, String) that works on an UnwrappedParallelMathMLDOM.

Returns:Document

new MathML Document with the given structure or null if the given wrapper does not have the required annotation.

Exceptions
IllegalArgumentException:
if passed null.
isolateDescendantback to summary
private static Document isolateDescendant(final Element mathElement, final NodeList descendants)
isolateDescendantback to summary
private static Document isolateDescendant(final Element mathElement, final Element descendant)
isolateFirstSemanticsBranchback to summary
public static Document isolateFirstSemanticsBranch(final Element mathElement)

"Isolates" the first <semantics/> branch of an annotation MathML element by producing a copy of the MathML element with a single child containing only the first child of the <semantics/> element.

For example:

  <math>
    <semantics>
      <mi>x</mi>
      <annotation-xml encoding='blah'><x/></annotation-xml>
    </semantics>
  </math>
  
results in:
  <math>
    <mi>x</mi>
  </math>
  
Returns:Document

new MathML Document with the given structure or null if the given math element is not annotated.

Exceptions
IllegalArgumentException:
if passed null or the given element is not a math element.
isolateFirstSemanticsBranchback to summary
public static Document isolateFirstSemanticsBranch(final UnwrappedParallelMathMLDOM unwrappedDOM)

Version of isolateFirstSemanticsBranch(Element) that works on an UnwrappedParallelMathMLDOM.

Returns:Document

new MathML Document with the given structure or null if the given wrapper has no first branch.

Exceptions
IllegalArgumentException:
if passed null.
parseMathMLDocumentStringback to summary
public static Document parseMathMLDocumentString(final String mathmlDocument) throws IOException, SAXException

Convenience method that parses a MathML document specified as a UTF-8-encoded String.

serializeDocumentback to summary
public static String serializeDocument(final Document document)

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.

Parameters
document:Document

DOM document to serialize, must not be null

serializeDocumentback to summary
public static String serializeDocument(final Document document, final SerializationSpecifier serializationOptions)

Convenience method that serializes the given DOM Document as a String, using the specified SerializationSpecifier, creating any XSLT stylesheet required for serialization on an ad hoc basis.

Parameters
document:Document

DOM document to serialize, must not be null

serializationOptions:SerializationSpecifier

desired SerializationOptions, may be null.

Since
1.2.0
serializeDocumentback to summary
public static String serializeDocument(final Document document, final SerializationSpecifier serializationOptions, StylesheetManager stylesheetManager)

Convenience method that serializes the given DOM Document as a String, using the specified SerializationSpecifier and given StylesheetManager to manage any XSLT stylesheets required to serialize properly.

Parameters
document:Document

DOM document to serialize, must not be null

serializationOptions:SerializationSpecifier

desired SerializationOptions, may be null.

Since
1.2.0
serializeDocumentback to summary
public static String serializeDocument(final Document document, final String encoding)

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.

Parameters
document:Document

DOM document to serialize, must not be null

encoding:String

desired encoding, null is interpreted as UTF-8.

serializeDocumentback to summary
public static String serializeDocument(final Document document, final String encoding, final boolean indent)

Convenience method that serializes the given DOM Document as a String, using the specified encoding and indentation options and omitting the XML declaration, which is a reasonable way of serializing a MathML document.

Parameters
document:Document

DOM document to serialize, must not be null

encoding:String

desired encoding, null is interpreted as UTF-8.

serializeDocumentback to summary
public static String serializeDocument(final Document document, final boolean indent, final boolean omitXMLDeclaration)

Deprecated

Use one of the alternative methods, including serializeDocument(Document, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Document as a String (encoded in UTF-8).

Parameters
document:Document

DOM element to serialize

indent:boolean

whether to indent the results or not

omitXMLDeclaration:boolean

whether to omit the XML declaration or not.

Annotations
@Deprecated
serializeDocumentback to summary
public static String serializeDocument(final Document document, final String encoding, final boolean indent, final boolean omitXMLDeclaration)

Deprecated

Use one of the alternative methods, including serializeDocument(Document, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Document as a String, using the specified encoding.

Parameters
document:Document

DOM element to serialize

encoding:String

desired encoding, null is interpreted as UTF-8.

indent:boolean

whether to indent the results or not

omitXMLDeclaration:boolean

whether to omit the XML declaration or not.

Annotations
@Deprecated
serializeElementback to summary
public static String serializeElement(final Element element)

Convenience method that serializes the given DOM Element as a String (encoded in UTF-8), indenting the results and omitting the XML declaration, which is a reasonable way of serializing MathML.

Parameters
element:Element

DOM element to serialize, must not be null.

serializeElementback to summary
public static String serializeElement(final Element element, final SerializationSpecifier serializationOptions)

Convenience method that serializes the given DOM Element as a String, using the given SerializationSpecifier, creating any XSLT stylesheet required for serialization on an ad hoc basis.

Parameters
element:Element

DOM element to serialize, must not be null.

serializationOptions:SerializationSpecifier

SerializationSpecifier, may be null.

Since
1.2.0
serializeElementback to summary
public static String serializeElement(final Element element, final SerializationSpecifier serializationOptions, StylesheetManager stylesheetManager)

Convenience method that serializes the given DOM Element as a String, using the given SerializationSpecifier and given StylesheetManager to manage any XSLT stylesheets required to serialize properly.

Parameters
element:Element

DOM element to serialize, must not be null.

serializationOptions:SerializationSpecifier

SerializationSpecifier, may be null.

stylesheetManager:StylesheetManager

StylesheetManager, must not be null

Since
1.2.0
serializeElementback to summary
public static String serializeElement(final Element element, final String encoding)

Convenience method that serializes the given DOM Element as a String, using the given encoding, indenting the results and omitting the XML declaration, which is a reasonable way of serializing MathML.

Parameters
element:Element

DOM element to serialize

encoding:String

desired encoding, null is interpreted as UTF-8.

serializeElementback to summary
public static String serializeElement(final Element element, final boolean indent)

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.

Parameters
element:Element

DOM element to serialize

indent:boolean

whether to indent or not

serializeElementback to summary
public static String serializeElement(final Element element, final boolean indent, final boolean omitXMLDeclaration)

Deprecated

Use one of the alternative methods, including serializeElement(Element, SerializationSpecifier) for ultimate control.

Convenience method that serializes the given DOM Element as a String (encoded in UTF-8).

Parameters
element:Element

DOM element to serialize

indent:boolean

whether to indent the results or not

omitXMLDeclaration:boolean

whether to omit the XML declaration or not.

Annotations
@Deprecated
serializeElementback to summary
public static String serializeElement(final Element element, final String encoding, final boolean indent, final boolean omitXMLDeclaration)

Convenience method that serializes the given DOM Element as a String, using the given parameters.

Parameters
element:Element

DOM element to serialize

encoding:String

desired encoding, null is interpreted as UTF-8.

indent:boolean

whether to indent the results or not

omitXMLDeclaration:boolean

whether to omit the XML declaration or not.

Annotations
@Deprecated
serializeElementback to summary
public static String serializeElement(final Element element, final String encoding, final boolean indent)

Convenience method that serializes the given DOM Element as a String, using the given parameters.

(This can be used in more general cases than MathML, through experienced programmers may want more control over what happens here.)

Parameters
element:Element

DOM element to serialize

encoding:String

desired encoding, null is interpreted as UTF-8.

indent:boolean

whether to indent the results or not

serializeNodeback to summary
private static String serializeNode(final Node node, final SerializationSpecifier serializationOptions)
serializeNodeback to summary
private static String serializeNode(final Node node, final String encoding, final boolean indent, final boolean omitXMLDeclaration)
serializeNodeback to summary
private static String serializeNode(StylesheetManager stylesheetManager, final Node node, final SerializationSpecifier serializationOptions)
unwrapParallelMathMLDOMback to summary
public static UnwrappedParallelMathMLDOM unwrapParallelMathMLDOM(final Element mathElement)

Convenience method to unwrap at MathML DOM Object containing top-level parallel markup, as defined Section 5.3.1 of the MathML 2.0 specification.

If there is no parallel markup detected then null if returned.

If the given element is null or is not a MathML "math" element, then an IllegalArgumentException is thrown.