1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski #ifndef _XML_PARSER_HXX_ 25*b1cdbd2cSJim Jagielski #define _XML_PARSER_HXX_ 26*b1cdbd2cSJim Jagielski 27*b1cdbd2cSJim Jagielski #include <expat.h> 28*b1cdbd2cSJim Jagielski #include <stdexcept> 29*b1cdbd2cSJim Jagielski 30*b1cdbd2cSJim Jagielski //----------------------------------------------------- 31*b1cdbd2cSJim Jagielski class xml_parser_exception : public std::runtime_error 32*b1cdbd2cSJim Jagielski { 33*b1cdbd2cSJim Jagielski public: 34*b1cdbd2cSJim Jagielski xml_parser_exception(const std::string & error_msg,int error_code,int line_number,int column_number,long byte_index)35*b1cdbd2cSJim Jagielski xml_parser_exception( 36*b1cdbd2cSJim Jagielski const std::string& error_msg, 37*b1cdbd2cSJim Jagielski int error_code, 38*b1cdbd2cSJim Jagielski int line_number, 39*b1cdbd2cSJim Jagielski int column_number, 40*b1cdbd2cSJim Jagielski long byte_index) : 41*b1cdbd2cSJim Jagielski std::runtime_error(error_msg), 42*b1cdbd2cSJim Jagielski error_code_(error_code), 43*b1cdbd2cSJim Jagielski line_number_(line_number), 44*b1cdbd2cSJim Jagielski column_number_(column_number), 45*b1cdbd2cSJim Jagielski byte_index_(byte_index) 46*b1cdbd2cSJim Jagielski {} 47*b1cdbd2cSJim Jagielski 48*b1cdbd2cSJim Jagielski int error_code_; 49*b1cdbd2cSJim Jagielski int line_number_; 50*b1cdbd2cSJim Jagielski int column_number_; 51*b1cdbd2cSJim Jagielski long byte_index_; 52*b1cdbd2cSJim Jagielski }; 53*b1cdbd2cSJim Jagielski 54*b1cdbd2cSJim Jagielski 55*b1cdbd2cSJim Jagielski //----------------------------------------------------- 56*b1cdbd2cSJim Jagielski // Simple wrapper around expat, the xml parser library 57*b1cdbd2cSJim Jagielski // created by James Clark 58*b1cdbd2cSJim Jagielski //----------------------------------------------------- 59*b1cdbd2cSJim Jagielski class i_xml_parser_event_handler; 60*b1cdbd2cSJim Jagielski 61*b1cdbd2cSJim Jagielski class xml_parser 62*b1cdbd2cSJim Jagielski { 63*b1cdbd2cSJim Jagielski public: 64*b1cdbd2cSJim Jagielski //######################################################## 65*b1cdbd2cSJim Jagielski xml_parser(const XML_Char* EncodingName = 0); 66*b1cdbd2cSJim Jagielski 67*b1cdbd2cSJim Jagielski //######################################################## 68*b1cdbd2cSJim Jagielski ~xml_parser(); 69*b1cdbd2cSJim Jagielski 70*b1cdbd2cSJim Jagielski //######################################################## 71*b1cdbd2cSJim Jagielski /** Parse a XML data stream 72*b1cdbd2cSJim Jagielski 73*b1cdbd2cSJim Jagielski @param pXmlData 74*b1cdbd2cSJim Jagielski Pointer to a buffer containing the xml data 75*b1cdbd2cSJim Jagielski 76*b1cdbd2cSJim Jagielski @param Length 77*b1cdbd2cSJim Jagielski Length of the buffer containing the xml data 78*b1cdbd2cSJim Jagielski 79*b1cdbd2cSJim Jagielski @param IsFinal 80*b1cdbd2cSJim Jagielski Indicates whether these are the last xml data 81*b1cdbd2cSJim Jagielski of an xml document to parse. For very large 82*b1cdbd2cSJim Jagielski xml documents it may be usefull to read and 83*b1cdbd2cSJim Jagielski parse the document partially. 84*b1cdbd2cSJim Jagielski 85*b1cdbd2cSJim Jagielski @precond XmlData must not be null 86*b1cdbd2cSJim Jagielski 87*b1cdbd2cSJim Jagielski @throws SaxException 88*b1cdbd2cSJim Jagielski If the used Sax parser returns an error. The SaxException 89*b1cdbd2cSJim Jagielski contains detailed information about the error. */ 90*b1cdbd2cSJim Jagielski void parse(const char* XmlData, size_t Length, bool IsFinal = true); 91*b1cdbd2cSJim Jagielski 92*b1cdbd2cSJim Jagielski //######################################################## 93*b1cdbd2cSJim Jagielski /** Set a document handler 94*b1cdbd2cSJim Jagielski 95*b1cdbd2cSJim Jagielski @descr A document handler implements the interface i_xml_parser_event_handler. 96*b1cdbd2cSJim Jagielski The document handler receive notifications of various events 97*b1cdbd2cSJim Jagielski from the sax parser for instance "start_document". 98*b1cdbd2cSJim Jagielski 99*b1cdbd2cSJim Jagielski The client is responsible for the life time management of 100*b1cdbd2cSJim Jagielski the given document handler, that means the document handler 101*b1cdbd2cSJim Jagielski instance must exist until a new one was set or until the parser 102*b1cdbd2cSJim Jagielski no longer exist. 103*b1cdbd2cSJim Jagielski 104*b1cdbd2cSJim Jagielski @param SaxDocumentHandler 105*b1cdbd2cSJim Jagielski The new document handler, may be null if not interessted in 106*b1cdbd2cSJim Jagielski sax parser events. 107*b1cdbd2cSJim Jagielski 108*b1cdbd2cSJim Jagielski @postcond currently used document handler == pSaxDocumentHandler */ 109*b1cdbd2cSJim Jagielski void set_document_handler(i_xml_parser_event_handler* event_handler); 110*b1cdbd2cSJim Jagielski 111*b1cdbd2cSJim Jagielski //######################################################## 112*b1cdbd2cSJim Jagielski /** Returns the currently used document handler or null if 113*b1cdbd2cSJim Jagielski no document handler was set before. */ 114*b1cdbd2cSJim Jagielski i_xml_parser_event_handler* get_document_handler() const; 115*b1cdbd2cSJim Jagielski private: 116*b1cdbd2cSJim Jagielski 117*b1cdbd2cSJim Jagielski void init(); 118*b1cdbd2cSJim Jagielski 119*b1cdbd2cSJim Jagielski private: 120*b1cdbd2cSJim Jagielski i_xml_parser_event_handler* document_handler_; 121*b1cdbd2cSJim Jagielski XML_Parser xml_parser_; 122*b1cdbd2cSJim Jagielski 123*b1cdbd2cSJim Jagielski // prevent copy and assignment 124*b1cdbd2cSJim Jagielski private: 125*b1cdbd2cSJim Jagielski xml_parser(const xml_parser&); 126*b1cdbd2cSJim Jagielski xml_parser& operator=(const xml_parser&); 127*b1cdbd2cSJim Jagielski }; 128*b1cdbd2cSJim Jagielski 129*b1cdbd2cSJim Jagielski #endif 130*b1cdbd2cSJim Jagielski 131