Class MathMLContentParser


  • public class MathMLContentParser
    extends java.lang.Object
    Main class for the Content-MathML parser. The standard single argument constructor builds a parser with a default set of parsing rules. Other rules can be added using the various put(String, TagHandler) methods.

    Various parse methods are available for parsing complete documents or fragments of mathml code. To enable mathml documents containing entities like π to be processed the setPreprendMathMLEntities(boolean) method can be called, if true a simple doctype declaration defining the entities is inserted at the start of the document.

    Author:
    Richard Morris
    See Also:
    Standard setup
    • Constructor Detail

      • MathMLContentParser

        public MathMLContentParser​(Jep jep,
                                   javax.xml.parsers.SAXParserFactory spf,
                                   MathMLDocumentHandler mdh)
        Creates a MathML parser with a given parser factory and MathMLDocumentHandler.
      • MathMLContentParser

        public MathMLContentParser​(Jep jep)
        Creates a standard MathML parser. Uses the default SAXParserFactory, standard MathMLDocumentHandler and default methods for MathML Content elements. Not all elements are supported.
        Parameters:
        jep - the Jep instance
    • Method Detail

      • parseSingle

        public Node parseSingle​(org.xml.sax.InputSource is)
                         throws javax.xml.parsers.ParserConfigurationException,
                                org.xml.sax.SAXException,
                                java.io.IOException
        Parse a document containing a single <math> element with a single child node.
        Parameters:
        is -
        Returns:
        node representing the child node
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException - if document does not contain exactly one math tag, or any non mathml data
        java.io.IOException
      • parseSingle

        public Node parseSingle​(java.io.Reader reader)
                         throws javax.xml.parsers.ParserConfigurationException,
                                org.xml.sax.SAXException,
                                java.io.IOException
        Parse a document containing a single <math> element with a single child node.
        Parameters:
        reader -
        Returns:
        node representing the child node
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException - if document does not contain exactly one math tag, or any non mathml data
        java.io.IOException
      • parseSingle

        public Node parseSingle​(java.lang.String xml)
                         throws javax.xml.parsers.ParserConfigurationException,
                                org.xml.sax.SAXException,
                                java.io.IOException
        Parse a document containing a single <math> element with a single child node.
        Parameters:
        xml - string containing mathml
        Returns:
        node representing the child node
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException - if document does not contain exactly one math tag, or any non mathml data
        java.io.IOException
      • parseFragment

        public Node parseFragment​(org.xml.sax.InputSource is)
                           throws javax.xml.parsers.ParserConfigurationException,
                                  org.xml.sax.SAXException,
                                  java.io.IOException
        Parse a document containing a mathml fragment, not enclosed in a math tag.
        Parameters:
        is -
        Returns:
        node representing the child node
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException - If the document contains non mathml data.
        java.io.IOException
      • parseFragment

        public Node parseFragment​(java.io.Reader reader)
                           throws javax.xml.parsers.ParserConfigurationException,
                                  org.xml.sax.SAXException,
                                  java.io.IOException
        Parse a document containing a mathml fragment, not enclosed in a math tag.
        Parameters:
        reader -
        Returns:
        node representing the child node
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException - If the document contains non mathml data.
        java.io.IOException
      • parseAllMath

        public java.util.List<Node> parseAllMath​(org.xml.sax.InputSource is)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
        Parse a document containing one or more math tags, with one or more child elements. Surrounding xml elements are silently ignored.
        Parameters:
        is -
        Returns:
        a list of all nodes one for each child of the math elements.
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • parseAllMath

        public java.util.List<Node> parseAllMath​(java.io.Reader reader)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
        Parse a document containing one or more math tags, with one or more child elements. Surrounding xml elements are silently ignored.
        Parameters:
        reader -
        Returns:
        a list of all nodes for all child of the math elements.
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • parseAllMath

        public java.util.List<Node> parseAllMath​(java.lang.String xml)
                                          throws javax.xml.parsers.ParserConfigurationException,
                                                 org.xml.sax.SAXException,
                                                 java.io.IOException
        Parse a document containing one or more math tags, with one or more child elements. Surrounding xml elements are silently ignored.
        Parameters:
        xml - String with MathML.
        Returns:
        a list of all nodes for all child of the math elements.
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • parseAll

        public java.lang.Object parseAll​(org.xml.sax.InputSource is,
                                         TagHandler unknownHandler)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
        Parse a document containing one or more math tags, with one or more child elements. Surrounding xml elements are silently ignored.
        Parameters:
        is -
        unknownHandler - a handler for unknown elements
        Returns:
        the result of parsing
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • parseAll

        public java.lang.Object parseAll​(java.io.Reader reader,
                                         TagHandler unknownHandler)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
        Parse a complete xml document containing one or more math tags, with one or more child elements.
        Parameters:
        reader - the Reader to read from
        unknownHandler - a handler for unknown elements
        Returns:
        the result of parsing, actual type depends on the unknownHandler
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • parseAll

        public java.lang.Object parseAll​(java.io.Reader reader,
                                         MathMLContentParser.Behaviour behaviour)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
        Parse a complete xml document containing one or more math tags, with one or more child elements.
        Parameters:
        reader - the Reader to read from
        behaviour -
        Returns:
        the result of parsing, actual type depends on the unknownHandler
        Throws:
        javax.xml.parsers.ParserConfigurationException
        org.xml.sax.SAXException
        java.io.IOException
      • put

        public TagHandler put​(java.lang.String key,
                              TagHandler handler)
        Adds the handler for a given element
        Parameters:
        key - the name of the element
        handler - the handler used for this type of element
        Returns:
        handler
      • putOperator

        public TagHandler putOperator​(java.lang.String key,
                                      Operator op)
        Adds a handler for a Jep operator, uses the operatorHandler
        Parameters:
        key - the name of the mathml element
        op - the jep operator
        Returns:
        the operatorHandler
      • putFunction

        public TagHandler putFunction​(java.lang.String key,
                                      java.lang.String jepName)
        Adds a handler for a Jep function, uses the standardFunctionHandler
        Parameters:
        key - name of the MathML element
        jepName - name of the function used in jep
        Returns:
        standardFunctionHandler
      • putConstantElement

        public TagHandler putConstantElement​(java.lang.String key,
                                             java.lang.String jepName)
        Add a handler for elements representing a constant.
        Parameters:
        key - the name of the mathml element
        jepName - name of the variable used in jep
        Returns:
        constantHandler
      • putSpecialNode

        public TagHandler putSpecialNode​(java.lang.String key,
                                         int nchildren)
        Adds a handler for special nodes like <sep/>, <logbase> and >degree< which only have meaning inside other elements. These are handled by the SpecialNodeHandler
        Parameters:
        key - tag name
        nchildren - number of children
        Returns:
        the handler
      • remove

        public TagHandler remove​(java.lang.Object key)
        Remove the handler associated with a give mathml element.
        Parameters:
        key - name of element
        Returns:
        the handler
      • getMathMLEntitiesString

        public java.lang.String getMathMLEntitiesString()
        Returns a string sith a simple DOCTYPE declaration with defined entities.
        Returns:
        string containing DOCTYPE deceleration
      • isPreprendMathMLEntities

        public boolean isPreprendMathMLEntities()
      • setPreprendMathMLEntities

        public void setPreprendMathMLEntities​(boolean preprendMathMLEntities)
        Whether to prepend a simple deceleration to the start of the document. This can be used to parse documents containing entities like &ExponentialE; without having to include the full MathML DTD.
        Parameters:
        preprendMathMLEntities -
        See Also:
        putEntity(java.lang.String, java.lang.String, java.lang.String)
      • putEntity

        public TagHandler putEntity​(java.lang.String entityName,
                                    java.lang.String unicode,
                                    java.lang.String jepVariableName)
        Adds a handler for a MathML Entity representing a constant. Entity deceleration will be added to MathMLEntitiesString which will be added to the start of the document if preprendMathMLEntities is true.
        Parameters:
        entityName - name of the entity, e.g. "ExponentialE"
        unicode - unicode character for entity, "\\u2147"
        jepVariableName - name of variable used in jep, "e"
        Returns:
        the constantHandler
        See Also:
        setPreprendMathMLEntities(boolean)
      • removeEntity

        public TagHandler removeEntity​(java.lang.Object key)
      • getSAXParserFactory

        public javax.xml.parsers.SAXParserFactory getSAXParserFactory()
      • setSAXParserFactory

        public void setSAXParserFactory​(javax.xml.parsers.SAXParserFactory spf)