Bump for 3.6-28
[LibreOffice.git] / sax / source / fastparser / fastparser.hxx
blobaac4e1dbbb6ac163910bc927ba0aba552d63d3ea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifndef _SAX_FASTPARSER_HXX_
30 #define _SAX_FASTPARSER_HXX_
32 #include <vector>
33 #include <stack>
34 #include <boost/unordered_map.hpp>
35 #include <boost/shared_ptr.hpp>
36 #include <rtl/ref.hxx>
37 #include <com/sun/star/xml/sax/XFastParser.hpp>
38 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
39 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <cppuhelper/implbase2.hxx>
43 #include <expat.h>
44 #include "xml2utf.hxx"
46 #include <sax/fastattribs.hxx>
48 #define PARSER_IMPLEMENTATION_NAME "com.sun.star.comp.extensions.xml.sax.FastParser"
49 #define PARSER_SERVICE_NAME "com.sun.star.xml.sax.FastParser"
51 namespace sax_fastparser {
53 class FastLocatorImpl;
54 struct NamespaceDefine;
55 struct SaxContextImpl;
57 typedef ::boost::shared_ptr< SaxContextImpl > SaxContextImplPtr;
58 typedef ::boost::shared_ptr< NamespaceDefine > NamespaceDefineRef;
60 typedef ::boost::unordered_map< ::rtl::OUString, sal_Int32,
61 ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > NamespaceMap;
63 // --------------------------------------------------------------------
65 struct ParserData
67 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler > mxDocumentHandler;
68 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler;
69 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler > mxErrorHandler;
70 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver > mxEntityResolver;
71 ::com::sun::star::lang::Locale maLocale;
73 ParserData();
74 ~ParserData();
77 // --------------------------------------------------------------------
79 // Entity binds all information needed for a single file
80 struct Entity : public ParserData
82 ::com::sun::star::xml::sax::InputSource maStructSource;
83 XML_Parser mpParser;
84 ::sax_expatwrap::XMLFile2UTFConverter maConverter;
85 ::rtl::Reference< FastAttributeList > mxAttributes;
87 // Exceptions cannot be thrown through the C-XmlParser (possible resource leaks),
88 // therefore the exception must be saved somewhere.
89 ::com::sun::star::uno::Any maSavedException;
91 ::std::stack< SaxContextImplPtr > maContextStack;
92 ::std::vector< NamespaceDefineRef > maNamespaceDefines;
94 explicit Entity( const ParserData& rData );
95 ~Entity();
98 // --------------------------------------------------------------------
100 // This class implements the external Parser interface
101 class FastSaxParser : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastParser, ::com::sun::star::lang::XServiceInfo >
103 public:
104 FastSaxParser();
105 virtual ~FastSaxParser();
107 // The implementation details
108 static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(void);
110 // XFastParser
111 virtual void SAL_CALL parseStream( const ::com::sun::star::xml::sax::InputSource& aInputSource ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
112 virtual void SAL_CALL setFastDocumentHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
113 virtual void SAL_CALL setTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
114 virtual void SAL_CALL registerNamespace( const ::rtl::OUString& NamespaceURL, sal_Int32 NamespaceToken ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
115 virtual ::rtl::OUString SAL_CALL getNamespaceURL( const ::rtl::OUString& rPrefix ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
116 virtual void SAL_CALL setErrorHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
117 virtual void SAL_CALL setEntityResolver( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver >& Resolver ) throw (::com::sun::star::uno::RuntimeException);
118 virtual void SAL_CALL setLocale( const ::com::sun::star::lang::Locale& rLocale ) throw (::com::sun::star::uno::RuntimeException);
120 // XServiceInfo
121 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException);
122 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
123 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException);
125 // called by the C callbacks of the expat parser
126 void callbackStartElement( const XML_Char* name, const XML_Char** atts );
127 void callbackEndElement( const XML_Char* name );
128 void callbackCharacters( const XML_Char* s, int nLen );
129 int callbackExternalEntityRef( XML_Parser parser, const XML_Char *openEntityNames, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId);
130 void callbackEntityDecl(const XML_Char *entityName, int is_parameter_entity,
131 const XML_Char *value, int value_length, const XML_Char *base,
132 const XML_Char *systemId, const XML_Char *publicId,
133 const XML_Char *notationName);
135 inline void pushEntity( const Entity& rEntity ) { maEntities.push( rEntity ); }
136 inline void popEntity() { maEntities.pop(); }
137 Entity& getEntity() { return maEntities.top(); }
139 private:
140 void parse();
142 sal_Int32 GetToken( const ::rtl::OString& rToken );
143 sal_Int32 GetToken( const sal_Char* pToken, sal_Int32 nTokenLen = 0 );
144 sal_Int32 GetTokenWithPrefix( const ::rtl::OString& rPrefix, const ::rtl::OString& rName ) throw (::com::sun::star::xml::sax::SAXException);
145 sal_Int32 GetTokenWithPrefix( const sal_Char*pPrefix, int nPrefixLen, const sal_Char* pName, int nNameLen ) throw (::com::sun::star::xml::sax::SAXException);
146 ::rtl::OUString GetNamespaceURL( const ::rtl::OString& rPrefix ) throw (::com::sun::star::xml::sax::SAXException);
147 ::rtl::OUString GetNamespaceURL( const sal_Char*pPrefix, int nPrefixLen ) throw (::com::sun::star::xml::sax::SAXException);
148 sal_Int32 GetNamespaceToken( const ::rtl::OUString& rNamespaceURL );
149 sal_Int32 GetTokenWithNamespaceURL( const ::rtl::OUString& rNamespaceURL, const sal_Char* pName, int nNameLen );
150 void DefineNamespace( const ::rtl::OString& rPrefix, const sal_Char* pNamespaceURL );
151 sal_Int32 CreateCustomToken( const sal_Char* pToken, int len = 0 );
153 void pushContext();
154 void popContext();
156 void splitName( const XML_Char *pwName, const XML_Char *&rpPrefix, sal_Int32 &rPrefixLen, const XML_Char *&rpName, sal_Int32 &rNameLen );
158 private:
159 ::osl::Mutex maMutex;
161 ::rtl::Reference< FastLocatorImpl > mxDocumentLocator;
162 NamespaceMap maNamespaceMap;
164 ParserData maData; /// Cached parser configuration for next call of parseStream().
165 ::std::stack< Entity > maEntities; /// Entity stack for each call of parseStream().
170 #endif // _SAX_FASTPARSER_HXX_
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */