Class MathMLContentParser
- java.lang.Object
-
- com.singularsys.extensions.mathml.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 variousput(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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MathMLContentParser.Behaviour
-
Field Summary
Fields Modifier and Type Field Description protected AnnotationHandler
annotationHandler
protected ApplyHandler
applyHandler
protected ConstantHandler
constantHandler
protected DeclareHandler
declareHandler
protected IdentifierHandler
identifierHandler
protected Jep
jep
protected MathTagHandler
mathElementHandler
protected java.util.Map<java.lang.String,java.lang.String>
mathMLEntities
protected static java.lang.String
MathMLEntitiesEnd
protected static java.lang.String
MathMLEntitiesStart
protected java.lang.String
mathMLEntitiesString
String with a simple DOCTYPE declaration defining MathML entities.protected MatrixHandler
matrixHandler
protected MathMLDocumentHandler
mdh
protected OperatorHandler
operatorHandler
protected OperatorTableI
ot
protected SemanticHandler
semanticHandler
protected SpecialNodeHandler
specialNodeHandler
protected javax.xml.parsers.SAXParserFactory
spf
protected StandardFunctionHandler
standardFunctionHandler
protected UnSupportedHandler
unsuportedHandler
-
Constructor Summary
Constructors Constructor Description MathMLContentParser(Jep jep)
Creates a standard MathML parser.MathMLContentParser(Jep jep, javax.xml.parsers.SAXParserFactory spf, MathMLDocumentHandler mdh)
Creates a MathML parser with a given parser factory and MathMLDocumentHandler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getMathMLEntitiesString()
Returns a string sith a simple DOCTYPE declaration with defined entities.javax.xml.parsers.SAXParserFactory
getSAXParserFactory()
boolean
isPreprendMathMLEntities()
java.lang.Object
parseAll(java.io.Reader reader, MathMLContentParser.Behaviour behaviour)
Parse a complete xml document containing one or more math tags, with one or more child elements.java.lang.Object
parseAll(java.io.Reader reader, TagHandler unknownHandler)
Parse a complete xml document containing one or more math tags, with one or more child elements.java.lang.Object
parseAll(org.xml.sax.InputSource is, TagHandler unknownHandler)
Parse a document containing one or more math tags, with one or more child elements.java.util.List<Node>
parseAllMath(java.io.Reader reader)
Parse a document containing one or more math tags, with one or more child elements.java.util.List<Node>
parseAllMath(java.lang.String xml)
Parse a document containing one or more math tags, with one or more child elements.java.util.List<Node>
parseAllMath(org.xml.sax.InputSource is)
Parse a document containing one or more math tags, with one or more child elements.Node
parseFragment(java.io.Reader reader)
Parse a document containing a mathml fragment, not enclosed in a math tag.Node
parseFragment(org.xml.sax.InputSource is)
Parse a document containing a mathml fragment, not enclosed in a math tag.Node
parseSingle(java.io.Reader reader)
Parse a document containing a single <math> element with a single child node.Node
parseSingle(java.lang.String xml)
Parse a document containing a single <math> element with a single child node.Node
parseSingle(org.xml.sax.InputSource is)
Parse a document containing a single <math> element with a single child node.TagHandler
put(java.lang.String key, TagHandler handler)
Adds the handler for a given elementTagHandler
putConstantElement(java.lang.String key, java.lang.String jepName)
Add a handler for elements representing a constant.TagHandler
putEntity(java.lang.String entityName, java.lang.String unicode, java.lang.String jepVariableName)
Adds a handler for a MathML Entity representing a constant.TagHandler
putFunction(java.lang.String key, java.lang.String jepName)
Adds a handler for a Jep function, uses the standardFunctionHandlerTagHandler
putOperator(java.lang.String key, Operator op)
Adds a handler for a Jep operator, uses the operatorHandlerTagHandler
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.TagHandler
remove(java.lang.Object key)
Remove the handler associated with a give mathml element.TagHandler
removeEntity(java.lang.Object key)
void
setPreprendMathMLEntities(boolean preprendMathMLEntities)
Whether to prepend a simple deceleration to the start of the document.void
setSAXParserFactory(javax.xml.parsers.SAXParserFactory spf)
-
-
-
Field Detail
-
spf
protected javax.xml.parsers.SAXParserFactory spf
-
mdh
protected MathMLDocumentHandler mdh
-
jep
protected Jep jep
-
ot
protected OperatorTableI ot
-
mathElementHandler
protected MathTagHandler mathElementHandler
-
unsuportedHandler
protected UnSupportedHandler unsuportedHandler
-
constantHandler
protected ConstantHandler constantHandler
-
identifierHandler
protected IdentifierHandler identifierHandler
-
operatorHandler
protected OperatorHandler operatorHandler
-
applyHandler
protected ApplyHandler applyHandler
-
standardFunctionHandler
protected StandardFunctionHandler standardFunctionHandler
-
specialNodeHandler
protected SpecialNodeHandler specialNodeHandler
-
matrixHandler
protected MatrixHandler matrixHandler
-
declareHandler
protected DeclareHandler declareHandler
-
semanticHandler
protected SemanticHandler semanticHandler
-
annotationHandler
protected AnnotationHandler annotationHandler
-
mathMLEntitiesString
protected java.lang.String mathMLEntitiesString
String with a simple DOCTYPE declaration defining MathML entities.
-
MathMLEntitiesStart
protected static final java.lang.String MathMLEntitiesStart
- See Also:
- Constant Field Values
-
MathMLEntitiesEnd
protected static final java.lang.String MathMLEntitiesEnd
- See Also:
- Constant Field Values
-
mathMLEntities
protected java.util.Map<java.lang.String,java.lang.String> mathMLEntities
-
-
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 datajava.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 datajava.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 datajava.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 fromunknownHandler
- 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 frombehaviour
-- 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 elementhandler
- 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 elementop
- 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 elementjepName
- 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 elementjepName
- 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 theSpecialNodeHandler
- Parameters:
key
- tag namenchildren
- 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 ⅇ 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)
-
-