xref: /trunk/main/sax/inc/sax/parser/saxparser.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 _SAX_SAXPARSER_HXX_
29 #define _SAX_SAXPARSER_HXX_
30 
31 #include "sax/dllapi.h"
32 #include <com/sun/star/xml/sax/InputSource.hpp>
33 #include <com/sun/star/xml/sax/SAXException.hpp>
34 #include <rtl/ref.hxx>
35 
36 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
37 #include <boost/shared_ptr.hpp>
38 #endif
39 
40 #include <map>
41 #include <memory>
42 #include "sax/tools/saxobject.hxx"
43 #include "sax/tools/attributemap.hxx"
44 
45 #include <boost/scoped_ptr.hpp>
46 
47 namespace sax
48 {
49 
50 // --------------------------------------------------------------------
51 
52 class SaxParser;
53 class SaxContext;
54 
55 typedef rtl::Reference< SaxParser > SaxParserRef;
56 typedef rtl::Reference< SaxContext > SaxContextRef;
57 
58 /** base class for each implementation that handles all sax calls for an element */
59 class SAX_DLLPUBLIC SaxContext : public SaxObject
60 {
61 public:
62 	SaxContext( const SaxParserRef& xParser );
63 	virtual ~SaxContext();
64 
65 	/** receives notification of the beginning of an element .
66 	 */
67 	virtual void StartElement( sal_uInt32 aElementToken, const AttributeMap& rAttributes );
68 
69 	/** receives notification of character data.
70 	 */
71 	virtual void Characters( const sal_Char *pCharacters, sal_uInt32 nLength );
72 
73 	/** receives notification of the end of an element.
74 	 */
75 	virtual void EndElement( sal_uInt32 aElementToken );
76 
77 	/** is called by the SaxParser for each child element inside this instances element */
78 	virtual SaxContextRef CreateContext( sal_uInt32 aElementToken, const AttributeMap& rAttributes );
79 
80 	const SaxParserRef& getParser() const { return mxParser; }
81 private:
82 	SaxParserRef	mxParser;
83 };
84 
85 // --------------------------------------------------------------------
86 
87 class SaxParserImpl;
88 
89 /** base class for loading a single xml stream. Derived classes must call
90 	parseStream to start parsing and are notified through virtual methods
91 	for sax events. */
92 class SAX_DLLPUBLIC SaxParser : public SaxObject
93 {
94 public:
95 	SaxParser();
96 	virtual ~SaxParser();
97 
98 	/** returns the unicode representation of a token from the xml stream
99 		that was unknown to the SaxTokenMap from the derived class. */
100 	rtl::OUString GetCustomToken( sal_uInt32 nToken );
101 
102 	/** returns the unicode representation of a namespace from the xml stream
103 		that was unknown to the SaxTokenMap from the derived class. */
104 	rtl::OUString GetCustomNamespace( sal_uInt32 nToken );
105 
106 	/** returns the system id of the currently parsed stream */
107 	const rtl::OUString& GetSystemId() const;
108 
109 	/** starts parsing of the source xml stream provided in the given sax InputSource */
110 	virtual void parseStream( const ::com::sun::star::xml::sax::InputSource& rInputSource ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
111 
112 	/** is called once when parsing of the xml stream starts */
113 	virtual void StartDocument();
114 
115 	/** is called once for each element in the xml stream.
116 		Default implementation calls StartElement() on the topmost contex. */
117 	virtual void StartElement( sal_uInt32 aElementToken, const AttributeMap& rAttributes );
118 
119 	/** is called for characters betwen elements in the xml stream.
120 		Default implementation calls Characters() on the topmost contex.
121 		@param pCharacters The characters in utf-8 encoding
122 		@param nLength the size in bytes of the utf-8 string
123 	*/
124 	virtual void Characters( const sal_Char *pCharacters, sal_uInt32 nLength );
125 
126 	/** is called once at the end of each element in the xml stream.
127 		Default implementation calls EndElement() on the topmost contex. */
128 	virtual void EndElement( sal_uInt32 aElementToken );
129 
130 	/** is called once after parsing the xml stream is finished */
131 	virtual void EndDocument();
132 
133 	/** is called once for each element in the xml stream and before StartElement() is called.
134 		Default implementation calls CreateContext at the topmost context.
135 		Returned contexts are pushed on a stack and removed after the corresponding EndElement call. */
136 	virtual SaxContextRef CreateContext( sal_uInt32 aElementToken, const AttributeMap& rAttributes );
137 
138 	/** must be implemented from derived classes. The returned SaxTokenMap is used to convert
139 		element names and attribute names and values to tokens. */
140 	virtual const SaxTokenMapRef& getTokenMap() = 0;
141 
142 private:
143 	void AddNamespace( sal_uInt32 nNamespaceId, sal_uInt32 nNamespaceURIToken );
144 
145 	boost::scoped_ptr< SaxParserImpl > mpImpl;
146 };
147 
148 }
149 
150 #endif // _SAX_SAXPARSER_HXX_
151