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