1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #include <stdlib.h> 28*cdf0e10cSrcweir #include <string.h> 29*cdf0e10cSrcweir #include <sal/alloca.h> 30*cdf0e10cSrcweir #include <vector> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <osl/diagnose.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 42*cdf0e10cSrcweir #include <cppuhelper/weak.hxx> 43*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 44*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir #include <expat.h> 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using namespace ::rtl; 49*cdf0e10cSrcweir using namespace ::std; 50*cdf0e10cSrcweir using namespace ::osl; 51*cdf0e10cSrcweir using namespace ::cppu; 52*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 53*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 54*cdf0e10cSrcweir using namespace ::com::sun::star::registry; 55*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax; 56*cdf0e10cSrcweir using namespace ::com::sun::star::util; 57*cdf0e10cSrcweir using namespace ::com::sun::star::io; 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir #include "factory.hxx" 60*cdf0e10cSrcweir #include "attrlistimpl.hxx" 61*cdf0e10cSrcweir #include "xml2utf.hxx" 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir namespace sax_expatwrap { 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // Useful macros for correct String conversion depending on the choosen expat-mode 66*cdf0e10cSrcweir #ifdef XML_UNICODE 67*cdf0e10cSrcweir OUString XmlNChar2OUString( const XML_Char *p , int nLen ) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir if( p ) { 70*cdf0e10cSrcweir if( sizeof( sal_Unicode ) == sizeof( XML_Char ) ) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir return OUString( (sal_Unicode*)p,nLen); 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir else 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir sal_Unicode *pWchar = (sal_Unicode *)alloca( sizeof( sal_Unicode ) * nLen ); 77*cdf0e10cSrcweir for( int n = 0 ; n < nLen ; n++ ) { 78*cdf0e10cSrcweir pWchar[n] = (sal_Unicode) p[n]; 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir return OUString( pWchar , nLen ); 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir else { 84*cdf0e10cSrcweir return OUString(); 85*cdf0e10cSrcweir } 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir OUString XmlChar2OUString( const XML_Char *p ) 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir if( p ) { 91*cdf0e10cSrcweir int nLen; 92*cdf0e10cSrcweir for( nLen = 0 ; p[nLen] ; nLen ++ ) 93*cdf0e10cSrcweir ; 94*cdf0e10cSrcweir return XmlNChar2OUString( p , nLen ); 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir else return OUString(); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir #define XML_CHAR_TO_OUSTRING(x) XmlChar2OUString(x) 101*cdf0e10cSrcweir #define XML_CHAR_N_TO_USTRING(x,n) XmlNChar2OUString(x,n) 102*cdf0e10cSrcweir #else 103*cdf0e10cSrcweir #define XML_CHAR_TO_OUSTRING(x) OUString(x , strlen( x ), RTL_TEXTENCODING_UTF8) 104*cdf0e10cSrcweir #define XML_CHAR_N_TO_USTRING(x,n) OUString(x,n, RTL_TEXTENCODING_UTF8 ) 105*cdf0e10cSrcweir #endif 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir /* 109*cdf0e10cSrcweir * The following macro encapsulates any call to an event handler. 110*cdf0e10cSrcweir * It ensures, that exceptions thrown by the event handler are 111*cdf0e10cSrcweir * treated properly. 112*cdf0e10cSrcweir */ 113*cdf0e10cSrcweir #define CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS(pThis,call) \ 114*cdf0e10cSrcweir if( ! pThis->bExceptionWasThrown ) { \ 115*cdf0e10cSrcweir try {\ 116*cdf0e10cSrcweir pThis->call;\ 117*cdf0e10cSrcweir }\ 118*cdf0e10cSrcweir catch( SAXParseException &e ) {\ 119*cdf0e10cSrcweir pThis->callErrorHandler( pThis , e );\ 120*cdf0e10cSrcweir }\ 121*cdf0e10cSrcweir catch( SAXException &e ) {\ 122*cdf0e10cSrcweir pThis->callErrorHandler( pThis , SAXParseException(\ 123*cdf0e10cSrcweir e.Message, \ 124*cdf0e10cSrcweir e.Context, \ 125*cdf0e10cSrcweir e.WrappedException,\ 126*cdf0e10cSrcweir pThis->rDocumentLocator->getPublicId(),\ 127*cdf0e10cSrcweir pThis->rDocumentLocator->getSystemId(),\ 128*cdf0e10cSrcweir pThis->rDocumentLocator->getLineNumber(),\ 129*cdf0e10cSrcweir pThis->rDocumentLocator->getColumnNumber()\ 130*cdf0e10cSrcweir ) );\ 131*cdf0e10cSrcweir }\ 132*cdf0e10cSrcweir catch( com::sun::star::uno::RuntimeException &e ) {\ 133*cdf0e10cSrcweir pThis->bExceptionWasThrown = sal_True; \ 134*cdf0e10cSrcweir pThis->bRTExceptionWasThrown = sal_True; \ 135*cdf0e10cSrcweir pImpl->rtexception = e; \ 136*cdf0e10cSrcweir }\ 137*cdf0e10cSrcweir }\ 138*cdf0e10cSrcweir ((void)0) 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.extensions.xml.sax.ParserExpat" 141*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.xml.sax.Parser" 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir class SaxExpatParser_Impl; 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // This class implements the external Parser interface 147*cdf0e10cSrcweir class SaxExpatParser : 148*cdf0e10cSrcweir public WeakImplHelper2< 149*cdf0e10cSrcweir XParser, 150*cdf0e10cSrcweir XServiceInfo 151*cdf0e10cSrcweir > 152*cdf0e10cSrcweir { 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir public: 155*cdf0e10cSrcweir SaxExpatParser(); 156*cdf0e10cSrcweir ~SaxExpatParser(); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir public: 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir // The implementation details 161*cdf0e10cSrcweir static Sequence< OUString > getSupportedServiceNames_Static(void) throw (); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir public: 164*cdf0e10cSrcweir // The SAX-Parser-Interface 165*cdf0e10cSrcweir virtual void SAL_CALL parseStream( const InputSource& structSource) 166*cdf0e10cSrcweir throw ( SAXException, 167*cdf0e10cSrcweir IOException, 168*cdf0e10cSrcweir RuntimeException); 169*cdf0e10cSrcweir virtual void SAL_CALL setDocumentHandler(const Reference< XDocumentHandler > & xHandler) 170*cdf0e10cSrcweir throw (RuntimeException); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir virtual void SAL_CALL setErrorHandler(const Reference< XErrorHandler > & xHandler) 173*cdf0e10cSrcweir throw (RuntimeException); 174*cdf0e10cSrcweir virtual void SAL_CALL setDTDHandler(const Reference < XDTDHandler > & xHandler) 175*cdf0e10cSrcweir throw (RuntimeException); 176*cdf0e10cSrcweir virtual void SAL_CALL setEntityResolver(const Reference< XEntityResolver >& xResolver) 177*cdf0e10cSrcweir throw (RuntimeException); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir virtual void SAL_CALL setLocale( const Locale &locale ) throw (RuntimeException); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir public: // XServiceInfo 182*cdf0e10cSrcweir OUString SAL_CALL getImplementationName() throw (); 183*cdf0e10cSrcweir Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw (); 184*cdf0e10cSrcweir sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (); 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir private: 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir SaxExpatParser_Impl *m_pImpl; 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir }; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir //-------------------------------------- 193*cdf0e10cSrcweir // the extern interface 194*cdf0e10cSrcweir //--------------------------------------- 195*cdf0e10cSrcweir Reference< XInterface > SAL_CALL SaxExpatParser_CreateInstance( 196*cdf0e10cSrcweir const Reference< XMultiServiceFactory > & ) throw(Exception) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir SaxExpatParser *p = new SaxExpatParser; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir return Reference< XInterface > ( (OWeakObject * ) p ); 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir Sequence< OUString > SaxExpatParser::getSupportedServiceNames_Static(void) throw () 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir Sequence<OUString> aRet(1); 208*cdf0e10cSrcweir aRet.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME) ); 209*cdf0e10cSrcweir return aRet; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir //--------------------------------------------- 214*cdf0e10cSrcweir // the implementation part 215*cdf0e10cSrcweir //--------------------------------------------- 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir // Entity binds all information neede for a single file 219*cdf0e10cSrcweir struct Entity 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir InputSource structSource; 222*cdf0e10cSrcweir XML_Parser pParser; 223*cdf0e10cSrcweir XMLFile2UTFConverter converter; 224*cdf0e10cSrcweir }; 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir class SaxExpatParser_Impl 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir public: // module scope 230*cdf0e10cSrcweir Mutex aMutex; 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir Reference< XDocumentHandler > rDocumentHandler; 233*cdf0e10cSrcweir Reference< XExtendedDocumentHandler > rExtendedDocumentHandler; 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir Reference< XErrorHandler > rErrorHandler; 236*cdf0e10cSrcweir Reference< XDTDHandler > rDTDHandler; 237*cdf0e10cSrcweir Reference< XEntityResolver > rEntityResolver; 238*cdf0e10cSrcweir Reference < XLocator > rDocumentLocator; 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir Reference < XAttributeList > rAttrList; 242*cdf0e10cSrcweir AttributeList *pAttrList; 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // External entity stack 245*cdf0e10cSrcweir vector<struct Entity> vecEntity; 246*cdf0e10cSrcweir void pushEntity( const struct Entity &entity ) 247*cdf0e10cSrcweir { vecEntity.push_back( entity ); } 248*cdf0e10cSrcweir void popEntity() 249*cdf0e10cSrcweir { vecEntity.pop_back( ); } 250*cdf0e10cSrcweir struct Entity &getEntity() 251*cdf0e10cSrcweir { return vecEntity.back(); } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir // Exception cannot be thrown through the C-XmlParser (possible resource leaks), 255*cdf0e10cSrcweir // therefor the exception must be saved somewhere. 256*cdf0e10cSrcweir SAXParseException exception; 257*cdf0e10cSrcweir RuntimeException rtexception; 258*cdf0e10cSrcweir sal_Bool bExceptionWasThrown; 259*cdf0e10cSrcweir sal_Bool bRTExceptionWasThrown; 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir Locale locale; 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir public: 264*cdf0e10cSrcweir // the C-Callbacks for the expat parser 265*cdf0e10cSrcweir void static callbackStartElement(void *userData, const XML_Char *name , const XML_Char **atts); 266*cdf0e10cSrcweir void static callbackEndElement(void *userData, const XML_Char *name); 267*cdf0e10cSrcweir void static callbackCharacters( void *userData , const XML_Char *s , int nLen ); 268*cdf0e10cSrcweir void static callbackProcessingInstruction( void *userData , 269*cdf0e10cSrcweir const XML_Char *sTarget , 270*cdf0e10cSrcweir const XML_Char *sData ); 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir void static callbackUnparsedEntityDecl( void *userData , 273*cdf0e10cSrcweir const XML_Char *entityName, 274*cdf0e10cSrcweir const XML_Char *base, 275*cdf0e10cSrcweir const XML_Char *systemId, 276*cdf0e10cSrcweir const XML_Char *publicId, 277*cdf0e10cSrcweir const XML_Char *notationName); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir void static callbackNotationDecl( void *userData, 280*cdf0e10cSrcweir const XML_Char *notationName, 281*cdf0e10cSrcweir const XML_Char *base, 282*cdf0e10cSrcweir const XML_Char *systemId, 283*cdf0e10cSrcweir const XML_Char *publicId); 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir int static callbackExternalEntityRef( XML_Parser parser, 286*cdf0e10cSrcweir const XML_Char *openEntityNames, 287*cdf0e10cSrcweir const XML_Char *base, 288*cdf0e10cSrcweir const XML_Char *systemId, 289*cdf0e10cSrcweir const XML_Char *publicId); 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir int static callbackUnknownEncoding(void *encodingHandlerData, 292*cdf0e10cSrcweir const XML_Char *name, 293*cdf0e10cSrcweir XML_Encoding *info); 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir void static callbackDefault( void *userData, const XML_Char *s, int len); 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir void static callbackStartCDATA( void *userData ); 298*cdf0e10cSrcweir void static callbackEndCDATA( void *userData ); 299*cdf0e10cSrcweir void static callbackComment( void *userData , const XML_Char *s ); 300*cdf0e10cSrcweir void static callErrorHandler( SaxExpatParser_Impl *pImpl , const SAXParseException &e ); 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir public: 303*cdf0e10cSrcweir void parse(); 304*cdf0e10cSrcweir }; 305*cdf0e10cSrcweir 306*cdf0e10cSrcweir extern "C" 307*cdf0e10cSrcweir { 308*cdf0e10cSrcweir static void call_callbackStartElement(void *userData, const XML_Char *name , const XML_Char **atts) 309*cdf0e10cSrcweir { 310*cdf0e10cSrcweir SaxExpatParser_Impl::callbackStartElement(userData,name,atts); 311*cdf0e10cSrcweir } 312*cdf0e10cSrcweir static void call_callbackEndElement(void *userData, const XML_Char *name) 313*cdf0e10cSrcweir { 314*cdf0e10cSrcweir SaxExpatParser_Impl::callbackEndElement(userData,name); 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir static void call_callbackCharacters( void *userData , const XML_Char *s , int nLen ) 317*cdf0e10cSrcweir { 318*cdf0e10cSrcweir SaxExpatParser_Impl::callbackCharacters(userData,s,nLen); 319*cdf0e10cSrcweir } 320*cdf0e10cSrcweir static void call_callbackProcessingInstruction(void *userData,const XML_Char *sTarget,const XML_Char *sData ) 321*cdf0e10cSrcweir { 322*cdf0e10cSrcweir SaxExpatParser_Impl::callbackProcessingInstruction(userData,sTarget,sData ); 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir static void call_callbackUnparsedEntityDecl(void *userData , 325*cdf0e10cSrcweir const XML_Char *entityName, 326*cdf0e10cSrcweir const XML_Char *base, 327*cdf0e10cSrcweir const XML_Char *systemId, 328*cdf0e10cSrcweir const XML_Char *publicId, 329*cdf0e10cSrcweir const XML_Char *notationName) 330*cdf0e10cSrcweir { 331*cdf0e10cSrcweir SaxExpatParser_Impl::callbackUnparsedEntityDecl(userData,entityName,base,systemId,publicId,notationName); 332*cdf0e10cSrcweir } 333*cdf0e10cSrcweir static void call_callbackNotationDecl(void *userData, 334*cdf0e10cSrcweir const XML_Char *notationName, 335*cdf0e10cSrcweir const XML_Char *base, 336*cdf0e10cSrcweir const XML_Char *systemId, 337*cdf0e10cSrcweir const XML_Char *publicId) 338*cdf0e10cSrcweir { 339*cdf0e10cSrcweir SaxExpatParser_Impl::callbackNotationDecl(userData,notationName,base,systemId,publicId); 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir static int call_callbackExternalEntityRef(XML_Parser parser, 342*cdf0e10cSrcweir const XML_Char *openEntityNames, 343*cdf0e10cSrcweir const XML_Char *base, 344*cdf0e10cSrcweir const XML_Char *systemId, 345*cdf0e10cSrcweir const XML_Char *publicId) 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir return SaxExpatParser_Impl::callbackExternalEntityRef(parser,openEntityNames,base,systemId,publicId); 348*cdf0e10cSrcweir } 349*cdf0e10cSrcweir static int call_callbackUnknownEncoding(void *encodingHandlerData, 350*cdf0e10cSrcweir const XML_Char *name, 351*cdf0e10cSrcweir XML_Encoding *info) 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir return SaxExpatParser_Impl::callbackUnknownEncoding(encodingHandlerData,name,info); 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir static void call_callbackDefault( void *userData, const XML_Char *s, int len) 356*cdf0e10cSrcweir { 357*cdf0e10cSrcweir SaxExpatParser_Impl::callbackDefault(userData,s,len); 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir static void call_callbackStartCDATA( void *userData ) 360*cdf0e10cSrcweir { 361*cdf0e10cSrcweir SaxExpatParser_Impl::callbackStartCDATA(userData); 362*cdf0e10cSrcweir } 363*cdf0e10cSrcweir static void call_callbackEndCDATA( void *userData ) 364*cdf0e10cSrcweir { 365*cdf0e10cSrcweir SaxExpatParser_Impl::callbackEndCDATA(userData); 366*cdf0e10cSrcweir } 367*cdf0e10cSrcweir static void call_callbackComment( void *userData , const XML_Char *s ) 368*cdf0e10cSrcweir { 369*cdf0e10cSrcweir SaxExpatParser_Impl::callbackComment(userData,s); 370*cdf0e10cSrcweir } 371*cdf0e10cSrcweir } 372*cdf0e10cSrcweir 373*cdf0e10cSrcweir 374*cdf0e10cSrcweir //--------------------------------------------- 375*cdf0e10cSrcweir // LocatorImpl 376*cdf0e10cSrcweir //--------------------------------------------- 377*cdf0e10cSrcweir class LocatorImpl : 378*cdf0e10cSrcweir public WeakImplHelper2< XLocator, com::sun::star::io::XSeekable > 379*cdf0e10cSrcweir // should use a different interface for stream positions! 380*cdf0e10cSrcweir { 381*cdf0e10cSrcweir public: 382*cdf0e10cSrcweir LocatorImpl( SaxExpatParser_Impl *p ) 383*cdf0e10cSrcweir { 384*cdf0e10cSrcweir m_pParser = p; 385*cdf0e10cSrcweir } 386*cdf0e10cSrcweir 387*cdf0e10cSrcweir public: //XLocator 388*cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getColumnNumber(void) throw () 389*cdf0e10cSrcweir { 390*cdf0e10cSrcweir return XML_GetCurrentColumnNumber( m_pParser->getEntity().pParser ); 391*cdf0e10cSrcweir } 392*cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getLineNumber(void) throw () 393*cdf0e10cSrcweir { 394*cdf0e10cSrcweir return XML_GetCurrentLineNumber( m_pParser->getEntity().pParser ); 395*cdf0e10cSrcweir } 396*cdf0e10cSrcweir virtual OUString SAL_CALL getPublicId(void) throw () 397*cdf0e10cSrcweir { 398*cdf0e10cSrcweir return m_pParser->getEntity().structSource.sPublicId; 399*cdf0e10cSrcweir } 400*cdf0e10cSrcweir virtual OUString SAL_CALL getSystemId(void) throw () 401*cdf0e10cSrcweir { 402*cdf0e10cSrcweir return m_pParser->getEntity().structSource.sSystemId; 403*cdf0e10cSrcweir } 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir // XSeekable (only for getPosition) 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir virtual void SAL_CALL seek( sal_Int64 ) throw() 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir } 410*cdf0e10cSrcweir virtual sal_Int64 SAL_CALL getPosition() throw() 411*cdf0e10cSrcweir { 412*cdf0e10cSrcweir return XML_GetCurrentByteIndex( m_pParser->getEntity().pParser ); 413*cdf0e10cSrcweir } 414*cdf0e10cSrcweir virtual ::sal_Int64 SAL_CALL getLength() throw() 415*cdf0e10cSrcweir { 416*cdf0e10cSrcweir return 0; 417*cdf0e10cSrcweir } 418*cdf0e10cSrcweir 419*cdf0e10cSrcweir private: 420*cdf0e10cSrcweir 421*cdf0e10cSrcweir SaxExpatParser_Impl *m_pParser; 422*cdf0e10cSrcweir }; 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir 427*cdf0e10cSrcweir SaxExpatParser::SaxExpatParser( ) 428*cdf0e10cSrcweir { 429*cdf0e10cSrcweir m_pImpl = new SaxExpatParser_Impl; 430*cdf0e10cSrcweir 431*cdf0e10cSrcweir LocatorImpl *pLoc = new LocatorImpl( m_pImpl ); 432*cdf0e10cSrcweir m_pImpl->rDocumentLocator = Reference< XLocator > ( pLoc ); 433*cdf0e10cSrcweir 434*cdf0e10cSrcweir // performance-Improvment. Reference is needed when calling the startTag callback. 435*cdf0e10cSrcweir // Handing out the same object with every call is allowed (see sax-specification) 436*cdf0e10cSrcweir m_pImpl->pAttrList = new AttributeList; 437*cdf0e10cSrcweir m_pImpl->rAttrList = Reference< XAttributeList > ( m_pImpl->pAttrList ); 438*cdf0e10cSrcweir 439*cdf0e10cSrcweir m_pImpl->bExceptionWasThrown = sal_False; 440*cdf0e10cSrcweir m_pImpl->bRTExceptionWasThrown = sal_False; 441*cdf0e10cSrcweir } 442*cdf0e10cSrcweir 443*cdf0e10cSrcweir SaxExpatParser::~SaxExpatParser() 444*cdf0e10cSrcweir { 445*cdf0e10cSrcweir delete m_pImpl; 446*cdf0e10cSrcweir } 447*cdf0e10cSrcweir 448*cdf0e10cSrcweir 449*cdf0e10cSrcweir /*************** 450*cdf0e10cSrcweir * 451*cdf0e10cSrcweir * parseStream does Parser-startup initializations. The SaxExpatParser_Impl::parse() method does 452*cdf0e10cSrcweir * the file-specific initialization work. (During a parser run, external files may be opened) 453*cdf0e10cSrcweir * 454*cdf0e10cSrcweir ****************/ 455*cdf0e10cSrcweir void SaxExpatParser::parseStream( const InputSource& structSource) 456*cdf0e10cSrcweir throw (SAXException, 457*cdf0e10cSrcweir IOException, 458*cdf0e10cSrcweir RuntimeException) 459*cdf0e10cSrcweir { 460*cdf0e10cSrcweir // Only one text at one time 461*cdf0e10cSrcweir MutexGuard guard( m_pImpl->aMutex ); 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir 464*cdf0e10cSrcweir struct Entity entity; 465*cdf0e10cSrcweir entity.structSource = structSource; 466*cdf0e10cSrcweir 467*cdf0e10cSrcweir if( ! entity.structSource.aInputStream.is() ) 468*cdf0e10cSrcweir { 469*cdf0e10cSrcweir throw SAXException( OUString::createFromAscii( "No input source" ) , 470*cdf0e10cSrcweir Reference< XInterface > () , Any() ); 471*cdf0e10cSrcweir } 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir entity.converter.setInputStream( entity.structSource.aInputStream ); 474*cdf0e10cSrcweir if( entity.structSource.sEncoding.getLength() ) 475*cdf0e10cSrcweir { 476*cdf0e10cSrcweir entity.converter.setEncoding( 477*cdf0e10cSrcweir OUStringToOString( entity.structSource.sEncoding , RTL_TEXTENCODING_ASCII_US ) ); 478*cdf0e10cSrcweir } 479*cdf0e10cSrcweir 480*cdf0e10cSrcweir // create parser with proper encoding 481*cdf0e10cSrcweir entity.pParser = XML_ParserCreate( 0 ); 482*cdf0e10cSrcweir if( ! entity.pParser ) 483*cdf0e10cSrcweir { 484*cdf0e10cSrcweir throw SAXException( OUString::createFromAscii( "Couldn't create parser" ) , 485*cdf0e10cSrcweir Reference< XInterface > (), Any() ); 486*cdf0e10cSrcweir } 487*cdf0e10cSrcweir 488*cdf0e10cSrcweir // set all necessary C-Callbacks 489*cdf0e10cSrcweir XML_SetUserData( entity.pParser , m_pImpl ); 490*cdf0e10cSrcweir XML_SetElementHandler( entity.pParser , 491*cdf0e10cSrcweir call_callbackStartElement , 492*cdf0e10cSrcweir call_callbackEndElement ); 493*cdf0e10cSrcweir XML_SetCharacterDataHandler( entity.pParser , call_callbackCharacters ); 494*cdf0e10cSrcweir XML_SetProcessingInstructionHandler(entity.pParser , 495*cdf0e10cSrcweir call_callbackProcessingInstruction ); 496*cdf0e10cSrcweir XML_SetUnparsedEntityDeclHandler( entity.pParser, 497*cdf0e10cSrcweir call_callbackUnparsedEntityDecl ); 498*cdf0e10cSrcweir XML_SetNotationDeclHandler( entity.pParser, call_callbackNotationDecl ); 499*cdf0e10cSrcweir XML_SetExternalEntityRefHandler( entity.pParser, 500*cdf0e10cSrcweir call_callbackExternalEntityRef); 501*cdf0e10cSrcweir XML_SetUnknownEncodingHandler( entity.pParser, call_callbackUnknownEncoding ,0); 502*cdf0e10cSrcweir 503*cdf0e10cSrcweir if( m_pImpl->rExtendedDocumentHandler.is() ) { 504*cdf0e10cSrcweir 505*cdf0e10cSrcweir // These handlers just delegate calls to the ExtendedHandler. If no extended handler is 506*cdf0e10cSrcweir // given, these callbacks can be ignored 507*cdf0e10cSrcweir XML_SetDefaultHandlerExpand( entity.pParser, call_callbackDefault ); 508*cdf0e10cSrcweir XML_SetCommentHandler( entity.pParser, call_callbackComment ); 509*cdf0e10cSrcweir XML_SetCdataSectionHandler( entity.pParser , 510*cdf0e10cSrcweir call_callbackStartCDATA , 511*cdf0e10cSrcweir call_callbackEndCDATA ); 512*cdf0e10cSrcweir } 513*cdf0e10cSrcweir 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir m_pImpl->exception = SAXParseException(); 516*cdf0e10cSrcweir m_pImpl->pushEntity( entity ); 517*cdf0e10cSrcweir try 518*cdf0e10cSrcweir { 519*cdf0e10cSrcweir // start the document 520*cdf0e10cSrcweir if( m_pImpl->rDocumentHandler.is() ) { 521*cdf0e10cSrcweir m_pImpl->rDocumentHandler->setDocumentLocator( m_pImpl->rDocumentLocator ); 522*cdf0e10cSrcweir m_pImpl->rDocumentHandler->startDocument(); 523*cdf0e10cSrcweir } 524*cdf0e10cSrcweir 525*cdf0e10cSrcweir m_pImpl->parse(); 526*cdf0e10cSrcweir 527*cdf0e10cSrcweir // finish document 528*cdf0e10cSrcweir if( m_pImpl->rDocumentHandler.is() ) { 529*cdf0e10cSrcweir m_pImpl->rDocumentHandler->endDocument(); 530*cdf0e10cSrcweir } 531*cdf0e10cSrcweir } 532*cdf0e10cSrcweir // catch( SAXParseException &e ) 533*cdf0e10cSrcweir // { 534*cdf0e10cSrcweir // m_pImpl->popEntity(); 535*cdf0e10cSrcweir // XML_ParserFree( entity.pParser ); 536*cdf0e10cSrcweir // Any aAny; 537*cdf0e10cSrcweir // aAny <<= e; 538*cdf0e10cSrcweir // throw SAXException( e.Message, e.Context, aAny ); 539*cdf0e10cSrcweir // } 540*cdf0e10cSrcweir catch( SAXException & ) 541*cdf0e10cSrcweir { 542*cdf0e10cSrcweir m_pImpl->popEntity(); 543*cdf0e10cSrcweir XML_ParserFree( entity.pParser ); 544*cdf0e10cSrcweir throw; 545*cdf0e10cSrcweir } 546*cdf0e10cSrcweir catch( IOException & ) 547*cdf0e10cSrcweir { 548*cdf0e10cSrcweir m_pImpl->popEntity(); 549*cdf0e10cSrcweir XML_ParserFree( entity.pParser ); 550*cdf0e10cSrcweir throw; 551*cdf0e10cSrcweir } 552*cdf0e10cSrcweir catch( RuntimeException & ) 553*cdf0e10cSrcweir { 554*cdf0e10cSrcweir m_pImpl->popEntity(); 555*cdf0e10cSrcweir XML_ParserFree( entity.pParser ); 556*cdf0e10cSrcweir throw; 557*cdf0e10cSrcweir } 558*cdf0e10cSrcweir 559*cdf0e10cSrcweir m_pImpl->popEntity(); 560*cdf0e10cSrcweir XML_ParserFree( entity.pParser ); 561*cdf0e10cSrcweir } 562*cdf0e10cSrcweir 563*cdf0e10cSrcweir void SaxExpatParser::setDocumentHandler(const Reference< XDocumentHandler > & xHandler) 564*cdf0e10cSrcweir throw (RuntimeException) 565*cdf0e10cSrcweir { 566*cdf0e10cSrcweir m_pImpl->rDocumentHandler = xHandler; 567*cdf0e10cSrcweir m_pImpl->rExtendedDocumentHandler = 568*cdf0e10cSrcweir Reference< XExtendedDocumentHandler >( xHandler , UNO_QUERY ); 569*cdf0e10cSrcweir } 570*cdf0e10cSrcweir 571*cdf0e10cSrcweir void SaxExpatParser::setErrorHandler(const Reference< XErrorHandler > & xHandler) 572*cdf0e10cSrcweir throw (RuntimeException) 573*cdf0e10cSrcweir { 574*cdf0e10cSrcweir m_pImpl->rErrorHandler = xHandler; 575*cdf0e10cSrcweir } 576*cdf0e10cSrcweir 577*cdf0e10cSrcweir void SaxExpatParser::setDTDHandler(const Reference< XDTDHandler > & xHandler) 578*cdf0e10cSrcweir throw (RuntimeException) 579*cdf0e10cSrcweir { 580*cdf0e10cSrcweir m_pImpl->rDTDHandler = xHandler; 581*cdf0e10cSrcweir } 582*cdf0e10cSrcweir 583*cdf0e10cSrcweir void SaxExpatParser::setEntityResolver(const Reference < XEntityResolver > & xResolver) 584*cdf0e10cSrcweir throw (RuntimeException) 585*cdf0e10cSrcweir { 586*cdf0e10cSrcweir m_pImpl->rEntityResolver = xResolver; 587*cdf0e10cSrcweir } 588*cdf0e10cSrcweir 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir void SaxExpatParser::setLocale( const Locale & locale ) throw (RuntimeException) 591*cdf0e10cSrcweir { 592*cdf0e10cSrcweir m_pImpl->locale = locale; 593*cdf0e10cSrcweir } 594*cdf0e10cSrcweir 595*cdf0e10cSrcweir // XServiceInfo 596*cdf0e10cSrcweir OUString SaxExpatParser::getImplementationName() throw () 597*cdf0e10cSrcweir { 598*cdf0e10cSrcweir return OUString::createFromAscii( IMPLEMENTATION_NAME ); 599*cdf0e10cSrcweir } 600*cdf0e10cSrcweir 601*cdf0e10cSrcweir // XServiceInfo 602*cdf0e10cSrcweir sal_Bool SaxExpatParser::supportsService(const OUString& ServiceName) throw () 603*cdf0e10cSrcweir { 604*cdf0e10cSrcweir Sequence< OUString > aSNL = getSupportedServiceNames(); 605*cdf0e10cSrcweir const OUString * pArray = aSNL.getConstArray(); 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) 608*cdf0e10cSrcweir if( pArray[i] == ServiceName ) 609*cdf0e10cSrcweir return sal_True; 610*cdf0e10cSrcweir 611*cdf0e10cSrcweir return sal_False; 612*cdf0e10cSrcweir } 613*cdf0e10cSrcweir 614*cdf0e10cSrcweir // XServiceInfo 615*cdf0e10cSrcweir Sequence< OUString > SaxExpatParser::getSupportedServiceNames(void) throw () 616*cdf0e10cSrcweir { 617*cdf0e10cSrcweir 618*cdf0e10cSrcweir Sequence<OUString> seq(1); 619*cdf0e10cSrcweir seq.getArray()[0] = OUString::createFromAscii( SERVICE_NAME ); 620*cdf0e10cSrcweir return seq; 621*cdf0e10cSrcweir } 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir 624*cdf0e10cSrcweir /*--------------------------------------- 625*cdf0e10cSrcweir * 626*cdf0e10cSrcweir * Helper functions and classes 627*cdf0e10cSrcweir * 628*cdf0e10cSrcweir * 629*cdf0e10cSrcweir *-------------------------------------------*/ 630*cdf0e10cSrcweir OUString getErrorMessage( XML_Error xmlE, OUString sSystemId , sal_Int32 nLine ) 631*cdf0e10cSrcweir { 632*cdf0e10cSrcweir OUString Message; 633*cdf0e10cSrcweir if( XML_ERROR_NONE == xmlE ) { 634*cdf0e10cSrcweir Message = OUString::createFromAscii( "No" ); 635*cdf0e10cSrcweir } 636*cdf0e10cSrcweir else if( XML_ERROR_NO_MEMORY == xmlE ) { 637*cdf0e10cSrcweir Message = OUString::createFromAscii( "no memory" ); 638*cdf0e10cSrcweir } 639*cdf0e10cSrcweir else if( XML_ERROR_SYNTAX == xmlE ) { 640*cdf0e10cSrcweir Message = OUString::createFromAscii( "syntax" ); 641*cdf0e10cSrcweir } 642*cdf0e10cSrcweir else if( XML_ERROR_NO_ELEMENTS == xmlE ) { 643*cdf0e10cSrcweir Message = OUString::createFromAscii( "no elements" ); 644*cdf0e10cSrcweir } 645*cdf0e10cSrcweir else if( XML_ERROR_INVALID_TOKEN == xmlE ) { 646*cdf0e10cSrcweir Message = OUString::createFromAscii( "invalid token" ); 647*cdf0e10cSrcweir } 648*cdf0e10cSrcweir else if( XML_ERROR_UNCLOSED_TOKEN == xmlE ) { 649*cdf0e10cSrcweir Message = OUString::createFromAscii( "unclosed token" ); 650*cdf0e10cSrcweir } 651*cdf0e10cSrcweir else if( XML_ERROR_PARTIAL_CHAR == xmlE ) { 652*cdf0e10cSrcweir Message = OUString::createFromAscii( "partial char" ); 653*cdf0e10cSrcweir } 654*cdf0e10cSrcweir else if( XML_ERROR_TAG_MISMATCH == xmlE ) { 655*cdf0e10cSrcweir Message = OUString::createFromAscii( "tag mismatch" ); 656*cdf0e10cSrcweir } 657*cdf0e10cSrcweir else if( XML_ERROR_DUPLICATE_ATTRIBUTE == xmlE ) { 658*cdf0e10cSrcweir Message = OUString::createFromAscii( "duplicate attribute" ); 659*cdf0e10cSrcweir } 660*cdf0e10cSrcweir else if( XML_ERROR_JUNK_AFTER_DOC_ELEMENT == xmlE ) { 661*cdf0e10cSrcweir Message = OUString::createFromAscii( "junk after doc element" ); 662*cdf0e10cSrcweir } 663*cdf0e10cSrcweir else if( XML_ERROR_PARAM_ENTITY_REF == xmlE ) { 664*cdf0e10cSrcweir Message = OUString::createFromAscii( "parameter entity reference" ); 665*cdf0e10cSrcweir } 666*cdf0e10cSrcweir else if( XML_ERROR_UNDEFINED_ENTITY == xmlE ) { 667*cdf0e10cSrcweir Message = OUString::createFromAscii( "undefined entity" ); 668*cdf0e10cSrcweir } 669*cdf0e10cSrcweir else if( XML_ERROR_RECURSIVE_ENTITY_REF == xmlE ) { 670*cdf0e10cSrcweir Message = OUString::createFromAscii( "recursive entity reference" ); 671*cdf0e10cSrcweir } 672*cdf0e10cSrcweir else if( XML_ERROR_ASYNC_ENTITY == xmlE ) { 673*cdf0e10cSrcweir Message = OUString::createFromAscii( "async entity" ); 674*cdf0e10cSrcweir } 675*cdf0e10cSrcweir else if( XML_ERROR_BAD_CHAR_REF == xmlE ) { 676*cdf0e10cSrcweir Message = OUString::createFromAscii( "bad char reference" ); 677*cdf0e10cSrcweir } 678*cdf0e10cSrcweir else if( XML_ERROR_BINARY_ENTITY_REF == xmlE ) { 679*cdf0e10cSrcweir Message = OUString::createFromAscii( "binary entity reference" ); 680*cdf0e10cSrcweir } 681*cdf0e10cSrcweir else if( XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF == xmlE ) { 682*cdf0e10cSrcweir Message = OUString::createFromAscii( "attribute external entity reference" ); 683*cdf0e10cSrcweir } 684*cdf0e10cSrcweir else if( XML_ERROR_MISPLACED_XML_PI == xmlE ) { 685*cdf0e10cSrcweir Message = OUString::createFromAscii( "misplaced xml processing instruction" ); 686*cdf0e10cSrcweir } 687*cdf0e10cSrcweir else if( XML_ERROR_UNKNOWN_ENCODING == xmlE ) { 688*cdf0e10cSrcweir Message = OUString::createFromAscii( "unknown encoding" ); 689*cdf0e10cSrcweir } 690*cdf0e10cSrcweir else if( XML_ERROR_INCORRECT_ENCODING == xmlE ) { 691*cdf0e10cSrcweir Message = OUString::createFromAscii( "incorrect encoding" ); 692*cdf0e10cSrcweir } 693*cdf0e10cSrcweir else if( XML_ERROR_UNCLOSED_CDATA_SECTION == xmlE ) { 694*cdf0e10cSrcweir Message = OUString::createFromAscii( "unclosed cdata section" ); 695*cdf0e10cSrcweir } 696*cdf0e10cSrcweir else if( XML_ERROR_EXTERNAL_ENTITY_HANDLING == xmlE ) { 697*cdf0e10cSrcweir Message = OUString::createFromAscii( "external entity reference" ); 698*cdf0e10cSrcweir } 699*cdf0e10cSrcweir else if( XML_ERROR_NOT_STANDALONE == xmlE ) { 700*cdf0e10cSrcweir Message = OUString::createFromAscii( "not standalone" ); 701*cdf0e10cSrcweir } 702*cdf0e10cSrcweir 703*cdf0e10cSrcweir OUString str = OUString::createFromAscii( "[" ); 704*cdf0e10cSrcweir str += sSystemId; 705*cdf0e10cSrcweir str += OUString::createFromAscii( " line " ); 706*cdf0e10cSrcweir str += OUString::valueOf( nLine ); 707*cdf0e10cSrcweir str += OUString::createFromAscii( "]: " ); 708*cdf0e10cSrcweir str += Message; 709*cdf0e10cSrcweir str += OUString::createFromAscii( "error" ); 710*cdf0e10cSrcweir 711*cdf0e10cSrcweir return str; 712*cdf0e10cSrcweir } 713*cdf0e10cSrcweir 714*cdf0e10cSrcweir 715*cdf0e10cSrcweir // starts parsing with actual parser ! 716*cdf0e10cSrcweir void SaxExpatParser_Impl::parse( ) 717*cdf0e10cSrcweir { 718*cdf0e10cSrcweir const int nBufSize = 16*1024; 719*cdf0e10cSrcweir 720*cdf0e10cSrcweir int nRead = nBufSize; 721*cdf0e10cSrcweir Sequence< sal_Int8 > seqOut(nBufSize); 722*cdf0e10cSrcweir 723*cdf0e10cSrcweir while( nRead ) { 724*cdf0e10cSrcweir nRead = getEntity().converter.readAndConvert( seqOut , nBufSize ); 725*cdf0e10cSrcweir 726*cdf0e10cSrcweir if( ! nRead ) { 727*cdf0e10cSrcweir XML_Parse( getEntity().pParser , 728*cdf0e10cSrcweir ( const char * ) seqOut.getArray() , 729*cdf0e10cSrcweir 0 , 730*cdf0e10cSrcweir 1 ); 731*cdf0e10cSrcweir break; 732*cdf0e10cSrcweir } 733*cdf0e10cSrcweir 734*cdf0e10cSrcweir sal_Bool bContinue = ( XML_Parse( getEntity().pParser , 735*cdf0e10cSrcweir (const char *) seqOut.getArray(), 736*cdf0e10cSrcweir nRead, 737*cdf0e10cSrcweir 0 ) != 0 ); 738*cdf0e10cSrcweir 739*cdf0e10cSrcweir if( ! bContinue || this->bExceptionWasThrown ) { 740*cdf0e10cSrcweir 741*cdf0e10cSrcweir if ( this->bRTExceptionWasThrown ) 742*cdf0e10cSrcweir throw rtexception; 743*cdf0e10cSrcweir 744*cdf0e10cSrcweir // Error during parsing ! 745*cdf0e10cSrcweir XML_Error xmlE = XML_GetErrorCode( getEntity().pParser ); 746*cdf0e10cSrcweir OUString sSystemId = rDocumentLocator->getSystemId(); 747*cdf0e10cSrcweir sal_Int32 nLine = rDocumentLocator->getLineNumber(); 748*cdf0e10cSrcweir 749*cdf0e10cSrcweir SAXParseException aExcept( 750*cdf0e10cSrcweir getErrorMessage(xmlE , sSystemId, nLine) , 751*cdf0e10cSrcweir Reference< XInterface >(), 752*cdf0e10cSrcweir Any( &exception , getCppuType( &exception) ), 753*cdf0e10cSrcweir rDocumentLocator->getPublicId(), 754*cdf0e10cSrcweir rDocumentLocator->getSystemId(), 755*cdf0e10cSrcweir rDocumentLocator->getLineNumber(), 756*cdf0e10cSrcweir rDocumentLocator->getColumnNumber() 757*cdf0e10cSrcweir ); 758*cdf0e10cSrcweir 759*cdf0e10cSrcweir if( rErrorHandler.is() ) { 760*cdf0e10cSrcweir 761*cdf0e10cSrcweir // error handler is set, so the handler may throw the exception 762*cdf0e10cSrcweir Any a; 763*cdf0e10cSrcweir a <<= aExcept; 764*cdf0e10cSrcweir rErrorHandler->fatalError( a ); 765*cdf0e10cSrcweir } 766*cdf0e10cSrcweir 767*cdf0e10cSrcweir // Error handler has not thrown an exception, but parsing cannot go on, 768*cdf0e10cSrcweir // so an exception MUST be thrown. 769*cdf0e10cSrcweir throw aExcept; 770*cdf0e10cSrcweir } // if( ! bContinue ) 771*cdf0e10cSrcweir } // while 772*cdf0e10cSrcweir } 773*cdf0e10cSrcweir 774*cdf0e10cSrcweir //------------------------------------------ 775*cdf0e10cSrcweir // 776*cdf0e10cSrcweir // The C-Callbacks 777*cdf0e10cSrcweir // 778*cdf0e10cSrcweir //----------------------------------------- 779*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackStartElement( void *pvThis , 780*cdf0e10cSrcweir const XML_Char *pwName , 781*cdf0e10cSrcweir const XML_Char **awAttributes ) 782*cdf0e10cSrcweir { 783*cdf0e10cSrcweir // in case of two concurrent threads, there is only the danger of an leak, 784*cdf0e10cSrcweir // which is neglectable for one string 785*cdf0e10cSrcweir static OUString g_CDATA( RTL_CONSTASCII_USTRINGPARAM( "CDATA" ) ); 786*cdf0e10cSrcweir 787*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 788*cdf0e10cSrcweir 789*cdf0e10cSrcweir if( pImpl->rDocumentHandler.is() ) { 790*cdf0e10cSrcweir 791*cdf0e10cSrcweir int i = 0; 792*cdf0e10cSrcweir pImpl->pAttrList->clear(); 793*cdf0e10cSrcweir 794*cdf0e10cSrcweir while( awAttributes[i] ) { 795*cdf0e10cSrcweir OSL_ASSERT( awAttributes[i+1] ); 796*cdf0e10cSrcweir pImpl->pAttrList->addAttribute( 797*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( awAttributes[i] ) , 798*cdf0e10cSrcweir g_CDATA , // expat doesn't know types 799*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( awAttributes[i+1] ) ); 800*cdf0e10cSrcweir i +=2; 801*cdf0e10cSrcweir } 802*cdf0e10cSrcweir 803*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( 804*cdf0e10cSrcweir pImpl , 805*cdf0e10cSrcweir rDocumentHandler->startElement( XML_CHAR_TO_OUSTRING( pwName ) , 806*cdf0e10cSrcweir pImpl->rAttrList ) ); 807*cdf0e10cSrcweir } 808*cdf0e10cSrcweir } 809*cdf0e10cSrcweir 810*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackEndElement( void *pvThis , const XML_Char *pwName ) 811*cdf0e10cSrcweir { 812*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 813*cdf0e10cSrcweir 814*cdf0e10cSrcweir if( pImpl->rDocumentHandler.is() ) { 815*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl, 816*cdf0e10cSrcweir rDocumentHandler->endElement( XML_CHAR_TO_OUSTRING( pwName ) ) ); 817*cdf0e10cSrcweir } 818*cdf0e10cSrcweir } 819*cdf0e10cSrcweir 820*cdf0e10cSrcweir 821*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackCharacters( void *pvThis , const XML_Char *s , int nLen ) 822*cdf0e10cSrcweir { 823*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 824*cdf0e10cSrcweir 825*cdf0e10cSrcweir if( pImpl->rDocumentHandler.is() ) { 826*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl , 827*cdf0e10cSrcweir rDocumentHandler->characters( XML_CHAR_N_TO_USTRING(s,nLen) ) ); 828*cdf0e10cSrcweir } 829*cdf0e10cSrcweir } 830*cdf0e10cSrcweir 831*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackProcessingInstruction( void *pvThis, 832*cdf0e10cSrcweir const XML_Char *sTarget , 833*cdf0e10cSrcweir const XML_Char *sData ) 834*cdf0e10cSrcweir { 835*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 836*cdf0e10cSrcweir if( pImpl->rDocumentHandler.is() ) { 837*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( 838*cdf0e10cSrcweir pImpl , 839*cdf0e10cSrcweir rDocumentHandler->processingInstruction( XML_CHAR_TO_OUSTRING( sTarget ), 840*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( sData ) ) ); 841*cdf0e10cSrcweir } 842*cdf0e10cSrcweir } 843*cdf0e10cSrcweir 844*cdf0e10cSrcweir 845*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackUnparsedEntityDecl(void *pvThis , 846*cdf0e10cSrcweir const XML_Char *entityName, 847*cdf0e10cSrcweir const XML_Char * /*base*/, 848*cdf0e10cSrcweir const XML_Char *systemId, 849*cdf0e10cSrcweir const XML_Char *publicId, 850*cdf0e10cSrcweir const XML_Char *notationName) 851*cdf0e10cSrcweir { 852*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 853*cdf0e10cSrcweir if( pImpl->rDTDHandler.is() ) { 854*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( 855*cdf0e10cSrcweir pImpl , 856*cdf0e10cSrcweir rDTDHandler->unparsedEntityDecl( 857*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( entityName ), 858*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( publicId ) , 859*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( systemId ) , 860*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( notationName ) ) ); 861*cdf0e10cSrcweir } 862*cdf0e10cSrcweir } 863*cdf0e10cSrcweir 864*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackNotationDecl( void *pvThis, 865*cdf0e10cSrcweir const XML_Char *notationName, 866*cdf0e10cSrcweir const XML_Char * /*base*/, 867*cdf0e10cSrcweir const XML_Char *systemId, 868*cdf0e10cSrcweir const XML_Char *publicId) 869*cdf0e10cSrcweir { 870*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 871*cdf0e10cSrcweir if( pImpl->rDTDHandler.is() ) { 872*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl, 873*cdf0e10cSrcweir rDTDHandler->notationDecl( XML_CHAR_TO_OUSTRING( notationName ) , 874*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( publicId ) , 875*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( systemId ) ) ); 876*cdf0e10cSrcweir } 877*cdf0e10cSrcweir 878*cdf0e10cSrcweir } 879*cdf0e10cSrcweir 880*cdf0e10cSrcweir 881*cdf0e10cSrcweir 882*cdf0e10cSrcweir int SaxExpatParser_Impl::callbackExternalEntityRef( XML_Parser parser, 883*cdf0e10cSrcweir const XML_Char *context, 884*cdf0e10cSrcweir const XML_Char * /*base*/, 885*cdf0e10cSrcweir const XML_Char *systemId, 886*cdf0e10cSrcweir const XML_Char *publicId) 887*cdf0e10cSrcweir { 888*cdf0e10cSrcweir sal_Bool bOK = sal_True; 889*cdf0e10cSrcweir InputSource source; 890*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)XML_GetUserData( parser )); 891*cdf0e10cSrcweir 892*cdf0e10cSrcweir struct Entity entity; 893*cdf0e10cSrcweir 894*cdf0e10cSrcweir if( pImpl->rEntityResolver.is() ) { 895*cdf0e10cSrcweir try 896*cdf0e10cSrcweir { 897*cdf0e10cSrcweir entity.structSource = pImpl->rEntityResolver->resolveEntity( 898*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( publicId ) , 899*cdf0e10cSrcweir XML_CHAR_TO_OUSTRING( systemId ) ); 900*cdf0e10cSrcweir } 901*cdf0e10cSrcweir catch( SAXParseException & e ) 902*cdf0e10cSrcweir { 903*cdf0e10cSrcweir pImpl->exception = e; 904*cdf0e10cSrcweir bOK = sal_False; 905*cdf0e10cSrcweir } 906*cdf0e10cSrcweir catch( SAXException & e ) 907*cdf0e10cSrcweir { 908*cdf0e10cSrcweir pImpl->exception = SAXParseException( 909*cdf0e10cSrcweir e.Message , e.Context , e.WrappedException , 910*cdf0e10cSrcweir pImpl->rDocumentLocator->getPublicId(), 911*cdf0e10cSrcweir pImpl->rDocumentLocator->getSystemId(), 912*cdf0e10cSrcweir pImpl->rDocumentLocator->getLineNumber(), 913*cdf0e10cSrcweir pImpl->rDocumentLocator->getColumnNumber() ); 914*cdf0e10cSrcweir bOK = sal_False; 915*cdf0e10cSrcweir } 916*cdf0e10cSrcweir } 917*cdf0e10cSrcweir 918*cdf0e10cSrcweir if( entity.structSource.aInputStream.is() ) { 919*cdf0e10cSrcweir entity.pParser = XML_ExternalEntityParserCreate( parser , context, 0 ); 920*cdf0e10cSrcweir if( ! entity.pParser ) 921*cdf0e10cSrcweir { 922*cdf0e10cSrcweir return sal_False; 923*cdf0e10cSrcweir } 924*cdf0e10cSrcweir 925*cdf0e10cSrcweir entity.converter.setInputStream( entity.structSource.aInputStream ); 926*cdf0e10cSrcweir pImpl->pushEntity( entity ); 927*cdf0e10cSrcweir try 928*cdf0e10cSrcweir { 929*cdf0e10cSrcweir pImpl->parse(); 930*cdf0e10cSrcweir } 931*cdf0e10cSrcweir catch( SAXParseException & e ) 932*cdf0e10cSrcweir { 933*cdf0e10cSrcweir pImpl->exception = e; 934*cdf0e10cSrcweir bOK = sal_False; 935*cdf0e10cSrcweir } 936*cdf0e10cSrcweir catch( IOException &e ) 937*cdf0e10cSrcweir { 938*cdf0e10cSrcweir pImpl->exception.WrappedException <<= e; 939*cdf0e10cSrcweir bOK = sal_False; 940*cdf0e10cSrcweir } 941*cdf0e10cSrcweir catch( RuntimeException &e ) 942*cdf0e10cSrcweir { 943*cdf0e10cSrcweir pImpl->exception.WrappedException <<=e; 944*cdf0e10cSrcweir bOK = sal_False; 945*cdf0e10cSrcweir } 946*cdf0e10cSrcweir 947*cdf0e10cSrcweir pImpl->popEntity(); 948*cdf0e10cSrcweir 949*cdf0e10cSrcweir XML_ParserFree( entity.pParser ); 950*cdf0e10cSrcweir } 951*cdf0e10cSrcweir 952*cdf0e10cSrcweir return bOK; 953*cdf0e10cSrcweir } 954*cdf0e10cSrcweir 955*cdf0e10cSrcweir int SaxExpatParser_Impl::callbackUnknownEncoding(void * /*encodingHandlerData*/, 956*cdf0e10cSrcweir const XML_Char * /*name*/, 957*cdf0e10cSrcweir XML_Encoding * /*info*/) 958*cdf0e10cSrcweir { 959*cdf0e10cSrcweir return 0; 960*cdf0e10cSrcweir } 961*cdf0e10cSrcweir 962*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackDefault( void *pvThis, const XML_Char *s, int len) 963*cdf0e10cSrcweir { 964*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 965*cdf0e10cSrcweir 966*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl, 967*cdf0e10cSrcweir rExtendedDocumentHandler->unknown( XML_CHAR_N_TO_USTRING( s ,len) ) ); 968*cdf0e10cSrcweir } 969*cdf0e10cSrcweir 970*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackComment( void *pvThis , const XML_Char *s ) 971*cdf0e10cSrcweir { 972*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 973*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl, 974*cdf0e10cSrcweir rExtendedDocumentHandler->comment( XML_CHAR_TO_OUSTRING( s ) ) ); 975*cdf0e10cSrcweir } 976*cdf0e10cSrcweir 977*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackStartCDATA( void *pvThis ) 978*cdf0e10cSrcweir { 979*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 980*cdf0e10cSrcweir 981*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS( pImpl, rExtendedDocumentHandler->startCDATA() ); 982*cdf0e10cSrcweir } 983*cdf0e10cSrcweir 984*cdf0e10cSrcweir 985*cdf0e10cSrcweir void SaxExpatParser_Impl::callErrorHandler( SaxExpatParser_Impl *pImpl , 986*cdf0e10cSrcweir const SAXParseException & e ) 987*cdf0e10cSrcweir { 988*cdf0e10cSrcweir try 989*cdf0e10cSrcweir { 990*cdf0e10cSrcweir if( pImpl->rErrorHandler.is() ) { 991*cdf0e10cSrcweir Any a; 992*cdf0e10cSrcweir a <<= e; 993*cdf0e10cSrcweir pImpl->rErrorHandler->error( a ); 994*cdf0e10cSrcweir } 995*cdf0e10cSrcweir else { 996*cdf0e10cSrcweir pImpl->exception = e; 997*cdf0e10cSrcweir pImpl->bExceptionWasThrown = sal_True; 998*cdf0e10cSrcweir } 999*cdf0e10cSrcweir } 1000*cdf0e10cSrcweir catch( SAXParseException & ex ) { 1001*cdf0e10cSrcweir pImpl->exception = ex; 1002*cdf0e10cSrcweir pImpl->bExceptionWasThrown = sal_True; 1003*cdf0e10cSrcweir } 1004*cdf0e10cSrcweir catch( SAXException & ex ) { 1005*cdf0e10cSrcweir pImpl->exception = SAXParseException( 1006*cdf0e10cSrcweir ex.Message, 1007*cdf0e10cSrcweir ex.Context, 1008*cdf0e10cSrcweir ex.WrappedException, 1009*cdf0e10cSrcweir pImpl->rDocumentLocator->getPublicId(), 1010*cdf0e10cSrcweir pImpl->rDocumentLocator->getSystemId(), 1011*cdf0e10cSrcweir pImpl->rDocumentLocator->getLineNumber(), 1012*cdf0e10cSrcweir pImpl->rDocumentLocator->getColumnNumber() 1013*cdf0e10cSrcweir ); 1014*cdf0e10cSrcweir pImpl->bExceptionWasThrown = sal_True; 1015*cdf0e10cSrcweir } 1016*cdf0e10cSrcweir } 1017*cdf0e10cSrcweir 1018*cdf0e10cSrcweir void SaxExpatParser_Impl::callbackEndCDATA( void *pvThis ) 1019*cdf0e10cSrcweir { 1020*cdf0e10cSrcweir SaxExpatParser_Impl *pImpl = ((SaxExpatParser_Impl*)pvThis); 1021*cdf0e10cSrcweir 1022*cdf0e10cSrcweir CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS(pImpl,rExtendedDocumentHandler->endCDATA() ); 1023*cdf0e10cSrcweir } 1024*cdf0e10cSrcweir 1025*cdf0e10cSrcweir } 1026*cdf0e10cSrcweir using namespace sax_expatwrap; 1027*cdf0e10cSrcweir 1028*cdf0e10cSrcweir extern "C" 1029*cdf0e10cSrcweir { 1030*cdf0e10cSrcweir 1031*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment( 1032*cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ ) 1033*cdf0e10cSrcweir { 1034*cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 1035*cdf0e10cSrcweir } 1036*cdf0e10cSrcweir 1037*cdf0e10cSrcweir void * SAL_CALL component_getFactory( 1038*cdf0e10cSrcweir const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ ) 1039*cdf0e10cSrcweir { 1040*cdf0e10cSrcweir void * pRet = 0; 1041*cdf0e10cSrcweir 1042*cdf0e10cSrcweir if (pServiceManager ) 1043*cdf0e10cSrcweir { 1044*cdf0e10cSrcweir Reference< XSingleServiceFactory > xRet; 1045*cdf0e10cSrcweir Reference< XMultiServiceFactory > xSMgr = 1046*cdf0e10cSrcweir reinterpret_cast< XMultiServiceFactory * > ( pServiceManager ); 1047*cdf0e10cSrcweir 1048*cdf0e10cSrcweir OUString aImplementationName = OUString::createFromAscii( pImplName ); 1049*cdf0e10cSrcweir 1050*cdf0e10cSrcweir if (aImplementationName == 1051*cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) ) ) 1052*cdf0e10cSrcweir { 1053*cdf0e10cSrcweir xRet = createSingleFactory( xSMgr, aImplementationName, 1054*cdf0e10cSrcweir SaxExpatParser_CreateInstance, 1055*cdf0e10cSrcweir SaxExpatParser::getSupportedServiceNames_Static() ); 1056*cdf0e10cSrcweir } 1057*cdf0e10cSrcweir else if ( aImplementationName == SaxWriter_getImplementationName() ) 1058*cdf0e10cSrcweir { 1059*cdf0e10cSrcweir xRet = createSingleFactory( xSMgr, aImplementationName, 1060*cdf0e10cSrcweir SaxWriter_CreateInstance, 1061*cdf0e10cSrcweir SaxWriter_getSupportedServiceNames() ); 1062*cdf0e10cSrcweir } 1063*cdf0e10cSrcweir 1064*cdf0e10cSrcweir if (xRet.is()) 1065*cdf0e10cSrcweir { 1066*cdf0e10cSrcweir xRet->acquire(); 1067*cdf0e10cSrcweir pRet = xRet.get(); 1068*cdf0e10cSrcweir } 1069*cdf0e10cSrcweir } 1070*cdf0e10cSrcweir 1071*cdf0e10cSrcweir return pRet; 1072*cdf0e10cSrcweir } 1073*cdf0e10cSrcweir 1074*cdf0e10cSrcweir 1075*cdf0e10cSrcweir } 1076*cdf0e10cSrcweir 1077