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