1f9b72d11SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3f9b72d11SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4f9b72d11SAndrew Rist * or more contributor license agreements. See the NOTICE file
5f9b72d11SAndrew Rist * distributed with this work for additional information
6f9b72d11SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7f9b72d11SAndrew Rist * to you under the Apache License, Version 2.0 (the
8f9b72d11SAndrew Rist * "License"); you may not use this file except in compliance
9f9b72d11SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11f9b72d11SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13f9b72d11SAndrew Rist * Unless required by applicable law or agreed to in writing,
14f9b72d11SAndrew Rist * software distributed under the License is distributed on an
15f9b72d11SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f9b72d11SAndrew Rist * KIND, either express or implied. See the License for the
17f9b72d11SAndrew Rist * specific language governing permissions and limitations
18f9b72d11SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20f9b72d11SAndrew Rist *************************************************************/
21f9b72d11SAndrew Rist
22f9b72d11SAndrew 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>
41*85ec52e3SDamjan Jovanovic #include <cppuhelper/implementationentry.hxx>
42cdf0e10cSrcweir
43cdf0e10cSrcweir #include <expat.h>
44cdf0e10cSrcweir
45cdf0e10cSrcweir using namespace ::rtl;
46cdf0e10cSrcweir using namespace ::std;
47cdf0e10cSrcweir using namespace ::osl;
48cdf0e10cSrcweir using namespace ::cppu;
49cdf0e10cSrcweir using namespace ::com::sun::star::uno;
50cdf0e10cSrcweir using namespace ::com::sun::star::lang;
51cdf0e10cSrcweir using namespace ::com::sun::star::registry;
52cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
53cdf0e10cSrcweir using namespace ::com::sun::star::util;
54cdf0e10cSrcweir using namespace ::com::sun::star::io;
55cdf0e10cSrcweir
56cdf0e10cSrcweir #include "factory.hxx"
57cdf0e10cSrcweir #include "attrlistimpl.hxx"
58cdf0e10cSrcweir #include "xml2utf.hxx"
59cdf0e10cSrcweir
60cdf0e10cSrcweir namespace sax_expatwrap {
61cdf0e10cSrcweir
6286e1cf34SPedro Giffuni // Useful macros for correct String conversion depending on the chosen expat-mode
63cdf0e10cSrcweir #ifdef XML_UNICODE
XmlNChar2OUString(const XML_Char * p,int nLen)64cdf0e10cSrcweir OUString XmlNChar2OUString( const XML_Char *p , int nLen )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir if( p ) {
67cdf0e10cSrcweir if( sizeof( sal_Unicode ) == sizeof( XML_Char ) )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir return OUString( (sal_Unicode*)p,nLen);
70cdf0e10cSrcweir }
71cdf0e10cSrcweir else
72cdf0e10cSrcweir {
73cdf0e10cSrcweir sal_Unicode *pWchar = (sal_Unicode *)alloca( sizeof( sal_Unicode ) * nLen );
74cdf0e10cSrcweir for( int n = 0 ; n < nLen ; n++ ) {
75cdf0e10cSrcweir pWchar[n] = (sal_Unicode) p[n];
76cdf0e10cSrcweir }
77cdf0e10cSrcweir return OUString( pWchar , nLen );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir }
80cdf0e10cSrcweir else {
81cdf0e10cSrcweir return OUString();
82cdf0e10cSrcweir }
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
XmlChar2OUString(const XML_Char * p)85cdf0e10cSrcweir OUString XmlChar2OUString( const XML_Char *p )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir if( p ) {
88cdf0e10cSrcweir int nLen;
89cdf0e10cSrcweir for( nLen = 0 ; p[nLen] ; nLen ++ )
90cdf0e10cSrcweir ;
91cdf0e10cSrcweir return XmlNChar2OUString( p , nLen );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir else return OUString();
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
96cdf0e10cSrcweir
97cdf0e10cSrcweir #define XML_CHAR_TO_OUSTRING(x) XmlChar2OUString(x)
98cdf0e10cSrcweir #define XML_CHAR_N_TO_USTRING(x,n) XmlNChar2OUString(x,n)
99cdf0e10cSrcweir #else
100cdf0e10cSrcweir #define XML_CHAR_TO_OUSTRING(x) OUString(x , strlen( x ), RTL_TEXTENCODING_UTF8)
101cdf0e10cSrcweir #define XML_CHAR_N_TO_USTRING(x,n) OUString(x,n, RTL_TEXTENCODING_UTF8 )
102cdf0e10cSrcweir #endif
103cdf0e10cSrcweir
104cdf0e10cSrcweir
105cdf0e10cSrcweir /*
106cdf0e10cSrcweir * The following macro encapsulates any call to an event handler.
107cdf0e10cSrcweir * It ensures, that exceptions thrown by the event handler are
108cdf0e10cSrcweir * treated properly.
109cdf0e10cSrcweir */
110cdf0e10cSrcweir #define CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS(pThis,call) \
111cdf0e10cSrcweir if( ! pThis->bExceptionWasThrown ) { \
112cdf0e10cSrcweir try {\
113cdf0e10cSrcweir pThis->call;\
114cdf0e10cSrcweir }\
115cdf0e10cSrcweir catch( SAXParseException &e ) {\
116cdf0e10cSrcweir pThis->callErrorHandler( pThis , e );\
117cdf0e10cSrcweir }\
118cdf0e10cSrcweir catch( SAXException &e ) {\
119cdf0e10cSrcweir pThis->callErrorHandler( pThis , SAXParseException(\
120cdf0e10cSrcweir e.Message, \
121cdf0e10cSrcweir e.Context, \
122cdf0e10cSrcweir e.WrappedException,\
123cdf0e10cSrcweir pThis->rDocumentLocator->getPublicId(),\
124cdf0e10cSrcweir pThis->rDocumentLocator->getSystemId(),\
125cdf0e10cSrcweir pThis->rDocumentLocator->getLineNumber(),\
126cdf0e10cSrcweir pThis->rDocumentLocator->getColumnNumber()\
127cdf0e10cSrcweir ) );\
128cdf0e10cSrcweir }\
129cdf0e10cSrcweir catch( com::sun::star::uno::RuntimeException &e ) {\
130cdf0e10cSrcweir pThis->bExceptionWasThrown = sal_True; \
131cdf0e10cSrcweir pThis->bRTExceptionWasThrown = sal_True; \
132cdf0e10cSrcweir pImpl->rtexception = e; \
133cdf0e10cSrcweir }\
134cdf0e10cSrcweir }\
135cdf0e10cSrcweir ((void)0)
136cdf0e10cSrcweir
137cdf0e10cSrcweir
138cdf0e10cSrcweir class SaxExpatParser_Impl;
139cdf0e10cSrcweir
140cdf0e10cSrcweir
141cdf0e10cSrcweir // This class implements the external Parser interface
142cdf0e10cSrcweir class SaxExpatParser :
143cdf0e10cSrcweir public WeakImplHelper2<
144cdf0e10cSrcweir XParser,
145cdf0e10cSrcweir XServiceInfo
146cdf0e10cSrcweir >
147cdf0e10cSrcweir {
148cdf0e10cSrcweir
149cdf0e10cSrcweir public:
150cdf0e10cSrcweir SaxExpatParser();
151cdf0e10cSrcweir ~SaxExpatParser();
152cdf0e10cSrcweir
153cdf0e10cSrcweir public:
154cdf0e10cSrcweir
155cdf0e10cSrcweir // The implementation details
156*85ec52e3SDamjan Jovanovic static OUString getImplementationName_Static(void) throw();
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(Reference<XComponentContext> const &)191cdf0e10cSrcweir Reference< XInterface > SAL_CALL SaxExpatParser_CreateInstance(
192*85ec52e3SDamjan Jovanovic Reference< XComponentContext > const & ) throw(Exception)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir SaxExpatParser *p = new SaxExpatParser;
195cdf0e10cSrcweir
196cdf0e10cSrcweir return Reference< XInterface > ( (OWeakObject * ) p );
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
getImplementationName_Static()199*85ec52e3SDamjan Jovanovic OUString SaxExpatParser::getImplementationName_Static() throw ()
200*85ec52e3SDamjan Jovanovic {
201*85ec52e3SDamjan Jovanovic return OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.extensions.xml.sax.ParserExpat" ) );
202*85ec52e3SDamjan Jovanovic }
203cdf0e10cSrcweir
getSupportedServiceNames_Static(void)204cdf0e10cSrcweir Sequence< OUString > SaxExpatParser::getSupportedServiceNames_Static(void) throw ()
205cdf0e10cSrcweir {
206cdf0e10cSrcweir Sequence<OUString> aRet(1);
207*85ec52e3SDamjan Jovanovic aRet.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) );
208cdf0e10cSrcweir return aRet;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir
212cdf0e10cSrcweir //---------------------------------------------
213cdf0e10cSrcweir // the implementation part
214cdf0e10cSrcweir //---------------------------------------------
215cdf0e10cSrcweir
216cdf0e10cSrcweir
217cdf0e10cSrcweir // Entity binds all information neede for a single file
218cdf0e10cSrcweir struct Entity
219cdf0e10cSrcweir {
220cdf0e10cSrcweir InputSource structSource;
221cdf0e10cSrcweir XML_Parser pParser;
222cdf0e10cSrcweir XMLFile2UTFConverter converter;
223cdf0e10cSrcweir };
224cdf0e10cSrcweir
225cdf0e10cSrcweir
226cdf0e10cSrcweir class SaxExpatParser_Impl
227cdf0e10cSrcweir {
228cdf0e10cSrcweir public: // module scope
229cdf0e10cSrcweir Mutex aMutex;
230cdf0e10cSrcweir
231cdf0e10cSrcweir Reference< XDocumentHandler > rDocumentHandler;
232cdf0e10cSrcweir Reference< XExtendedDocumentHandler > rExtendedDocumentHandler;
233cdf0e10cSrcweir
234cdf0e10cSrcweir Reference< XErrorHandler > rErrorHandler;
235cdf0e10cSrcweir Reference< XDTDHandler > rDTDHandler;
236cdf0e10cSrcweir Reference< XEntityResolver > rEntityResolver;
237cdf0e10cSrcweir Reference < XLocator > rDocumentLocator;
238cdf0e10cSrcweir
239cdf0e10cSrcweir
240cdf0e10cSrcweir Reference < XAttributeList > rAttrList;
241cdf0e10cSrcweir AttributeList *pAttrList;
242cdf0e10cSrcweir
243cdf0e10cSrcweir // External entity stack
244cdf0e10cSrcweir vector<struct Entity> vecEntity;
pushEntity(const struct Entity & entity)245cdf0e10cSrcweir void pushEntity( const struct Entity &entity )
246cdf0e10cSrcweir { vecEntity.push_back( entity ); }
popEntity()247cdf0e10cSrcweir void popEntity()
248cdf0e10cSrcweir { vecEntity.pop_back( ); }
getEntity()249cdf0e10cSrcweir struct Entity &getEntity()
250cdf0e10cSrcweir { return vecEntity.back(); }
251cdf0e10cSrcweir
252cdf0e10cSrcweir
253cdf0e10cSrcweir // Exception cannot be thrown through the C-XmlParser (possible resource leaks),
254cdf0e10cSrcweir // therefor the exception must be saved somewhere.
255cdf0e10cSrcweir SAXParseException exception;
256cdf0e10cSrcweir RuntimeException rtexception;
257cdf0e10cSrcweir sal_Bool bExceptionWasThrown;
258cdf0e10cSrcweir sal_Bool bRTExceptionWasThrown;
259cdf0e10cSrcweir
260cdf0e10cSrcweir Locale locale;
261cdf0e10cSrcweir
262cdf0e10cSrcweir public:
263cdf0e10cSrcweir // the C-Callbacks for the expat parser
264cdf0e10cSrcweir void static callbackStartElement(void *userData, const XML_Char *name , const XML_Char **atts);
265cdf0e10cSrcweir void static callbackEndElement(void *userData, const XML_Char *name);
266cdf0e10cSrcweir void static callbackCharacters( void *userData , const XML_Char *s , int nLen );
267cdf0e10cSrcweir void static callbackProcessingInstruction( void *userData ,
268cdf0e10cSrcweir const XML_Char *sTarget ,
269cdf0e10cSrcweir const XML_Char *sData );
270cdf0e10cSrcweir
271cdf0e10cSrcweir void static callbackUnparsedEntityDecl( void *userData ,
272cdf0e10cSrcweir const XML_Char *entityName,
273cdf0e10cSrcweir const XML_Char *base,
274cdf0e10cSrcweir const XML_Char *systemId,
275cdf0e10cSrcweir const XML_Char *publicId,
276cdf0e10cSrcweir const XML_Char *notationName);
277cdf0e10cSrcweir
278cdf0e10cSrcweir void static callbackNotationDecl( void *userData,
279cdf0e10cSrcweir const XML_Char *notationName,
280cdf0e10cSrcweir const XML_Char *base,
281cdf0e10cSrcweir const XML_Char *systemId,
282cdf0e10cSrcweir const XML_Char *publicId);
283cdf0e10cSrcweir
284cdf0e10cSrcweir int static callbackExternalEntityRef( XML_Parser parser,
285cdf0e10cSrcweir const XML_Char *openEntityNames,
286cdf0e10cSrcweir const XML_Char *base,
287cdf0e10cSrcweir const XML_Char *systemId,
288cdf0e10cSrcweir const XML_Char *publicId);
289cdf0e10cSrcweir
290cdf0e10cSrcweir int static callbackUnknownEncoding(void *encodingHandlerData,
291cdf0e10cSrcweir const XML_Char *name,
292cdf0e10cSrcweir XML_Encoding *info);
293cdf0e10cSrcweir
294cdf0e10cSrcweir void static callbackDefault( void *userData, const XML_Char *s, int len);
295cdf0e10cSrcweir
296cdf0e10cSrcweir void static callbackStartCDATA( void *userData );
297cdf0e10cSrcweir void static callbackEndCDATA( void *userData );
298cdf0e10cSrcweir void static callbackComment( void *userData , const XML_Char *s );
299cdf0e10cSrcweir void static callErrorHandler( SaxExpatParser_Impl *pImpl , const SAXParseException &e );
300cdf0e10cSrcweir
301cdf0e10cSrcweir public:
302cdf0e10cSrcweir void parse();
303cdf0e10cSrcweir };
304cdf0e10cSrcweir
305cdf0e10cSrcweir extern "C"
306cdf0e10cSrcweir {
call_callbackStartElement(void * userData,const XML_Char * name,const XML_Char ** atts)307cdf0e10cSrcweir static void call_callbackStartElement(void *userData, const XML_Char *name , const XML_Char **atts)
308cdf0e10cSrcweir {
309cdf0e10cSrcweir SaxExpatParser_Impl::callbackStartElement(userData,name,atts);
310cdf0e10cSrcweir }
call_callbackEndElement(void * userData,const XML_Char * name)311cdf0e10cSrcweir static void call_callbackEndElement(void *userData, const XML_Char *name)
312cdf0e10cSrcweir {
313cdf0e10cSrcweir SaxExpatParser_Impl::callbackEndElement(userData,name);
314cdf0e10cSrcweir }
call_callbackCharacters(void * userData,const XML_Char * s,int nLen)315cdf0e10cSrcweir static void call_callbackCharacters( void *userData , const XML_Char *s , int nLen )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir SaxExpatParser_Impl::callbackCharacters(userData,s,nLen);
318cdf0e10cSrcweir }
call_callbackProcessingInstruction(void * userData,const XML_Char * sTarget,const XML_Char * sData)319cdf0e10cSrcweir static void call_callbackProcessingInstruction(void *userData,const XML_Char *sTarget,const XML_Char *sData )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir SaxExpatParser_Impl::callbackProcessingInstruction(userData,sTarget,sData );
322cdf0e10cSrcweir }
call_callbackUnparsedEntityDecl(void * userData,const XML_Char * entityName,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId,const XML_Char * notationName)323cdf0e10cSrcweir static void call_callbackUnparsedEntityDecl(void *userData ,
324cdf0e10cSrcweir const XML_Char *entityName,
325cdf0e10cSrcweir const XML_Char *base,
326cdf0e10cSrcweir const XML_Char *systemId,
327cdf0e10cSrcweir const XML_Char *publicId,
328cdf0e10cSrcweir const XML_Char *notationName)
329cdf0e10cSrcweir {
330cdf0e10cSrcweir SaxExpatParser_Impl::callbackUnparsedEntityDecl(userData,entityName,base,systemId,publicId,notationName);
331cdf0e10cSrcweir }
call_callbackNotationDecl(void * userData,const XML_Char * notationName,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)332cdf0e10cSrcweir static void call_callbackNotationDecl(void *userData,
333cdf0e10cSrcweir const XML_Char *notationName,
334cdf0e10cSrcweir const XML_Char *base,
335cdf0e10cSrcweir const XML_Char *systemId,
336cdf0e10cSrcweir const XML_Char *publicId)
337cdf0e10cSrcweir {
338cdf0e10cSrcweir SaxExpatParser_Impl::callbackNotationDecl(userData,notationName,base,systemId,publicId);
339cdf0e10cSrcweir }
call_callbackExternalEntityRef(XML_Parser parser,const XML_Char * openEntityNames,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)340cdf0e10cSrcweir static int call_callbackExternalEntityRef(XML_Parser parser,
341cdf0e10cSrcweir const XML_Char *openEntityNames,
342cdf0e10cSrcweir const XML_Char *base,
343cdf0e10cSrcweir const XML_Char *systemId,
344cdf0e10cSrcweir const XML_Char *publicId)
345cdf0e10cSrcweir {
346cdf0e10cSrcweir return SaxExpatParser_Impl::callbackExternalEntityRef(parser,openEntityNames,base,systemId,publicId);
347cdf0e10cSrcweir }
call_callbackUnknownEncoding(void * encodingHandlerData,const XML_Char * name,XML_Encoding * info)348cdf0e10cSrcweir static int call_callbackUnknownEncoding(void *encodingHandlerData,
349cdf0e10cSrcweir const XML_Char *name,
350cdf0e10cSrcweir XML_Encoding *info)
351cdf0e10cSrcweir {
352cdf0e10cSrcweir return SaxExpatParser_Impl::callbackUnknownEncoding(encodingHandlerData,name,info);
353cdf0e10cSrcweir }
call_callbackDefault(void * userData,const XML_Char * s,int len)354cdf0e10cSrcweir static void call_callbackDefault( void *userData, const XML_Char *s, int len)
355cdf0e10cSrcweir {
356cdf0e10cSrcweir SaxExpatParser_Impl::callbackDefault(userData,s,len);
357cdf0e10cSrcweir }
call_callbackStartCDATA(void * userData)358cdf0e10cSrcweir static void call_callbackStartCDATA( void *userData )
359cdf0e10cSrcweir {
360cdf0e10cSrcweir SaxExpatParser_Impl::callbackStartCDATA(userData);
361cdf0e10cSrcweir }
call_callbackEndCDATA(void * userData)362cdf0e10cSrcweir static void call_callbackEndCDATA( void *userData )
363cdf0e10cSrcweir {
364cdf0e10cSrcweir SaxExpatParser_Impl::callbackEndCDATA(userData);
365cdf0e10cSrcweir }
call_callbackComment(void * userData,const XML_Char * s)366cdf0e10cSrcweir static void call_callbackComment( void *userData , const XML_Char *s )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir SaxExpatParser_Impl::callbackComment(userData,s);
369cdf0e10cSrcweir }
370cdf0e10cSrcweir }
371cdf0e10cSrcweir
372cdf0e10cSrcweir
373cdf0e10cSrcweir //---------------------------------------------
374cdf0e10cSrcweir // LocatorImpl
375cdf0e10cSrcweir //---------------------------------------------
376cdf0e10cSrcweir class LocatorImpl :
377cdf0e10cSrcweir public WeakImplHelper2< XLocator, com::sun::star::io::XSeekable >
378cdf0e10cSrcweir // should use a different interface for stream positions!
379cdf0e10cSrcweir {
380cdf0e10cSrcweir public:
LocatorImpl(SaxExpatParser_Impl * p)381cdf0e10cSrcweir LocatorImpl( SaxExpatParser_Impl *p )
382cdf0e10cSrcweir {
383cdf0e10cSrcweir m_pParser = p;
384cdf0e10cSrcweir }
385cdf0e10cSrcweir
386cdf0e10cSrcweir public: //XLocator
getColumnNumber(void)387cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getColumnNumber(void) throw ()
388cdf0e10cSrcweir {
389cdf0e10cSrcweir return XML_GetCurrentColumnNumber( m_pParser->getEntity().pParser );
390cdf0e10cSrcweir }
getLineNumber(void)391cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getLineNumber(void) throw ()
392cdf0e10cSrcweir {
393cdf0e10cSrcweir return XML_GetCurrentLineNumber( m_pParser->getEntity().pParser );
394cdf0e10cSrcweir }
getPublicId(void)395cdf0e10cSrcweir virtual OUString SAL_CALL getPublicId(void) throw ()
396cdf0e10cSrcweir {
397cdf0e10cSrcweir return m_pParser->getEntity().structSource.sPublicId;
398cdf0e10cSrcweir }
getSystemId(void)399cdf0e10cSrcweir virtual OUString SAL_CALL getSystemId(void) throw ()
400cdf0e10cSrcweir {
401cdf0e10cSrcweir return m_pParser->getEntity().structSource.sSystemId;
402cdf0e10cSrcweir }
403cdf0e10cSrcweir
404cdf0e10cSrcweir // XSeekable (only for getPosition)
405cdf0e10cSrcweir
seek(sal_Int64)406cdf0e10cSrcweir virtual void SAL_CALL seek( sal_Int64 ) throw()
407cdf0e10cSrcweir {
408cdf0e10cSrcweir }
getPosition()409cdf0e10cSrcweir virtual sal_Int64 SAL_CALL getPosition() throw()
410cdf0e10cSrcweir {
411cdf0e10cSrcweir return XML_GetCurrentByteIndex( m_pParser->getEntity().pParser );
412cdf0e10cSrcweir }
getLength()413cdf0e10cSrcweir virtual ::sal_Int64 SAL_CALL getLength() throw()
414cdf0e10cSrcweir {
415cdf0e10cSrcweir return 0;
416cdf0e10cSrcweir }
417cdf0e10cSrcweir
418cdf0e10cSrcweir private:
419cdf0e10cSrcweir
420cdf0e10cSrcweir SaxExpatParser_Impl *m_pParser;
421cdf0e10cSrcweir };
422cdf0e10cSrcweir
423cdf0e10cSrcweir
424cdf0e10cSrcweir
425cdf0e10cSrcweir
SaxExpatParser()426cdf0e10cSrcweir SaxExpatParser::SaxExpatParser( )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir m_pImpl = new SaxExpatParser_Impl;
429cdf0e10cSrcweir
430cdf0e10cSrcweir LocatorImpl *pLoc = new LocatorImpl( m_pImpl );
431cdf0e10cSrcweir m_pImpl->rDocumentLocator = Reference< XLocator > ( pLoc );
432cdf0e10cSrcweir
433cdf0e10cSrcweir // performance-Improvment. Reference is needed when calling the startTag callback.
434cdf0e10cSrcweir // Handing out the same object with every call is allowed (see sax-specification)
435cdf0e10cSrcweir m_pImpl->pAttrList = new AttributeList;
436cdf0e10cSrcweir m_pImpl->rAttrList = Reference< XAttributeList > ( m_pImpl->pAttrList );
437cdf0e10cSrcweir
438cdf0e10cSrcweir m_pImpl->bExceptionWasThrown = sal_False;
439cdf0e10cSrcweir m_pImpl->bRTExceptionWasThrown = sal_False;
440cdf0e10cSrcweir }
441cdf0e10cSrcweir
~SaxExpatParser()442cdf0e10cSrcweir SaxExpatParser::~SaxExpatParser()
443cdf0e10cSrcweir {
444cdf0e10cSrcweir delete m_pImpl;
445cdf0e10cSrcweir }
446cdf0e10cSrcweir
447cdf0e10cSrcweir
448cdf0e10cSrcweir /***************
449cdf0e10cSrcweir *
450cdf0e10cSrcweir * parseStream does Parser-startup initializations. The SaxExpatParser_Impl::parse() method does
451cdf0e10cSrcweir * the file-specific initialization work. (During a parser run, external files may be opened)
452cdf0e10cSrcweir *
453cdf0e10cSrcweir ****************/
parseStream(const InputSource & structSource)454cdf0e10cSrcweir void SaxExpatParser::parseStream( const InputSource& structSource)
455cdf0e10cSrcweir throw (SAXException,
456cdf0e10cSrcweir IOException,
457cdf0e10cSrcweir RuntimeException)
458cdf0e10cSrcweir {
459cdf0e10cSrcweir // Only one text at one time
460cdf0e10cSrcweir MutexGuard guard( m_pImpl->aMutex );
461cdf0e10cSrcweir
462cdf0e10cSrcweir
463cdf0e10cSrcweir struct Entity entity;
464cdf0e10cSrcweir entity.structSource = structSource;
465cdf0e10cSrcweir
466cdf0e10cSrcweir if( ! entity.structSource.aInputStream.is() )
467cdf0e10cSrcweir {
468cdf0e10cSrcweir throw SAXException( OUString::createFromAscii( "No input source" ) ,
469cdf0e10cSrcweir Reference< XInterface > () , Any() );
470cdf0e10cSrcweir }
471cdf0e10cSrcweir
472cdf0e10cSrcweir entity.converter.setInputStream( entity.structSource.aInputStream );
473cdf0e10cSrcweir if( entity.structSource.sEncoding.getLength() )
474cdf0e10cSrcweir {
475cdf0e10cSrcweir entity.converter.setEncoding(
476cdf0e10cSrcweir OUStringToOString( entity.structSource.sEncoding , RTL_TEXTENCODING_ASCII_US ) );
477cdf0e10cSrcweir }
478cdf0e10cSrcweir
479cdf0e10cSrcweir // create parser with proper encoding
480cdf0e10cSrcweir entity.pParser = XML_ParserCreate( 0 );
481cdf0e10cSrcweir if( ! entity.pParser )
482cdf0e10cSrcweir {
483cdf0e10cSrcweir throw SAXException( OUString::createFromAscii( "Couldn't create parser" ) ,
484cdf0e10cSrcweir Reference< XInterface > (), Any() );
485cdf0e10cSrcweir }
486cdf0e10cSrcweir
487cdf0e10cSrcweir // set all necessary C-Callbacks
488cdf0e10cSrcweir XML_SetUserData( entity.pParser , m_pImpl );
489cdf0e10cSrcweir XML_SetElementHandler( entity.pParser ,
490cdf0e10cSrcweir call_callbackStartElement ,
491cdf0e10cSrcweir call_callbackEndElement );
492cdf0e10cSrcweir XML_SetCharacterDataHandler( entity.pParser , call_callbackCharacters );
493cdf0e10cSrcweir XML_SetProcessingInstructionHandler(entity.pParser ,
494cdf0e10cSrcweir call_callbackProcessingInstruction );
495cdf0e10cSrcweir XML_SetUnparsedEntityDeclHandler( entity.pParser,
496cdf0e10cSrcweir call_callbackUnparsedEntityDecl );
497cdf0e10cSrcweir XML_SetNotationDeclHandler( entity.pParser, call_callbackNotationDecl );
498cdf0e10cSrcweir XML_SetExternalEntityRefHandler( entity.pParser,
499cdf0e10cSrcweir call_callbackExternalEntityRef);
500cdf0e10cSrcweir XML_SetUnknownEncodingHandler( entity.pParser, call_callbackUnknownEncoding ,0);
501cdf0e10cSrcweir
502cdf0e10cSrcweir if( m_pImpl->rExtendedDocumentHandler.is() ) {
503cdf0e10cSrcweir
504cdf0e10cSrcweir // These handlers just delegate calls to the ExtendedHandler. If no extended handler is
505cdf0e10cSrcweir // given, these callbacks can be ignored
506cdf0e10cSrcweir XML_SetDefaultHandlerExpand( entity.pParser, call_callbackDefault );
507cdf0e10cSrcweir XML_SetCommentHandler( entity.pParser, call_callbackComment );
508cdf0e10cSrcweir XML_SetCdataSectionHandler( entity.pParser ,
509cdf0e10cSrcweir call_callbackStartCDATA ,
510cdf0e10cSrcweir call_callbackEndCDATA );
511cdf0e10cSrcweir }
512cdf0e10cSrcweir
513cdf0e10cSrcweir
514cdf0e10cSrcweir m_pImpl->exception = SAXParseException();
515cdf0e10cSrcweir m_pImpl->pushEntity( entity );
516cdf0e10cSrcweir try
517cdf0e10cSrcweir {
518cdf0e10cSrcweir // start the document
519cdf0e10cSrcweir if( m_pImpl->rDocumentHandler.is() ) {
520cdf0e10cSrcweir m_pImpl->rDocumentHandler->setDocumentLocator( m_pImpl->rDocumentLocator );
521cdf0e10cSrcweir m_pImpl->rDocumentHandler->startDocument();
522cdf0e10cSrcweir }
523cdf0e10cSrcweir
524cdf0e10cSrcweir m_pImpl->parse();
525cdf0e10cSrcweir
526cdf0e10cSrcweir // finish document
527cdf0e10cSrcweir if( m_pImpl->rDocumentHandler.is() ) {
528cdf0e10cSrcweir m_pImpl->rDocumentHandler->endDocument();
529cdf0e10cSrcweir }
530cdf0e10cSrcweir }
531cdf0e10cSrcweir // catch( SAXParseException &e )
532cdf0e10cSrcweir // {
533cdf0e10cSrcweir // m_pImpl->popEntity();
534cdf0e10cSrcweir // XML_ParserFree( entity.pParser );
535cdf0e10cSrcweir // Any aAny;
536cdf0e10cSrcweir // aAny <<= e;
537cdf0e10cSrcweir // throw SAXException( e.Message, e.Context, aAny );
538cdf0e10cSrcweir // }
539cdf0e10cSrcweir catch( SAXException & )
540cdf0e10cSrcweir {
541cdf0e10cSrcweir m_pImpl->popEntity();
542cdf0e10cSrcweir XML_ParserFree( entity.pParser );
543cdf0e10cSrcweir throw;
544cdf0e10cSrcweir }
545cdf0e10cSrcweir catch( IOException & )
546cdf0e10cSrcweir {
547cdf0e10cSrcweir m_pImpl->popEntity();
548cdf0e10cSrcweir XML_ParserFree( entity.pParser );
549cdf0e10cSrcweir throw;
550cdf0e10cSrcweir }
551cdf0e10cSrcweir catch( RuntimeException & )
552cdf0e10cSrcweir {
553cdf0e10cSrcweir m_pImpl->popEntity();
554cdf0e10cSrcweir XML_ParserFree( entity.pParser );
555cdf0e10cSrcweir throw;
556cdf0e10cSrcweir }
557cdf0e10cSrcweir
558cdf0e10cSrcweir m_pImpl->popEntity();
559cdf0e10cSrcweir XML_ParserFree( entity.pParser );
560cdf0e10cSrcweir }
561cdf0e10cSrcweir
setDocumentHandler(const Reference<XDocumentHandler> & xHandler)562cdf0e10cSrcweir void SaxExpatParser::setDocumentHandler(const Reference< XDocumentHandler > & xHandler)
563cdf0e10cSrcweir throw (RuntimeException)
564cdf0e10cSrcweir {
565cdf0e10cSrcweir m_pImpl->rDocumentHandler = xHandler;
566cdf0e10cSrcweir m_pImpl->rExtendedDocumentHandler =
567cdf0e10cSrcweir Reference< XExtendedDocumentHandler >( xHandler , UNO_QUERY );
568cdf0e10cSrcweir }
569cdf0e10cSrcweir
setErrorHandler(const Reference<XErrorHandler> & xHandler)570cdf0e10cSrcweir void SaxExpatParser::setErrorHandler(const Reference< XErrorHandler > & xHandler)
571cdf0e10cSrcweir throw (RuntimeException)
572cdf0e10cSrcweir {
573cdf0e10cSrcweir m_pImpl->rErrorHandler = xHandler;
574cdf0e10cSrcweir }
575cdf0e10cSrcweir
setDTDHandler(const Reference<XDTDHandler> & xHandler)576cdf0e10cSrcweir void SaxExpatParser::setDTDHandler(const Reference< XDTDHandler > & xHandler)
577cdf0e10cSrcweir throw (RuntimeException)
578cdf0e10cSrcweir {
579cdf0e10cSrcweir m_pImpl->rDTDHandler = xHandler;
580cdf0e10cSrcweir }
581cdf0e10cSrcweir
setEntityResolver(const Reference<XEntityResolver> & xResolver)582cdf0e10cSrcweir void SaxExpatParser::setEntityResolver(const Reference < XEntityResolver > & xResolver)
583cdf0e10cSrcweir throw (RuntimeException)
584cdf0e10cSrcweir {
585cdf0e10cSrcweir m_pImpl->rEntityResolver = xResolver;
586cdf0e10cSrcweir }
587cdf0e10cSrcweir
588cdf0e10cSrcweir
setLocale(const Locale & locale)589cdf0e10cSrcweir void SaxExpatParser::setLocale( const Locale & locale ) throw (RuntimeException)
590cdf0e10cSrcweir {
591cdf0e10cSrcweir m_pImpl->locale = locale;
592cdf0e10cSrcweir }
593cdf0e10cSrcweir
594cdf0e10cSrcweir // XServiceInfo
getImplementationName()595cdf0e10cSrcweir OUString SaxExpatParser::getImplementationName() throw ()
596cdf0e10cSrcweir {
597*85ec52e3SDamjan Jovanovic return getImplementationName_Static();
598cdf0e10cSrcweir }
599cdf0e10cSrcweir
600cdf0e10cSrcweir // XServiceInfo
supportsService(const OUString & ServiceName)601cdf0e10cSrcweir sal_Bool SaxExpatParser::supportsService(const OUString& ServiceName) throw ()
602cdf0e10cSrcweir {
603cdf0e10cSrcweir Sequence< OUString > aSNL = getSupportedServiceNames();
604cdf0e10cSrcweir const OUString * pArray = aSNL.getConstArray();
605cdf0e10cSrcweir
606cdf0e10cSrcweir for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
607cdf0e10cSrcweir if( pArray[i] == ServiceName )
608cdf0e10cSrcweir return sal_True;
609cdf0e10cSrcweir
610cdf0e10cSrcweir return sal_False;
611cdf0e10cSrcweir }
612cdf0e10cSrcweir
613cdf0e10cSrcweir // XServiceInfo
getSupportedServiceNames(void)614cdf0e10cSrcweir Sequence< OUString > SaxExpatParser::getSupportedServiceNames(void) throw ()
615cdf0e10cSrcweir {
616*85ec52e3SDamjan Jovanovic return getSupportedServiceNames_Static();
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
1024*85ec52e3SDamjan Jovanovic
1025*85ec52e3SDamjan Jovanovic static struct ::cppu::ImplementationEntry g_component_entries[] =
1026*85ec52e3SDamjan Jovanovic {
1027*85ec52e3SDamjan Jovanovic {
1028*85ec52e3SDamjan Jovanovic SaxExpatParser_CreateInstance,
1029*85ec52e3SDamjan Jovanovic SaxExpatParser::getImplementationName_Static,
1030*85ec52e3SDamjan Jovanovic SaxExpatParser::getSupportedServiceNames_Static,
1031*85ec52e3SDamjan Jovanovic ::cppu::createSingleComponentFactory,
1032*85ec52e3SDamjan Jovanovic 0,
1033*85ec52e3SDamjan Jovanovic 0
1034*85ec52e3SDamjan Jovanovic },
1035*85ec52e3SDamjan Jovanovic {
1036*85ec52e3SDamjan Jovanovic SaxWriter_CreateInstance,
1037*85ec52e3SDamjan Jovanovic SaxWriter_getImplementationName,
1038*85ec52e3SDamjan Jovanovic SaxWriter_getSupportedServiceNames,
1039*85ec52e3SDamjan Jovanovic ::cppu::createSingleComponentFactory,
1040*85ec52e3SDamjan Jovanovic 0,
1041*85ec52e3SDamjan Jovanovic 0
1042*85ec52e3SDamjan Jovanovic },
1043*85ec52e3SDamjan Jovanovic { 0, 0, 0, 0, 0, 0 }
1044*85ec52e3SDamjan Jovanovic };
1045*85ec52e3SDamjan Jovanovic
1046cdf0e10cSrcweir extern "C"
1047cdf0e10cSrcweir {
1048cdf0e10cSrcweir
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)1049b63233d8Sdamjan SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
1050cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
1051cdf0e10cSrcweir {
1052cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
1053cdf0e10cSrcweir }
1054cdf0e10cSrcweir
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)1055b63233d8Sdamjan SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
1056*85ec52e3SDamjan Jovanovic const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
1057cdf0e10cSrcweir {
1058*85ec52e3SDamjan Jovanovic return ::cppu::component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey, g_component_entries );
1059cdf0e10cSrcweir }
1060cdf0e10cSrcweir
1061cdf0e10cSrcweir
1062cdf0e10cSrcweir }
1063