merge the formfield patch from ooo-build
[ooovba.git] / sax / source / fastparser / fastparser.hxx
blob07cb6afac77d1f28a5a03877ac8ceef9cde9e925
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile$
10 * $Revision$
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _SAX_FASTPARSER_HXX_
32 #define _SAX_FASTPARSER_HXX_
34 #include <vector>
35 #include <stack>
36 #include <hash_map>
37 #include <boost/shared_ptr.hpp>
38 #include <rtl/ref.hxx>
39 #include <com/sun/star/xml/sax/XFastParser.hpp>
40 #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
41 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <cppuhelper/implbase2.hxx>
45 #ifdef SYSTEM_EXPAT
46 #include <expat.h>
47 #else
48 #include "expat/xmlparse.h"
49 #endif
50 #include "xml2utf.hxx"
52 #include <sax/fastattribs.hxx>
54 #define PARSER_IMPLEMENTATION_NAME "com.sun.star.comp.extensions.xml.sax.FastParser"
55 #define PARSER_SERVICE_NAME "com.sun.star.xml.sax.FastParser"
57 namespace sax_fastparser
60 class FastLocatorImpl;
61 struct NamespaceDefine;
62 struct SaxContextImpl;
63 typedef boost::shared_ptr< SaxContextImpl > SaxContextImplPtr;
64 typedef ::std::hash_map< ::rtl::OUString, sal_Int32,
65 ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString >
66 > NamespaceMap;
68 // --------------------------------------------------------------------
70 // Entity binds all information neede for a single file
71 struct Entity
73 ::com::sun::star::xml::sax::InputSource maStructSource;
74 XML_Parser mpParser;
75 sax_expatwrap::XMLFile2UTFConverter maConverter;
78 // --------------------------------------------------------------------
80 // This class implements the external Parser interface
81 class FastSaxParser : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastParser, ::com::sun::star::lang::XServiceInfo >
83 public:
84 FastSaxParser();
85 ~FastSaxParser();
87 // The implementation details
88 static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(void);
90 // XFastParser
91 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);
92 virtual void SAL_CALL setFastDocumentHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
93 virtual void SAL_CALL setTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
94 virtual void SAL_CALL registerNamespace( const ::rtl::OUString& NamespaceURL, sal_Int32 NamespaceToken ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
95 virtual void SAL_CALL setErrorHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
96 virtual void SAL_CALL setEntityResolver( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver >& Resolver ) throw (::com::sun::star::uno::RuntimeException);
97 virtual void SAL_CALL setLocale( const ::com::sun::star::lang::Locale& rLocale ) throw (::com::sun::star::uno::RuntimeException);
99 // XServiceInfo
100 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (::com::sun::star::uno::RuntimeException);
101 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
102 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException);
104 public:
105 // the C-Callbacks for the expat parser
106 void static callbackStartElement(void *userData, const XML_Char *name , const XML_Char **atts);
107 void static callbackEndElement(void *userData, const XML_Char *name);
108 void static callbackCharacters( void *userData , const XML_Char *s , int nLen );
109 int static callbackExternalEntityRef( XML_Parser parser, const XML_Char *openEntityNames, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId);
111 public:
112 void pushEntity( const struct Entity &entity ) { vecEntity.push_back( entity ); }
113 void popEntity() { vecEntity.pop_back( ); }
114 struct Entity &getEntity() { return vecEntity.back(); }
116 private:
117 void parse();
119 sal_Int32 GetToken( const ::rtl::OString& rToken );
120 sal_Int32 GetToken( const sal_Char* pToken, sal_Int32 nTokenLen = 0 );
121 sal_Int32 GetTokenWithPrefix( const ::rtl::OString& rPrefix, const ::rtl::OString& rName ) throw (::com::sun::star::xml::sax::SAXException);
122 sal_Int32 GetTokenWithPrefix( const sal_Char*pPrefix, int nPrefixLen, const sal_Char* pName, int nNameLen ) throw (::com::sun::star::xml::sax::SAXException);
123 ::rtl::OUString GetNamespaceURL( const ::rtl::OString& rPrefix ) throw (::com::sun::star::xml::sax::SAXException);
124 ::rtl::OUString GetNamespaceURL( const sal_Char*pPrefix, int nPrefixLen ) throw (::com::sun::star::xml::sax::SAXException);
125 sal_Int32 GetNamespaceToken( const ::rtl::OUString& rNamespaceURL );
126 sal_Int32 GetTokenWithNamespaceURL( const ::rtl::OUString& rNamespaceURL, const sal_Char* pName, int nNameLen );
127 void DefineNamespace( const ::rtl::OString& rPrefix, const sal_Char* pNamespaceURL );
128 sal_Int32 CreateCustomToken( const sal_Char* pToken, int len = 0 );
130 void pushContext();
131 void popContext();
133 void splitName( const XML_Char *pwName, const XML_Char *&rpPrefix, sal_Int32 &rPrefixLen, const XML_Char *&rpName, sal_Int32 &rNameLen );
135 private:
136 ::osl::Mutex maMutex;
138 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler > mxDocumentHandler;
139 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler;
140 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler > mxErrorHandler;
141 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver > mxEntityResolver;
143 rtl::Reference < FastLocatorImpl > mxDocumentLocator;
144 rtl::Reference < FastAttributeList > mxAttributes;
146 // External entity stack
147 std::vector< struct Entity > vecEntity;
149 // Exception cannot be thrown through the C-XmlParser (possible resource leaks),
150 // therefor the maSavedException must be saved somewhere.
151 ::com::sun::star::uno::Any maSavedException;
152 sal_Bool mbExceptionWasThrown;
154 ::com::sun::star::lang::Locale maLocale;
156 std::stack< SaxContextImplPtr > maContextStack;
157 std::vector< boost::shared_ptr< NamespaceDefine > > maNamespaceDefines;
158 NamespaceMap maNamespaceMap;
163 #endif // _SAX_FASTPARSER_HXX_