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