1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef _XML_PARSER_HXX_ 29*cdf0e10cSrcweir #define _XML_PARSER_HXX_ 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include <expat.h> 32*cdf0e10cSrcweir #include <stdexcept> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir //----------------------------------------------------- 35*cdf0e10cSrcweir class xml_parser_exception : public std::runtime_error 36*cdf0e10cSrcweir { 37*cdf0e10cSrcweir public: 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir xml_parser_exception( 40*cdf0e10cSrcweir const std::string& error_msg, 41*cdf0e10cSrcweir int error_code, 42*cdf0e10cSrcweir int line_number, 43*cdf0e10cSrcweir int column_number, 44*cdf0e10cSrcweir long byte_index) : 45*cdf0e10cSrcweir std::runtime_error(error_msg), 46*cdf0e10cSrcweir error_code_(error_code), 47*cdf0e10cSrcweir line_number_(line_number), 48*cdf0e10cSrcweir column_number_(column_number), 49*cdf0e10cSrcweir byte_index_(byte_index) 50*cdf0e10cSrcweir {} 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir int error_code_; 53*cdf0e10cSrcweir int line_number_; 54*cdf0e10cSrcweir int column_number_; 55*cdf0e10cSrcweir long byte_index_; 56*cdf0e10cSrcweir }; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir //----------------------------------------------------- 60*cdf0e10cSrcweir // Simple wrapper around expat, the xml parser library 61*cdf0e10cSrcweir // created by James Clark 62*cdf0e10cSrcweir //----------------------------------------------------- 63*cdf0e10cSrcweir class i_xml_parser_event_handler; 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir class xml_parser 66*cdf0e10cSrcweir { 67*cdf0e10cSrcweir public: 68*cdf0e10cSrcweir //######################################################## 69*cdf0e10cSrcweir xml_parser(const XML_Char* EncodingName = 0); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir //######################################################## 72*cdf0e10cSrcweir ~xml_parser(); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir //######################################################## 75*cdf0e10cSrcweir /** Parse a XML data stream 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir @param pXmlData 78*cdf0e10cSrcweir Pointer to a buffer containing the xml data 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir @param Length 81*cdf0e10cSrcweir Length of the buffer containing the xml data 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir @param IsFinal 84*cdf0e10cSrcweir Indicates whether these are the last xml data 85*cdf0e10cSrcweir of an xml document to parse. For very large 86*cdf0e10cSrcweir xml documents it may be usefull to read and 87*cdf0e10cSrcweir parse the document partially. 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir @precond XmlData must not be null 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir @throws SaxException 92*cdf0e10cSrcweir If the used Sax parser returns an error. The SaxException 93*cdf0e10cSrcweir contains detailed information about the error. */ 94*cdf0e10cSrcweir void parse(const char* XmlData, size_t Length, bool IsFinal = true); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir //######################################################## 97*cdf0e10cSrcweir /** Set a document handler 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir @descr A document handler implements the interface i_xml_parser_event_handler. 100*cdf0e10cSrcweir The document handler receive notifications of various events 101*cdf0e10cSrcweir from the sax parser for instance "start_document". 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir The client is responsible for the life time management of 104*cdf0e10cSrcweir the given document handler, that means the document handler 105*cdf0e10cSrcweir instance must exist until a new one was set or until the parser 106*cdf0e10cSrcweir no longer exist. 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir @param SaxDocumentHandler 109*cdf0e10cSrcweir The new document handler, may be null if not interessted in 110*cdf0e10cSrcweir sax parser events. 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir @postcond currently used document handler == pSaxDocumentHandler */ 113*cdf0e10cSrcweir void set_document_handler(i_xml_parser_event_handler* event_handler); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir //######################################################## 116*cdf0e10cSrcweir /** Returns the currently used document handler or null if 117*cdf0e10cSrcweir no document handler was set before. */ 118*cdf0e10cSrcweir i_xml_parser_event_handler* get_document_handler() const; 119*cdf0e10cSrcweir private: 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir void init(); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir private: 124*cdf0e10cSrcweir i_xml_parser_event_handler* document_handler_; 125*cdf0e10cSrcweir XML_Parser xml_parser_; 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir // prevent copy and assignment 128*cdf0e10cSrcweir private: 129*cdf0e10cSrcweir xml_parser(const xml_parser&); 130*cdf0e10cSrcweir xml_parser& operator=(const xml_parser&); 131*cdf0e10cSrcweir }; 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir #endif 134*cdf0e10cSrcweir 135