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