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