1 /****************************************************************************** 2 Copyright (c) 2007 netAllied GmbH, Tettnang 3 4 Permission is hereby granted, free of charge, to any person 5 obtaining a copy of this software and associated documentation 6 files (the "Software"), to deal in the Software without 7 restriction, including without limitation the rights to use, 8 copy, modify, merge, publish, distribute, sublicense, and/or sell 9 copies of the Software, and to permit persons to whom the 10 Software is furnished to do so, subject to the following 11 conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 OTHER DEALINGS IN THE SOFTWARE. 24 ******************************************************************************/ 25 26 #ifndef __MATHML_PARSER_H__ 27 #define __MATHML_PARSER_H__ 28 29 #include "MathMLSolverPrerequisites.h" 30 #include <xercesc/parsers/XercesDOMParser.hpp> 31 #include "MathMLASTConstantExpression.h" 32 33 namespace MathML 34 { 35 /** Parses a MathML file and creates an AST. 36 @todo: Convenience method returning lists, strings, etc. created within, should be created 37 in the calling method and passed as reference (-> no copying of strings, lists, etc). 38 E.g.: 39 - IS: ElementList getChildElements ( xercesc::DOMElement* element ) 40 - SHOULD BE: void getChildElements ( ElementList& result, xercesc::DOMElement* element ) 41 42 */ 43 44 class _MATHML_SOLVER_EXPORT Parser 45 { 46 47 private: 48 49 /** The file path/url. */ 50 String mFile; 51 52 /** The xml parser. */ 53 xercesc::XercesDOMParser* xmlParser; 54 /** The parser handler for notification. */ 55 MathML::ParserHandler* mParserHandler; 56 /** Temporary list of variable names. */ 57 ParserHandler::VariableList mVariableList; 58 59 public: 60 /** Type definition for a list DOM nodes of type 'element'. */ 61 typedef std::vector<xercesc::DOMElement*> ElementList; 62 /** Type definition for a list DOM nodes of type 'text'. */ 63 typedef std::vector<xercesc::DOMText*> TextNodeList; 64 65 /** C-tor. 66 @param handler The parser handler being notifiied. 67 */ 68 Parser( MathML::ParserHandler* handler = 0 ); 69 70 /** D-tor. */ 71 virtual ~Parser(); 72 73 /** Getter for path/url of parsing document. */ 74 virtual const String& getFile(); 75 76 /** Methode for starting a parse-process. 77 @param fileName The string of a file path or url. 78 */ 79 virtual void parse( const String& fileName ); 80 81 private: 82 /** Getter for retrieving the parser handler. */ 83 virtual MathML::ParserHandler* getParserHandler(); 84 85 /** Convenience method for creating (double) constant expression 86 from a xercesc character pointer. 87 @param xmlValue The xercesc character pointer. 88 @return The AST::ConstantExpression for the given string. 89 */ 90 virtual AST::ConstantExpression* createConstant( const XMLCh* xmlValue ); 91 92 /** Convenience method for retrieving DOM child nodes of type 93 'element' for the given DOMElement. 94 @param element The element for retrieving its child elements. 95 */ 96 virtual ElementList getChildElements ( xercesc::DOMElement* element ); 97 98 /** Convenience method for checking if given child node is 99 of the given name. 100 @param node The DOMNode to be checked. 101 @param name The name to check against. 102 @return True if node is of given name, otherwise false. 103 */ 104 virtual bool isElementOfName( xercesc::DOMNode* node, String name ); 105 106 /** Convenience method for retrieving a element from a document 107 by its id-attribute. 108 @param doc The DOMDocument to be searched. 109 @param elementId The id to search for. 110 @return The found DOMElement or 0. 111 */ 112 virtual xercesc::DOMElement* getElementById( xercesc::DOMDocument* doc, const String& elementId ); 113 114 /** Convenience method for retrieving the attribute value for 115 the given attribute name. 116 @param element The DOMElement to be search for the attribute. 117 @param attributeName The name of the attribute to search for. 118 @return The string representation of its value if found, otherwise an empty string. 119 */ 120 virtual String getAttributeValue ( xercesc::DOMElement* element, String attributeName ); 121 122 /** Convenience method for retrieving child text nodes from a given DOMElement. 123 @param element The DOMElement to be search for the text nodes. 124 @return A list of found text nodes, otherwise an empty list. 125 */ 126 virtual TextNodeList getTextNodes ( xercesc::DOMElement* element ); 127 128 129 /** Method for retrieving a unary operator from the given 130 (MathML) DOMElement. 131 @param element The element to be check for a unary operator. 132 @return The string representation for a correct unary operator, 133 otherwise the string constant 'NO_OPERATOR'. 134 */ 135 virtual const String& getUnaryOperator ( xercesc::DOMElement* element ); 136 137 /** Method for retrieving a binary operator from the given 138 (MathML) DOMElement. 139 @param element The element to be check for a binary operator. 140 @return The string representation for a correct binary operator, 141 otherwise the string constant 'NO_OPERATOR'. 142 */ 143 virtual const String& getBinaryOperator ( xercesc::DOMElement* element ); 144 145 /** Method for retrieving a relation operator from the given 146 (MathML) DOMElement. 147 @param element The element to be check for a relation operator. 148 @return The string representation for a correct relation operator, 149 otherwise the string constant 'NO_OPERATOR'. 150 */ 151 virtual const String& getRelationOperator ( xercesc::DOMElement* element ); 152 153 /** Method for retrieving a logic operator from the given 154 (MathML) DOMElement. 155 @param element The element to be check for a logic operator. 156 @return The string representation for a correct logic operator, 157 otherwise the string constant 'NO_OPERATOR'. 158 */ 159 virtual const String& getLogicOperator ( xercesc::DOMElement* element ); 160 161 /** Method for retrieving the name of a function from the given 162 (MathML) DOMElement. 163 @param element The element to be check for a function name. 164 @return The string representation for a correct function name, 165 otherwise the string constant 'NO_FUNCTION'. 166 */ 167 virtual const String& getFunction ( xercesc::DOMElement* element ); 168 169 /** Methode for handling the given DOMElement. 170 @param element The element to be further processed for its name (and attributes). 171 @return The common interface of the underlying expression. 172 */ 173 virtual AST::INode* handleElement ( xercesc::DOMElement* element ); 174 175 /** Methode for handling (MathML) <apply>-elements. 176 @param element The element to be further processed for its children and attributes. 177 @return The common interface of the underlying expression. 178 */ 179 virtual AST::INode* handleElementApply( xercesc::DOMElement* element ); 180 181 /** Methode for handling (MathML) <declare>-elements. 182 @param element The element to be further processed for its children and attributes). 183 @param siblings The list of siblings holding the parameter arguments for this declaration-construct. 184 @return The interface of an AST::FragmentExpression -node. 185 */ 186 virtual AST::INode* handleElementDeclare( xercesc::DOMElement* element, const ElementList& siblings ); 187 188 /** Methode for handling 'external' defined fragments 189 (or code) referenced by (MathML) <csymbol>-elements. 190 @param firstChild The identified<csymbol>-element of the given list. 191 @param childList The ElementList containing the <csymbol..>-element and its siblings. 192 @return The interface of an AST::FragmentExpression -node. 193 */ 194 virtual AST::INode* handleExternalReferenceByCSymbol( xercesc::DOMElement* firstChild, ElementList& childList ); 195 196 /** Methode for handling (MathML) <cn>-elements. 197 @param element The <cn> -DOMElement.. 198 @return The interface of an AST::ConstantExpression -node. 199 */ 200 virtual AST::INode* handleElementCN( xercesc::DOMElement* element ); 201 202 /** Methode for handling (MathML) <ci>-elements. 203 @param element The <ci> -DOMElement.. 204 @return The interface of an AST::VariableExpression -node. 205 */ 206 virtual AST::INode* handleElementCI( xercesc::DOMElement* element ); 207 208 /** Methode for handling (MathML) <logbase>-elements. 209 @param element The <logbase> -DOMElement.. 210 @return The interface of an AST-node representening the base for a logarithm. 211 */ 212 virtual AST::INode* handleElementLogbase( xercesc::DOMElement* element ); 213 214 /** Methode for handling (MathML) <pi>-elements. 215 @param element The <pi> -DOMElement.. 216 @return The interface of an AST::ConstantExpression -node for the constant 'PI'. 217 */ 218 virtual AST::INode* handleElementPI( xercesc::DOMElement* element ); 219 220 /** Methode for handling (MathML) <exponentiale>-elements. 221 @param element The <exponentiale> -DOMElement.. 222 @return The interface of an AST::ConstantExpression -node for the constant 'e'. 223 */ 224 virtual AST::INode* handleElementExponentiale( xercesc::DOMElement* element ); 225 226 /** Methode for handling (MathML) <true>-elements. 227 @param element The <true> -DOMElement.. 228 @return The interface of an AST::ConstantExpression -node for the boolean constant 'true'. 229 */ 230 virtual AST::INode* handleElementTrue( xercesc::DOMElement* element ); 231 232 /** Methode for handling (MathML) <false>-elements. 233 @param element The <false> -DOMElement.. 234 @return The interface of an AST::ConstantExpression -node for the boolean constant 'false'. 235 */ 236 virtual AST::INode* handleElementFalse( xercesc::DOMElement* element ); 237 238 239 /** Methode for handling unary operations. 240 @param elements The list of elements containing operator and operand(s). 241 @return The interface of an AST::UnaryExpression -node. 242 */ 243 virtual AST::INode* handleUnaryOperation( ElementList& elements ); 244 245 /** Methode for handling operations with two operands. 246 @param elements The list of elements containing operator and operand(s). 247 @return The interface of an specialized AST::ExpressionNode (arithmetic, logic, ..). 248 */ 249 virtual AST::INode* handleBinaryOperation( ElementList& elements ); 250 251 /** Methode for handling operations with more than two operands. 252 @param elements The list of elements containing operator and operands. 253 @return The interface of an specialized AST::ExpressionNode (arithmetic, logic, ..) 254 but no AST::BinaryComparisionExpression (relations). 255 */ 256 virtual AST::INode* handleMultiOperandOperation( ElementList& elements ); 257 258 259 /** todo: description */ 260 virtual String getContainer( const String& url ); 261 /** todo: description */ 262 virtual String getId( const String& url ); 263 }; 264 265 } //namespace MathML 266 267 #endif //__MATHML_PARSER_H__ 268