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