xref: /AOO41X/main/sax/test/saxdemo.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir //------------------------------------------------------
29*cdf0e10cSrcweir // testcomponent - Loads a service and its testcomponent from dlls performs a test.
30*cdf0e10cSrcweir // Expands the dll-names depending on the actual environment.
31*cdf0e10cSrcweir // Example : testcomponent stardiv.uno.io.Pipe stm
32*cdf0e10cSrcweir //
33*cdf0e10cSrcweir // Therefor the testcode must exist in teststm and the testservice must be named test.stardiv.uno.io.Pipe
34*cdf0e10cSrcweir //
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <stdio.h>
37*cdf0e10cSrcweir #include <vector>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
50*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
51*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <vos/dynload.hxx>
54*cdf0e10cSrcweir #include <vos/diagnose.hxx>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using namespace ::rtl;
57*cdf0e10cSrcweir using namespace ::std;
58*cdf0e10cSrcweir using namespace ::cppu;
59*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
60*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
61*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
62*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
63*cdf0e10cSrcweir using namespace ::com::sun::star::io;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir /************
67*cdf0e10cSrcweir  * Sequence of bytes -> InputStream
68*cdf0e10cSrcweir  ************/
69*cdf0e10cSrcweir class OInputStream : public WeakImplHelper1 < XInputStream >
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir public:
72*cdf0e10cSrcweir 	OInputStream( const Sequence< sal_Int8 >&seq ) :
73*cdf0e10cSrcweir 		m_seq( seq ),
74*cdf0e10cSrcweir 		nPos( 0 )
75*cdf0e10cSrcweir 		{}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir public:
78*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
79*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
80*cdf0e10cSrcweir 		{
81*cdf0e10cSrcweir 			nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
82*cdf0e10cSrcweir 				m_seq.getLength() - nPos :
83*cdf0e10cSrcweir 				nBytesToRead;
84*cdf0e10cSrcweir 			aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
85*cdf0e10cSrcweir 			nPos += nBytesToRead;
86*cdf0e10cSrcweir 			return nBytesToRead;
87*cdf0e10cSrcweir 		}
88*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL readSomeBytes(
89*cdf0e10cSrcweir 		::com::sun::star::uno::Sequence< sal_Int8 >& aData,
90*cdf0e10cSrcweir 		sal_Int32 nMaxBytesToRead )
91*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
92*cdf0e10cSrcweir 		{
93*cdf0e10cSrcweir 			return readBytes( aData, nMaxBytesToRead );
94*cdf0e10cSrcweir 		}
95*cdf0e10cSrcweir     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
96*cdf0e10cSrcweir 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
97*cdf0e10cSrcweir 		{
98*cdf0e10cSrcweir 			// not implemented
99*cdf0e10cSrcweir 		}
100*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL available(  )
101*cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
102*cdf0e10cSrcweir 		{
103*cdf0e10cSrcweir 			return m_seq.getLength() - nPos;
104*cdf0e10cSrcweir 		}
105*cdf0e10cSrcweir     virtual void SAL_CALL closeInput(  )
106*cdf0e10cSrcweir 		throw(NotConnectedException, IOException, RuntimeException)
107*cdf0e10cSrcweir 		{
108*cdf0e10cSrcweir 			// not needed
109*cdf0e10cSrcweir 		}
110*cdf0e10cSrcweir 	sal_Int32 nPos;
111*cdf0e10cSrcweir 	Sequence< sal_Int8> m_seq;
112*cdf0e10cSrcweir };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir //-------------------------------
115*cdf0e10cSrcweir // Helper : create an input stream from a file
116*cdf0e10cSrcweir //------------------------------
117*cdf0e10cSrcweir Reference< XInputStream > createStreamFromFile(
118*cdf0e10cSrcweir 	const char *pcFile )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir 	FILE *f = fopen( pcFile , "rb" );
121*cdf0e10cSrcweir 	Reference<  XInputStream >  r;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 	if( f ) {
124*cdf0e10cSrcweir 		fseek( f , 0 , SEEK_END );
125*cdf0e10cSrcweir 		int nLength = ftell( f );
126*cdf0e10cSrcweir 		fseek( f , 0 , SEEK_SET );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 		Sequence<sal_Int8> seqIn(nLength);
129*cdf0e10cSrcweir 		fread( seqIn.getArray() , nLength , 1 , f );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 		r = Reference< XInputStream > ( new OInputStream( seqIn ) );
132*cdf0e10cSrcweir 		fclose( f );
133*cdf0e10cSrcweir 	}
134*cdf0e10cSrcweir 	return r;
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir //-----------------------------------------
138*cdf0e10cSrcweir // The document handler, which is needed for the saxparser
139*cdf0e10cSrcweir // The Documenthandler for reading sax
140*cdf0e10cSrcweir //-----------------------------------------
141*cdf0e10cSrcweir class TestDocumentHandler :
142*cdf0e10cSrcweir 	public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir public:
145*cdf0e10cSrcweir 	TestDocumentHandler(  )
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 	}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir public: // Error handler
150*cdf0e10cSrcweir     virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException)
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir     	printf( "Error !\n" );
153*cdf0e10cSrcweir     	throw  SAXException(
154*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("error from error handler")) ,
155*cdf0e10cSrcweir 			Reference < XInterface >() ,
156*cdf0e10cSrcweir 			aSAXParseException );
157*cdf0e10cSrcweir     }
158*cdf0e10cSrcweir     virtual void SAL_CALL fatalError(const Any& aSAXParseException) throw (SAXException, RuntimeException)
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir     	printf( "Fatal Error !\n" );
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir     virtual void SAL_CALL warning(const Any& aSAXParseException) throw (SAXException, RuntimeException)
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir     	printf( "Warning !\n" );
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir public: // ExtendedDocumentHandler
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     virtual void SAL_CALL startDocument(void) throw (SAXException, RuntimeException)
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir 		m_iElementCount = 0;
173*cdf0e10cSrcweir 		m_iAttributeCount = 0;
174*cdf0e10cSrcweir 		m_iWhitespaceCount =0;
175*cdf0e10cSrcweir 		m_iCharCount=0;
176*cdf0e10cSrcweir    		printf( "document started\n" );
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir     virtual void SAL_CALL endDocument(void) throw (SAXException, RuntimeException)
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir 		printf( "document finished\n" );
181*cdf0e10cSrcweir 		printf( "(ElementCount %d),(AttributeCount %d),(WhitespaceCount %d),(CharCount %d)\n",
182*cdf0e10cSrcweir 				m_iElementCount, m_iAttributeCount, m_iWhitespaceCount , m_iCharCount );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir     virtual void SAL_CALL startElement(const OUString& aName,
186*cdf0e10cSrcweir 							  const Reference< XAttributeList > & xAttribs)
187*cdf0e10cSrcweir 		throw (SAXException,RuntimeException)
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir     	m_iElementCount ++;
190*cdf0e10cSrcweir     	m_iAttributeCount += xAttribs->getLength();
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     virtual void SAL_CALL endElement(const OUString& aName) throw (SAXException,RuntimeException)
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir 		// ignored
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException)
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir     	m_iCharCount += aChars.getLength();
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir     virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces) throw (SAXException,RuntimeException)
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir 		m_iWhitespaceCount += aWhitespaces.getLength();
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     virtual void SAL_CALL processingInstruction(const OUString& aTarget, const OUString& aData) throw (SAXException,RuntimeException)
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir 		// ignored
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & xLocator)
213*cdf0e10cSrcweir 		throw (SAXException,RuntimeException)
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir 		// ignored
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     virtual InputSource SAL_CALL resolveEntity(
219*cdf0e10cSrcweir 		const OUString& sPublicId,
220*cdf0e10cSrcweir 		const OUString& sSystemId)
221*cdf0e10cSrcweir 		throw (SAXException,RuntimeException)
222*cdf0e10cSrcweir 	{
223*cdf0e10cSrcweir 		InputSource source;
224*cdf0e10cSrcweir 		source.sSystemId = sSystemId;
225*cdf0e10cSrcweir 		source.sPublicId = sPublicId;
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 		source.aInputStream = createStreamFromFile(
228*cdf0e10cSrcweir 			OUStringToOString( sSystemId , RTL_TEXTENCODING_ASCII_US) );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 		return source;
231*cdf0e10cSrcweir 	}
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     virtual void SAL_CALL startCDATA(void) throw (SAXException,RuntimeException)
234*cdf0e10cSrcweir     {
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir     virtual void SAL_CALL endCDATA(void) throw (SAXException,RuntimeException)
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir     virtual void SAL_CALL comment(const OUString& sComment) throw (SAXException,RuntimeException)
240*cdf0e10cSrcweir     {
241*cdf0e10cSrcweir     }
242*cdf0e10cSrcweir     virtual void SAL_CALL unknown(const OUString& sString) throw (SAXException,RuntimeException)
243*cdf0e10cSrcweir     {
244*cdf0e10cSrcweir     }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 	virtual void SAL_CALL allowLineBreak( void) throw (SAXException, RuntimeException )
247*cdf0e10cSrcweir 	{
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	}
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir public:
252*cdf0e10cSrcweir 	int m_iElementCount;
253*cdf0e10cSrcweir 	int m_iAttributeCount;
254*cdf0e10cSrcweir 	int m_iWhitespaceCount;
255*cdf0e10cSrcweir 	int m_iCharCount;
256*cdf0e10cSrcweir };
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir //--------------------------------------
259*cdf0e10cSrcweir // helper implementation for writing
260*cdf0e10cSrcweir // implements an XAttributeList
261*cdf0e10cSrcweir //-------------------------------------
262*cdf0e10cSrcweir struct AttributeListImpl_impl;
263*cdf0e10cSrcweir class AttributeListImpl : public WeakImplHelper1< XAttributeList >
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir public:
266*cdf0e10cSrcweir 	AttributeListImpl();
267*cdf0e10cSrcweir 	AttributeListImpl( const AttributeListImpl & );
268*cdf0e10cSrcweir 	~AttributeListImpl();
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir public:
271*cdf0e10cSrcweir     virtual sal_Int16 SAL_CALL getLength(void) throw  (RuntimeException);
272*cdf0e10cSrcweir     virtual OUString SAL_CALL getNameByIndex(sal_Int16 i) throw  (RuntimeException);
273*cdf0e10cSrcweir     virtual OUString SAL_CALL getTypeByIndex(sal_Int16 i) throw  (RuntimeException);
274*cdf0e10cSrcweir     virtual OUString SAL_CALL getTypeByName(const OUString& aName) throw  (RuntimeException);
275*cdf0e10cSrcweir     virtual OUString SAL_CALL getValueByIndex(sal_Int16 i) throw  (RuntimeException);
276*cdf0e10cSrcweir     virtual OUString SAL_CALL getValueByName(const OUString& aName) throw  (RuntimeException);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir public:
279*cdf0e10cSrcweir 	void addAttribute( const OUString &sName ,
280*cdf0e10cSrcweir 					   const OUString &sType ,
281*cdf0e10cSrcweir 					   const OUString &sValue );
282*cdf0e10cSrcweir 	void clear();
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir private:
285*cdf0e10cSrcweir 	struct AttributeListImpl_impl *m_pImpl;
286*cdf0e10cSrcweir };
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir struct TagAttribute
290*cdf0e10cSrcweir {
291*cdf0e10cSrcweir 	TagAttribute(){}
292*cdf0e10cSrcweir 	TagAttribute( const OUString &sName,
293*cdf0e10cSrcweir 				  const OUString &sType ,
294*cdf0e10cSrcweir 				  const OUString &sValue )
295*cdf0e10cSrcweir 	{
296*cdf0e10cSrcweir 		this->sName 	= sName;
297*cdf0e10cSrcweir 		this->sType 	= sType;
298*cdf0e10cSrcweir 		this->sValue 	= sValue;
299*cdf0e10cSrcweir 	}
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 	OUString sName;
302*cdf0e10cSrcweir 	OUString sType;
303*cdf0e10cSrcweir 	OUString sValue;
304*cdf0e10cSrcweir };
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir struct AttributeListImpl_impl
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir 	AttributeListImpl_impl()
309*cdf0e10cSrcweir 	{
310*cdf0e10cSrcweir 		// performance improvement during adding
311*cdf0e10cSrcweir 		vecAttribute.reserve(20);
312*cdf0e10cSrcweir 	}
313*cdf0e10cSrcweir 	vector<struct TagAttribute> vecAttribute;
314*cdf0e10cSrcweir };
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir sal_Int16 AttributeListImpl::getLength(void) throw  (RuntimeException)
319*cdf0e10cSrcweir {
320*cdf0e10cSrcweir 	return m_pImpl->vecAttribute.size();
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir AttributeListImpl::AttributeListImpl( const AttributeListImpl &r )
325*cdf0e10cSrcweir {
326*cdf0e10cSrcweir 	m_pImpl = new AttributeListImpl_impl;
327*cdf0e10cSrcweir 	*m_pImpl = *(r.m_pImpl);
328*cdf0e10cSrcweir }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir OUString AttributeListImpl::getNameByIndex(sal_Int16 i) throw  (RuntimeException)
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir 	if( i < m_pImpl->vecAttribute.size() ) {
333*cdf0e10cSrcweir 		return m_pImpl->vecAttribute[i].sName;
334*cdf0e10cSrcweir 	}
335*cdf0e10cSrcweir 	return OUString();
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir OUString AttributeListImpl::getTypeByIndex(sal_Int16 i) throw  (RuntimeException)
340*cdf0e10cSrcweir {
341*cdf0e10cSrcweir 	if( i < m_pImpl->vecAttribute.size() ) {
342*cdf0e10cSrcweir 		return m_pImpl->vecAttribute[i].sType;
343*cdf0e10cSrcweir 	}
344*cdf0e10cSrcweir 	return OUString();
345*cdf0e10cSrcweir }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir OUString AttributeListImpl::getValueByIndex(sal_Int16 i) throw  (RuntimeException)
348*cdf0e10cSrcweir {
349*cdf0e10cSrcweir 	if( i < m_pImpl->vecAttribute.size() ) {
350*cdf0e10cSrcweir 		return m_pImpl->vecAttribute[i].sValue;
351*cdf0e10cSrcweir 	}
352*cdf0e10cSrcweir 	return OUString();
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir OUString AttributeListImpl::getTypeByName( const OUString& sName ) throw  (RuntimeException)
357*cdf0e10cSrcweir {
358*cdf0e10cSrcweir 	vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
361*cdf0e10cSrcweir 		if( (*ii).sName == sName ) {
362*cdf0e10cSrcweir 			return (*ii).sType;
363*cdf0e10cSrcweir 		}
364*cdf0e10cSrcweir 	}
365*cdf0e10cSrcweir 	return OUString();
366*cdf0e10cSrcweir }
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir OUString AttributeListImpl::getValueByName(const OUString& sName) throw  (RuntimeException)
369*cdf0e10cSrcweir {
370*cdf0e10cSrcweir 	vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir 	for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
373*cdf0e10cSrcweir 		if( (*ii).sName == sName ) {
374*cdf0e10cSrcweir 			return (*ii).sValue;
375*cdf0e10cSrcweir 		}
376*cdf0e10cSrcweir 	}
377*cdf0e10cSrcweir 	return OUString();
378*cdf0e10cSrcweir }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir AttributeListImpl::AttributeListImpl()
383*cdf0e10cSrcweir {
384*cdf0e10cSrcweir 	m_pImpl = new AttributeListImpl_impl;
385*cdf0e10cSrcweir }
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir AttributeListImpl::~AttributeListImpl()
390*cdf0e10cSrcweir {
391*cdf0e10cSrcweir 	delete m_pImpl;
392*cdf0e10cSrcweir }
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir void AttributeListImpl::addAttribute( 	const OUString &sName ,
396*cdf0e10cSrcweir 										const OUString &sType ,
397*cdf0e10cSrcweir 										const OUString &sValue )
398*cdf0e10cSrcweir {
399*cdf0e10cSrcweir 	m_pImpl->vecAttribute.push_back( TagAttribute( sName , sType , sValue ) );
400*cdf0e10cSrcweir }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir void AttributeListImpl::clear()
403*cdf0e10cSrcweir {
404*cdf0e10cSrcweir 	m_pImpl->vecAttribute.clear();
405*cdf0e10cSrcweir }
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir //--------------------------------------
409*cdf0e10cSrcweir // helper function for writing
410*cdf0e10cSrcweir // ensures that linebreaks are inserted
411*cdf0e10cSrcweir // when writing a long text.
412*cdf0e10cSrcweir // Note: this implementation may be a bit slow,
413*cdf0e10cSrcweir // but it shows, how the SAX-Writer handles the allowLineBreak calls.
414*cdf0e10cSrcweir //--------------------------------------
415*cdf0e10cSrcweir void writeParagraphHelper(
416*cdf0e10cSrcweir 	const  Reference< XExtendedDocumentHandler > &r ,
417*cdf0e10cSrcweir 	const OUString & s)
418*cdf0e10cSrcweir {
419*cdf0e10cSrcweir 	int nMax = s.getLength();
420*cdf0e10cSrcweir 	int nStart = 0;
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir 	Sequence<sal_uInt16> seq( s.getLength() );
423*cdf0e10cSrcweir 	memcpy( seq.getArray() , s.getStr() , s.getLength() * sizeof( sal_uInt16 ) );
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir 	for( int n = 1 ; n < nMax ; n++ ){
426*cdf0e10cSrcweir 		if( 32 == seq.getArray()[n] ) {
427*cdf0e10cSrcweir 			r->allowLineBreak();
428*cdf0e10cSrcweir 			r->characters( s.copy( nStart , n - nStart ) );
429*cdf0e10cSrcweir 			nStart = n;
430*cdf0e10cSrcweir 		}
431*cdf0e10cSrcweir 	}
432*cdf0e10cSrcweir 	r->allowLineBreak();
433*cdf0e10cSrcweir 	r->characters( s.copy( nStart , n - nStart ) );
434*cdf0e10cSrcweir }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 
437*cdf0e10cSrcweir //---------------------------------
438*cdf0e10cSrcweir // helper implementation for SAX-Writer
439*cdf0e10cSrcweir // writes data to a file
440*cdf0e10cSrcweir //--------------------------------
441*cdf0e10cSrcweir class OFileWriter :
442*cdf0e10cSrcweir 		public WeakImplHelper1< XOutputStream >
443*cdf0e10cSrcweir {
444*cdf0e10cSrcweir public:
445*cdf0e10cSrcweir 	OFileWriter( char *pcFile ) { strncpy( m_pcFile , pcFile, 256 - 1 ); m_f = 0; }
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir public:
449*cdf0e10cSrcweir     virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData)
450*cdf0e10cSrcweir 		throw  (NotConnectedException, BufferSizeExceededException, RuntimeException);
451*cdf0e10cSrcweir     virtual void SAL_CALL flush(void)
452*cdf0e10cSrcweir 		throw  (NotConnectedException, BufferSizeExceededException, RuntimeException);
453*cdf0e10cSrcweir     virtual void SAL_CALL closeOutput(void)
454*cdf0e10cSrcweir 		throw  (NotConnectedException, BufferSizeExceededException, RuntimeException);
455*cdf0e10cSrcweir private:
456*cdf0e10cSrcweir 	char m_pcFile[256];
457*cdf0e10cSrcweir 	FILE *m_f;
458*cdf0e10cSrcweir };
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir void OFileWriter::writeBytes(const Sequence< sal_Int8 >& aData)
462*cdf0e10cSrcweir 	throw  (NotConnectedException, BufferSizeExceededException, RuntimeException)
463*cdf0e10cSrcweir {
464*cdf0e10cSrcweir 	if( ! m_f ) {
465*cdf0e10cSrcweir 		m_f = fopen( m_pcFile , "w" );
466*cdf0e10cSrcweir 	}
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 	fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f );
469*cdf0e10cSrcweir }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir void OFileWriter::flush(void)
473*cdf0e10cSrcweir 	throw  (NotConnectedException, BufferSizeExceededException, RuntimeException)
474*cdf0e10cSrcweir {
475*cdf0e10cSrcweir 	fflush( m_f );
476*cdf0e10cSrcweir }
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir void OFileWriter::closeOutput(void)
479*cdf0e10cSrcweir 	throw  (NotConnectedException, BufferSizeExceededException, RuntimeException)
480*cdf0e10cSrcweir {
481*cdf0e10cSrcweir 	fclose( m_f );
482*cdf0e10cSrcweir 	m_f = 0;
483*cdf0e10cSrcweir }
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir // Needed to switch on solaris threads
488*cdf0e10cSrcweir #ifdef SOLARIS
489*cdf0e10cSrcweir extern "C" void ChangeGlobalInit();
490*cdf0e10cSrcweir #endif
491*cdf0e10cSrcweir int main (int argc, char **argv)
492*cdf0e10cSrcweir {
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir 	if( argc < 3) {
495*cdf0e10cSrcweir 		printf( "usage : saxdemo inputfile outputfile\n" );
496*cdf0e10cSrcweir 		exit( 0 );
497*cdf0e10cSrcweir 	}
498*cdf0e10cSrcweir #ifdef SOLARIS
499*cdf0e10cSrcweir 	// switch on threads in solaris
500*cdf0e10cSrcweir 	ChangeGlobalInit();
501*cdf0e10cSrcweir #endif
502*cdf0e10cSrcweir 
503*cdf0e10cSrcweir 	// create service manager
504*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xSMgr = createRegistryServiceFactory(
505*cdf0e10cSrcweir 		OUString( RTL_CONSTASCII_USTRINGPARAM( "applicat.rdb" )) );
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 	Reference < XImplementationRegistration > xReg;
508*cdf0e10cSrcweir 	try
509*cdf0e10cSrcweir 	{
510*cdf0e10cSrcweir 		// Create registration service
511*cdf0e10cSrcweir 		Reference < XInterface > x = xSMgr->createInstance(
512*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ) );
513*cdf0e10cSrcweir 		xReg = Reference<  XImplementationRegistration > ( x , UNO_QUERY );
514*cdf0e10cSrcweir 	}
515*cdf0e10cSrcweir 	catch( Exception & ) {
516*cdf0e10cSrcweir 		printf( "Couldn't create ImplementationRegistration service\n" );
517*cdf0e10cSrcweir 		exit(1);
518*cdf0e10cSrcweir 	}
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir 	OString sTestName;
521*cdf0e10cSrcweir 	try
522*cdf0e10cSrcweir 	{
523*cdf0e10cSrcweir 		// Load dll for the tested component
524*cdf0e10cSrcweir 		OUString aDllName =
525*cdf0e10cSrcweir             OUString::createFromAscii( "sax.uno" SAL_DLLEXTENSION );
526*cdf0e10cSrcweir 		xReg->registerImplementation(
527*cdf0e10cSrcweir 			OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
528*cdf0e10cSrcweir 			aDllName,
529*cdf0e10cSrcweir 			Reference< XSimpleRegistry > ()  );
530*cdf0e10cSrcweir 	}
531*cdf0e10cSrcweir 	catch( Exception &e ) {
532*cdf0e10cSrcweir 		printf( "Couldn't reach sax dll\n" );
533*cdf0e10cSrcweir 		printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir 		exit(1);
536*cdf0e10cSrcweir 	}
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir 	//--------------------------------
540*cdf0e10cSrcweir 	// parser demo
541*cdf0e10cSrcweir 	// read xml from a file and count elements
542*cdf0e10cSrcweir 	//--------------------------------
543*cdf0e10cSrcweir 	Reference< XInterface > x = xSMgr->createInstance(
544*cdf0e10cSrcweir 		OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ) );
545*cdf0e10cSrcweir 	if( x.is() )
546*cdf0e10cSrcweir 	{
547*cdf0e10cSrcweir 		Reference< XParser > rParser( x , UNO_QUERY );
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 		// create and connect the document handler to the parser
550*cdf0e10cSrcweir 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( );
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir 		Reference < XDocumentHandler >	rDocHandler( (XDocumentHandler *) pDocHandler );
553*cdf0e10cSrcweir 		Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
554*cdf0e10cSrcweir 
555*cdf0e10cSrcweir 		rParser->setDocumentHandler( rDocHandler );
556*cdf0e10cSrcweir 		rParser->setEntityResolver( rEntityResolver );
557*cdf0e10cSrcweir 
558*cdf0e10cSrcweir 		// create the input stream
559*cdf0e10cSrcweir 		InputSource source;
560*cdf0e10cSrcweir 		source.aInputStream = createStreamFromFile( argv[1] );
561*cdf0e10cSrcweir 		source.sSystemId 	= OUString::createFromAscii( argv[1] );
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir 		try
564*cdf0e10cSrcweir 		{
565*cdf0e10cSrcweir 			// start parsing
566*cdf0e10cSrcweir 			rParser->parseStream( source );
567*cdf0e10cSrcweir 		}
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir 		catch( Exception & e )
570*cdf0e10cSrcweir 		{
571*cdf0e10cSrcweir 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
572*cdf0e10cSrcweir 			printf( "Exception during parsing : %s\n" ,  o1.getStr() );
573*cdf0e10cSrcweir 		}
574*cdf0e10cSrcweir 	}
575*cdf0e10cSrcweir 	else
576*cdf0e10cSrcweir 	{
577*cdf0e10cSrcweir 		printf( "couln't create sax-parser component\n" );
578*cdf0e10cSrcweir 	}
579*cdf0e10cSrcweir 
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir 	//----------------------
582*cdf0e10cSrcweir 	// The SAX-Writer demo
583*cdf0e10cSrcweir 	//----------------------
584*cdf0e10cSrcweir 	x= xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.xml.sax.Writer" ) );
585*cdf0e10cSrcweir 	if( x.is() )
586*cdf0e10cSrcweir 	{
587*cdf0e10cSrcweir 		printf( "start writing to %s\n" , argv[2] );
588*cdf0e10cSrcweir 
589*cdf0e10cSrcweir 		OFileWriter *pw = new OFileWriter( argv[2] );
590*cdf0e10cSrcweir 		Reference< XActiveDataSource > source( x , UNO_QUERY );
591*cdf0e10cSrcweir 		source->setOutputStream( Reference< XOutputStream> ( (XOutputStream*) pw ) );
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir 		AttributeListImpl *pList = new AttributeListImpl;
594*cdf0e10cSrcweir 		Reference< XAttributeList > rList( (XAttributeList *) pList );
595*cdf0e10cSrcweir 
596*cdf0e10cSrcweir 		Reference< XExtendedDocumentHandler > r( x , UNO_QUERY );
597*cdf0e10cSrcweir 		r->startDocument();
598*cdf0e10cSrcweir 
599*cdf0e10cSrcweir 		pList->addAttribute( OUString( RTL_CONSTASCII_USTRINGPARAM("Arg1" )),
600*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("CDATA")) ,
601*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("foo\n	u")) );
602*cdf0e10cSrcweir 		pList->addAttribute( OUString( RTL_CONSTASCII_USTRINGPARAM("Arg2")) ,
603*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("CDATA")) ,
604*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("foo2")) );
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir 		r->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM("tag1"))  , rList );
607*cdf0e10cSrcweir 		// tells the writer to insert a linefeed
608*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
609*cdf0e10cSrcweir 
610*cdf0e10cSrcweir 		r->characters( OUString( RTL_CONSTASCII_USTRINGPARAM("huhu")) );
611*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
612*cdf0e10cSrcweir 
613*cdf0e10cSrcweir 		r->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM("hi")) , rList );
614*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir 		// the enpassant must be converted & -> &amp;
617*cdf0e10cSrcweir 		r->characters( OUString( RTL_CONSTASCII_USTRINGPARAM("&#252;")) );
618*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
619*cdf0e10cSrcweir 
620*cdf0e10cSrcweir 		// '>' must not be converted
621*cdf0e10cSrcweir 		r->startCDATA();
622*cdf0e10cSrcweir 		r->characters( OUString( RTL_CONSTASCII_USTRINGPARAM(" > foo < "))  );
623*cdf0e10cSrcweir 		r->endCDATA();
624*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir 		OUString testParagraph = OUString( RTL_CONSTASCII_USTRINGPARAM(
627*cdf0e10cSrcweir 			"This is only a test to check, if the writer inserts line feeds "
628*cdf0e10cSrcweir 			"if needed or if the writer puts the whole text into one line." ));
629*cdf0e10cSrcweir 		writeParagraphHelper( r , testParagraph );
630*cdf0e10cSrcweir 
631*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
632*cdf0e10cSrcweir 		r->comment( OUString( RTL_CONSTASCII_USTRINGPARAM("This is a comment !")) );
633*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
634*cdf0e10cSrcweir 
635*cdf0e10cSrcweir 		r->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM("emptytagtest"))  , rList );
636*cdf0e10cSrcweir 		r->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM("emptytagtest")) );
637*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 		r->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM("hi")) );
640*cdf0e10cSrcweir 		r->ignorableWhitespace( OUString() );
641*cdf0e10cSrcweir 
642*cdf0e10cSrcweir 		r->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM("tag1")) );
643*cdf0e10cSrcweir 		r->endDocument();
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir 		printf( "finished writing\n" );
646*cdf0e10cSrcweir 	}
647*cdf0e10cSrcweir 	else
648*cdf0e10cSrcweir 	{
649*cdf0e10cSrcweir 		printf( "couln't create sax-writer component\n" );
650*cdf0e10cSrcweir 	}
651*cdf0e10cSrcweir }
652