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