1*8d192041SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*8d192041SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*8d192041SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*8d192041SAndrew Rist  * distributed with this work for additional information
6*8d192041SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*8d192041SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*8d192041SAndrew Rist  * "License"); you may not use this file except in compliance
9*8d192041SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*8d192041SAndrew Rist  *
11*8d192041SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*8d192041SAndrew Rist  *
13*8d192041SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*8d192041SAndrew Rist  * software distributed under the License is distributed on an
15*8d192041SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*8d192041SAndrew Rist  * KIND, either express or implied.  See the License for the
17*8d192041SAndrew Rist  * specific language governing permissions and limitations
18*8d192041SAndrew Rist  * under the License.
19*8d192041SAndrew Rist  *
20*8d192041SAndrew Rist  *************************************************************/
21*8d192041SAndrew Rist 
22*8d192041SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _SAX_FASTPARSER_HXX_
25cdf0e10cSrcweir #define _SAX_FASTPARSER_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir #include <stack>
29cdf0e10cSrcweir #include <hash_map>
30cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
31cdf0e10cSrcweir #include <rtl/ref.hxx>
32cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastParser.hpp>
33cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
36cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <expat.h>
39cdf0e10cSrcweir #include "xml2utf.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <sax/fastattribs.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #define PARSER_IMPLEMENTATION_NAME "com.sun.star.comp.extensions.xml.sax.FastParser"
44cdf0e10cSrcweir #define PARSER_SERVICE_NAME        "com.sun.star.xml.sax.FastParser"
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace sax_fastparser {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class FastLocatorImpl;
49cdf0e10cSrcweir struct NamespaceDefine;
50cdf0e10cSrcweir struct SaxContextImpl;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir typedef ::boost::shared_ptr< SaxContextImpl > SaxContextImplPtr;
53cdf0e10cSrcweir typedef ::boost::shared_ptr< NamespaceDefine > NamespaceDefineRef;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir typedef ::std::hash_map< ::rtl::OUString, sal_Int32,
56cdf0e10cSrcweir         ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > NamespaceMap;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // --------------------------------------------------------------------
59cdf0e10cSrcweir 
60cdf0e10cSrcweir struct ParserData
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler > mxDocumentHandler;
63cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >    mxTokenHandler;
64cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler >        mxErrorHandler;
65cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver >      mxEntityResolver;
66cdf0e10cSrcweir     ::com::sun::star::lang::Locale          maLocale;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     ParserData();
69cdf0e10cSrcweir     ~ParserData();
70cdf0e10cSrcweir };
71cdf0e10cSrcweir 
72cdf0e10cSrcweir // --------------------------------------------------------------------
73cdf0e10cSrcweir 
74cdf0e10cSrcweir // Entity binds all information needed for a single file
75cdf0e10cSrcweir struct Entity : public ParserData
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     ::com::sun::star::xml::sax::InputSource maStructSource;
78cdf0e10cSrcweir     XML_Parser                              mpParser;
79cdf0e10cSrcweir     ::sax_expatwrap::XMLFile2UTFConverter   maConverter;
80cdf0e10cSrcweir     ::rtl::Reference< FastAttributeList >   mxAttributes;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // Exceptions cannot be thrown through the C-XmlParser (possible resource leaks),
83cdf0e10cSrcweir     // therefore the exception must be saved somewhere.
84cdf0e10cSrcweir     ::com::sun::star::uno::Any              maSavedException;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     ::std::stack< SaxContextImplPtr >       maContextStack;
87cdf0e10cSrcweir     ::std::vector< NamespaceDefineRef >     maNamespaceDefines;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     explicit Entity( const ParserData& rData );
90cdf0e10cSrcweir     ~Entity();
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir // --------------------------------------------------------------------
94cdf0e10cSrcweir 
95cdf0e10cSrcweir // This class implements the external Parser interface
96cdf0e10cSrcweir class FastSaxParser : public ::cppu::WeakImplHelper2< ::com::sun::star::xml::sax::XFastParser, ::com::sun::star::lang::XServiceInfo >
97cdf0e10cSrcweir {
98cdf0e10cSrcweir public:
99cdf0e10cSrcweir     FastSaxParser();
100cdf0e10cSrcweir     virtual ~FastSaxParser();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     // The implementation details
103cdf0e10cSrcweir     static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static(void);
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     // XFastParser
106cdf0e10cSrcweir     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);
107cdf0e10cSrcweir     virtual void SAL_CALL setFastDocumentHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastDocumentHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
108cdf0e10cSrcweir     virtual void SAL_CALL setTokenHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
109cdf0e10cSrcweir     virtual void SAL_CALL registerNamespace( const ::rtl::OUString& NamespaceURL, sal_Int32 NamespaceToken ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException);
110cdf0e10cSrcweir     virtual void SAL_CALL setErrorHandler( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XErrorHandler >& Handler ) throw (::com::sun::star::uno::RuntimeException);
111cdf0e10cSrcweir     virtual void SAL_CALL setEntityResolver( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XEntityResolver >& Resolver ) throw (::com::sun::star::uno::RuntimeException);
112cdf0e10cSrcweir     virtual void SAL_CALL setLocale( const ::com::sun::star::lang::Locale& rLocale ) throw (::com::sun::star::uno::RuntimeException);
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // XServiceInfo
115cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);
116cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (::com::sun::star::uno::RuntimeException);
117cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (::com::sun::star::uno::RuntimeException);
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     // called by the C callbacks of the expat parser
120cdf0e10cSrcweir     void callbackStartElement( const XML_Char* name, const XML_Char** atts );
121cdf0e10cSrcweir     void callbackEndElement( const XML_Char* name );
122cdf0e10cSrcweir     void callbackCharacters( const XML_Char* s, int nLen );
123cdf0e10cSrcweir     int callbackExternalEntityRef( XML_Parser parser, const XML_Char *openEntityNames, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId);
124cdf0e10cSrcweir 
pushEntity(const Entity & rEntity)125cdf0e10cSrcweir     inline void pushEntity( const Entity& rEntity ) { maEntities.push( rEntity ); }
popEntity()126cdf0e10cSrcweir     inline void popEntity()                         { maEntities.pop(); }
getEntity()127cdf0e10cSrcweir     Entity& getEntity()                             { return maEntities.top(); }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir private:
130cdf0e10cSrcweir     void parse();
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     sal_Int32 GetToken( const ::rtl::OString& rToken );
133cdf0e10cSrcweir     sal_Int32 GetToken( const sal_Char* pToken, sal_Int32 nTokenLen = 0 );
134cdf0e10cSrcweir     sal_Int32 GetTokenWithPrefix( const ::rtl::OString& rPrefix, const ::rtl::OString& rName ) throw (::com::sun::star::xml::sax::SAXException);
135cdf0e10cSrcweir     sal_Int32 GetTokenWithPrefix( const sal_Char*pPrefix, int nPrefixLen, const sal_Char* pName, int nNameLen ) throw (::com::sun::star::xml::sax::SAXException);
136cdf0e10cSrcweir     ::rtl::OUString GetNamespaceURL( const ::rtl::OString& rPrefix ) throw (::com::sun::star::xml::sax::SAXException);
137cdf0e10cSrcweir     ::rtl::OUString GetNamespaceURL( const sal_Char*pPrefix, int nPrefixLen ) throw (::com::sun::star::xml::sax::SAXException);
138cdf0e10cSrcweir     sal_Int32 GetNamespaceToken( const ::rtl::OUString& rNamespaceURL );
139cdf0e10cSrcweir     sal_Int32 GetTokenWithNamespaceURL( const ::rtl::OUString& rNamespaceURL, const sal_Char* pName, int nNameLen );
140cdf0e10cSrcweir     void DefineNamespace( const ::rtl::OString& rPrefix, const sal_Char* pNamespaceURL );
141cdf0e10cSrcweir     sal_Int32 CreateCustomToken( const sal_Char* pToken, int len = 0 );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     void pushContext();
144cdf0e10cSrcweir     void popContext();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     void splitName( const XML_Char *pwName, const XML_Char *&rpPrefix, sal_Int32 &rPrefixLen, const XML_Char *&rpName, sal_Int32 &rNameLen );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir private:
149cdf0e10cSrcweir     ::osl::Mutex maMutex;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     ::rtl::Reference< FastLocatorImpl >     mxDocumentLocator;
152cdf0e10cSrcweir     NamespaceMap                            maNamespaceMap;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     ParserData maData;                      /// Cached parser configuration for next call of parseStream().
155cdf0e10cSrcweir     ::std::stack< Entity > maEntities;      /// Entity stack for each call of parseStream().
156cdf0e10cSrcweir };
157cdf0e10cSrcweir 
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir #endif // _SAX_FASTPARSER_HXX_
161