xref: /trunk/main/sax/test/sax/testsax.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include <osl/time.h>
32 #include <osl/diagnose.h>
33 
34 
35 #include <com/sun/star/test/XSimpleTest.hpp>
36 
37 #include <com/sun/star/io/XOutputStream.hpp>
38 
39 #include <com/sun/star/xml/sax/SAXParseException.hpp>
40 #include <com/sun/star/xml/sax/XParser.hpp>
41 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
42 
43 #include <cppuhelper/factory.hxx>
44 #include <cppuhelper/implbase1.hxx>
45 #include <cppuhelper/implbase3.hxx>
46 
47 using namespace ::rtl;
48 using namespace ::cppu;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::test;
52 using namespace ::com::sun::star::registry;
53 using namespace ::com::sun::star::io;
54 using namespace ::com::sun::star::xml::sax;
55 
56 #include "factory.hxx"
57 
58 /****
59 * test szenarios :
60 *
61 *
62 *
63 ****/
64 
65 namespace sax_test {
66 
67 class OSaxParserTest : public WeakImplHelper1< XSimpleTest >
68 {
69 public:
70 	OSaxParserTest( const Reference < XMultiServiceFactory > & rFactory ) : m_rFactory( rFactory )
71 	{
72 	}
73 public:
74     virtual void SAL_CALL testInvariant(
75 		const OUString& TestName,
76 		const Reference < XInterface >& TestObject)
77 		throw (	IllegalArgumentException, RuntimeException);
78 
79     virtual sal_Int32 SAL_CALL test(
80 		const OUString& TestName,
81 		const Reference < XInterface >& TestObject,
82 		sal_Int32 hTestHandle)
83 		throw (	IllegalArgumentException,RuntimeException);
84 
85     virtual sal_Bool SAL_CALL testPassed(void) throw (	RuntimeException);
86     virtual Sequence< OUString > SAL_CALL getErrors(void) 				throw (RuntimeException);
87     virtual Sequence< Any > SAL_CALL getErrorExceptions(void) 		throw (RuntimeException);
88     virtual Sequence< OUString > SAL_CALL getWarnings(void) 				throw (RuntimeException);
89 
90 private:
91 	void testSimple( const Reference < XParser > &r );
92 	void testNamespaces( const Reference < XParser > &r );
93 	void testFile(  const Reference < XParser > &r );
94 	void testEncoding( const Reference < XParser > &rParser );
95 	void testPerformance( const Reference < XParser > &rParser );
96 
97 private:
98 	Sequence<Any>  		m_seqExceptions;
99 	Sequence<OUString> 		m_seqErrors;
100 	Sequence<OUString> 		m_seqWarnings;
101 	Reference < XMultiServiceFactory > m_rFactory;
102 };
103 
104 
105 
106 /**
107 * for external binding
108 *
109 *
110 **/
111 Reference < XInterface > SAL_CALL OSaxParserTest_CreateInstance( const Reference < XMultiServiceFactory > & rSMgr ) throw(Exception)
112 {
113 	OSaxParserTest *p = new OSaxParserTest( rSMgr );
114 	return Reference < XInterface > ( SAL_STATIC_CAST( OWeakObject * , p ) );
115 }
116 
117 
118 OUString     OSaxParserTest_getServiceName( ) throw ()
119 {
120 	return OUString( RTL_CONSTASCII_USTRINGPARAM("test.com.sun.star.xml.sax.Parser" ));
121 }
122 
123 OUString 	OSaxParserTest_getImplementationName( ) throw ()
124 {
125 	return OUString( RTL_CONSTASCII_USTRINGPARAM("test.extensions.xml.sax.Parser"));
126 }
127 
128 Sequence<OUString> OSaxParserTest_getSupportedServiceNames( ) throw ()
129 {
130 	Sequence<OUString> aRet(1);
131 
132 	aRet.getArray()[0] = OSaxParserTest_getImplementationName( );
133 
134 	return aRet;
135 }
136 
137 
138 
139 
140 void OSaxParserTest::testInvariant(
141 	const OUString& TestName,
142 	const Reference < XInterface >& TestObject )
143 	throw (	IllegalArgumentException, RuntimeException)
144 {
145 	if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser")) == TestName ) {
146 		Reference < XParser > parser( TestObject , UNO_QUERY );
147 
148 		ERROR_ASSERT( parser.is() , "XDataInputStream cannot be queried" );
149 	}
150 }
151 
152 
153 sal_Int32 OSaxParserTest::test(
154 	const OUString& TestName,
155 	const Reference < XInterface >& TestObject,
156 	sal_Int32 hTestHandle)
157 	throw (	IllegalArgumentException, RuntimeException)
158 {
159 	if( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser")) == TestName )  {
160 		try
161 		{
162 			if( 0 == hTestHandle ) {
163 				testInvariant( TestName , TestObject );
164 			}
165 			else {
166 
167 				Reference < XParser > parser( TestObject , UNO_QUERY );
168 
169 				if( 1 == hTestHandle ) {
170 					testSimple( parser );
171 				}
172 				else if( 2 == hTestHandle ) {
173 					testNamespaces( parser );
174 				}
175 				else if( 3 == hTestHandle ) {
176 					testEncoding( parser );
177 				}
178 				else if( 4 == hTestHandle ) {
179 					testFile( parser );
180 				}
181 				else if( 5 == hTestHandle ) {
182 					testPerformance( parser );
183 				}
184 			}
185 		}
186 		catch( Exception & e )
187 		{
188 			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US);
189 			BUILD_ERROR( 0 , o.getStr() );
190 		}
191 		catch( ... )
192 		{
193 			BUILD_ERROR( 0 , "unknown exception (Exception is  not base class)" );
194 		}
195 
196 		hTestHandle ++;
197 
198 		if( hTestHandle >= 6) {
199 			// all tests finished.
200 			hTestHandle = -1;
201 		}
202 	}
203 	else {
204 		BUILD_ERROR( 0 , "service not supported by test." );
205 	}
206 	return hTestHandle;
207 }
208 
209 
210 
211 sal_Bool OSaxParserTest::testPassed(void) 										throw (RuntimeException)
212 {
213 	return m_seqErrors.getLength() == 0;
214 }
215 
216 
217 Sequence< OUString > OSaxParserTest::getErrors(void) 							throw (RuntimeException)
218 {
219 	return m_seqErrors;
220 }
221 
222 
223 Sequence< Any > OSaxParserTest::getErrorExceptions(void) 					throw (RuntimeException)
224 {
225 	return m_seqExceptions;
226 }
227 
228 
229 Sequence< OUString > OSaxParserTest::getWarnings(void) 						throw (RuntimeException)
230 {
231 	return m_seqWarnings;
232 }
233 
234 Reference < XInputStream > createStreamFromSequence(
235 	const Sequence<sal_Int8> seqBytes ,
236 	const Reference < XMultiServiceFactory > &xSMgr )
237 {
238 	Reference < XInterface > xOutStreamService =
239 		xSMgr->createInstance( OUString::createFromAscii("com.sun.star.io.Pipe") );
240 	OSL_ASSERT( xOutStreamService.is() );
241 	Reference< XOutputStream >  rOutStream( xOutStreamService , UNO_QUERY );
242 	OSL_ASSERT( rOutStream.is() );
243 
244 	Reference< XInputStream > rInStream( xOutStreamService , UNO_QUERY );
245 	OSL_ASSERT( rInStream.is() );
246 
247 	rOutStream->writeBytes( seqBytes );
248 	rOutStream->flush();
249 	rOutStream->closeOutput();
250 
251 	return rInStream;
252 }
253 
254 Reference< XInputStream > createStreamFromFile(
255 	const char *pcFile ,
256 	const Reference < XMultiServiceFactory > &xSMgr )
257 {
258 	FILE *f = fopen( pcFile , "rb" );
259 	Reference<  XInputStream >  r;
260 
261 	if( f ) {
262 		fseek( f , 0 , SEEK_END );
263 		int nLength = ftell( f );
264 		fseek( f , 0 , SEEK_SET );
265 
266 		Sequence<sal_Int8> seqIn(nLength);
267 		fread( seqIn.getArray() , nLength , 1 , f );
268 
269 		r = createStreamFromSequence( seqIn , xSMgr );
270 		fclose( f );
271 	}
272 	return r;
273 }
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 //  #define PCHAR_TO_OUSTRING(x) OStringToOUString(x,CHARSET_PC_1252)
284 //  #define USTRING_TO_PCHAR(x) UStringToString(x,CHARSET_PC_437).GetStr()
285 
286 
287 
288 class TestDocumentHandler :
289 	public WeakImplHelper3< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
290 {
291 public:
292 	TestDocumentHandler( const Reference < XMultiServiceFactory >  &r , sal_Bool bPrint )
293 	{
294 		m_xSMgr = r;
295 		m_bPrint = bPrint;
296 	}
297 
298 public: // Error handler
299     virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException)
300     {
301     	printf( "Error !\n" );
302     	throw  SAXException(
303 			OUString( RTL_CONSTASCII_USTRINGPARAM("error from error handler")) ,
304 			Reference < XInterface >() ,
305 			aSAXParseException );
306     }
307     virtual void SAL_CALL fatalError(const Any& aSAXParseException) throw (SAXException, RuntimeException)
308     {
309     	printf( "Fatal Error !\n" );
310     }
311     virtual void SAL_CALL warning(const Any& aSAXParseException) throw (SAXException, RuntimeException)
312     {
313     	printf( "Warning !\n" );
314     }
315 
316 
317 public: // ExtendedDocumentHandler
318 
319     virtual void SAL_CALL startDocument(void) throw (SAXException, RuntimeException)
320     {
321  		m_iLevel = 0;
322 		m_iElementCount = 0;
323 		m_iAttributeCount = 0;
324 		m_iWhitespaceCount =0;
325 		m_iCharCount=0;
326 		if( m_bPrint ) {
327     		printf( "document started\n" );
328     	}
329     }
330     virtual void SAL_CALL endDocument(void) throw (SAXException, RuntimeException)
331     {
332 		if( m_bPrint ) {
333     		printf( "document finished\n" );
334     		printf( "(ElementCount %d),(AttributeCount %d),(WhitespaceCount %d),(CharCount %d)\n",
335     			m_iElementCount, m_iAttributeCount, m_iWhitespaceCount , m_iCharCount );
336     	}
337     }
338     virtual void SAL_CALL startElement(const OUString& aName,
339 							  const Reference< XAttributeList > & xAttribs)
340 		throw (SAXException,RuntimeException)
341 	{
342 
343 		if( m_rLocator.is() ) {
344 			if( m_bPrint )
345 			{
346 				OString o = OUStringToOString( m_rLocator->getSystemId() , RTL_TEXTENCODING_UTF8 );
347 				printf( "%s(%d):" , o.getStr() , m_rLocator->getLineNumber() );
348 			}
349 		}
350 		if( m_bPrint ) {
351 	    	int i;
352 	    	for( i = 0; i < m_iLevel ; i ++ ) {
353 	    		printf( "  " );
354 	    	}
355 			OString o = OUStringToOString(aName , RTL_TEXTENCODING_UTF8 );
356 	    	printf( "<%s> " , aName.getStr() );
357 
358 	    	for( i = 0 ; i < xAttribs->getLength() ; i ++ )
359 			{
360 				OString o1 = OUStringToOString(xAttribs->getNameByIndex( i ), RTL_TEXTENCODING_UTF8 );
361 				OString o2 = OUStringToOString(xAttribs->getTypeByIndex( i ), RTL_TEXTENCODING_UTF8 );
362 				OString o3 = OUStringToOString(xAttribs->getValueByIndex( i ) , RTL_TEXTENCODING_UTF8 );
363 	    		printf( "(%s,%s,'%s')" , o1.getStr(), o2.getStr(), o3.getStr() );
364 	    	}
365 	    	printf( "\n" );
366 		}
367     	m_iLevel ++;
368     	m_iElementCount ++;
369     	m_iAttributeCount += xAttribs->getLength();
370     }
371 
372     virtual void SAL_CALL endElement(const OUString& aName) throw (SAXException,RuntimeException)
373     {
374     	OSL_ASSERT( m_iLevel );
375     	m_iLevel --;
376     	if( m_bPrint ) {
377 	    	int i;
378 	    	for( i = 0; i < m_iLevel ; i ++ ) {
379 	    		printf( "  " );
380 	    	}
381 			OString o = OUStringToOString(aName , RTL_TEXTENCODING_UTF8 );
382 	    	printf( "</%s>\n" , o.getStr() );
383 	    }
384     }
385 
386     virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException)
387     {
388     	if( m_bPrint ) {
389 	    	int i;
390 	    	for( i = 0; i < m_iLevel ; i ++ ) {
391 	    		printf( "  " );
392 	    	}
393 			OString o = OUStringToOString(aChars , RTL_TEXTENCODING_UTF8 );
394 	    	printf( "%s\n" , o.getStr() );
395 	    }
396     	m_iCharCount += aChars.getLength();
397     }
398     virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces) throw (SAXException,RuntimeException)
399     {
400 		m_iWhitespaceCount += aWhitespaces.getLength();
401     }
402 
403     virtual void SAL_CALL processingInstruction(const OUString& aTarget, const OUString& aData) throw (SAXException,RuntimeException)
404     {
405 		if( m_bPrint )
406 		{
407 			OString o1 = OUStringToOString(aTarget, RTL_TEXTENCODING_UTF8 );
408 			OString o2 = OUStringToOString(aData, RTL_TEXTENCODING_UTF8 );
409 			printf( "PI : %s,%s\n" , o1.getStr() , o2.getStr() );
410 		}
411     }
412 
413     virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & xLocator)
414 		throw (SAXException,RuntimeException)
415     {
416 		m_rLocator = xLocator;
417     }
418 
419     virtual InputSource SAL_CALL resolveEntity(
420 		const OUString& sPublicId,
421 		const OUString& sSystemId)
422 		throw (SAXException,RuntimeException)
423 	{
424 		InputSource source;
425 		source.sSystemId = sSystemId;
426 		source.sPublicId = sPublicId;
427 
428 		source.aInputStream = createStreamFromFile(
429 			OUStringToOString( sSystemId , RTL_TEXTENCODING_ASCII_US) , m_xSMgr );
430 
431 		return source;
432 	}
433 
434     virtual void SAL_CALL startCDATA(void) throw (SAXException,RuntimeException)
435     {
436 		if( m_bPrint ) {
437     		printf( "CDataStart :\n" );
438     	}
439     }
440     virtual void SAL_CALL endCDATA(void) throw (SAXException,RuntimeException)
441     {
442 		if( m_bPrint ) {
443     		printf( "CEndStart :\n" );
444     	}
445     }
446     virtual void SAL_CALL comment(const OUString& sComment) throw (SAXException,RuntimeException)
447     {
448 		if( m_bPrint ) {
449 			OString o1 = OUStringToOString(sComment, RTL_TEXTENCODING_UTF8 );
450     		printf( "<!--%s-->\n" , o1.getStr() );
451     	}
452     }
453     virtual void SAL_CALL unknown(const OUString& sString) throw (SAXException,RuntimeException)
454     {
455 		if( m_bPrint )
456 		{
457 			OString o1 = OUStringToOString(sString, RTL_TEXTENCODING_UTF8 );
458     		printf( "UNKNOWN : {%s}\n" , o1.getStr() );
459     	}
460     }
461 
462 	virtual void SAL_CALL allowLineBreak( void) throw (SAXException, RuntimeException )
463 	{
464 
465 	}
466 
467 
468 public:
469 	int m_iLevel;
470 	int m_iElementCount;
471 	int m_iAttributeCount;
472 	int m_iWhitespaceCount;
473 	int m_iCharCount;
474 	sal_Bool m_bPrint;
475 
476 	Reference < XMultiServiceFactory > m_xSMgr;
477 	Reference < XLocator > m_rLocator;
478 };
479 
480 
481 void OSaxParserTest::testSimple( 	const Reference < XParser > &rParser )
482 {
483 
484 	char TestString[] = "<!DOCTYPE personnel [\n"
485 						"<!ENTITY testInternal \"internal Test!\">\n"
486 						"<!ENTITY test SYSTEM \"external_entity.xml\">\n"
487 						"]>\n"
488 						"<personnel>\n"
489 						"<person> fjklsfdklsdfkl\n"
490 						"fjklsfdklsdfkl\n"
491 						"<?testpi pidata?>\n"
492 						"&testInternal;\n"
493 						"<HUHU x='5' y='kjfd'> blahuhu\n"
494 						"<HI> blahi\n"
495 						"     <![CDATA[<greeting>Hello, '+1+12world!</greeting>]]>\n"
496 						"   <!-- huhu <jdk> -->\n"
497 						"<?testpi pidata?>\n"
498 						"</HI>\n"
499 						"aus XMLTest\n"
500 						"</HUHU>\n"
501 						"</person>\n"
502 						"</personnel>\n\n\n";
503 
504 	Sequence< sal_Int8> seqBytes( strlen( TestString ) );
505 	memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
506 
507 
508 	Reference< XInputStream > rInStream;
509 	OUString sInput;
510 	rInStream = createStreamFromSequence( seqBytes , m_rFactory );
511 	sInput = OUString( OUString( RTL_CONSTASCII_USTRINGPARAM("internal")) );
512 
513 	if( rParser.is() ) {
514 		InputSource source;
515 
516 		source.aInputStream = rInStream;
517 		source.sSystemId 	= sInput;
518 
519 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , sal_False );
520 		Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler , UNO_QUERY );
521 		Reference< XEntityResolver >
522 			rEntityResolver( (XEntityResolver *) pDocHandler , UNO_QUERY );
523 
524 		rParser->setDocumentHandler( rDocHandler );
525 		rParser->setEntityResolver( rEntityResolver );
526 
527 		try
528 		{
529 			rParser->parseStream( source );
530 			ERROR_ASSERT( pDocHandler->m_iElementCount 		== 4 , "wrong element count" 	);
531 			ERROR_ASSERT( pDocHandler->m_iAttributeCount 	== 2 , "wrong attribut count" 	);
532 			ERROR_ASSERT( pDocHandler->m_iCharCount 		== 130 , "wrong char count" 	);
533 			ERROR_ASSERT( pDocHandler->m_iWhitespaceCount 	== 0, "wrong whitespace count" );
534 		}
535 		catch( SAXParseException & e )
536 		{
537 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
538 			BUILD_ERROR( 1 , o1.getStr() );
539 		}
540 		catch( SAXException & e )
541 		{
542 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
543 			BUILD_ERROR( 1 , o1.getStr() );
544 		}
545 		catch( Exception & e )
546 		{
547 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
548 			BUILD_ERROR( 1 , o1.getStr() );
549 		}
550 		catch( ... )
551 		{
552 			BUILD_ERROR( 1 , "unknown exception" );
553 		}
554 	}
555 }
556 
557 void OSaxParserTest::testNamespaces( const Reference < XParser > &rParser )
558 {
559 
560 	char TestString[] =
561 	"<?xml version='1.0'?>\n"
562   	"<!-- all elements here are explicitly in the HTML namespace -->\n"
563   	"<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>\n"
564     	"<html:head><html:title>Frobnostication</html:title></html:head>\n"
565     	"<html:body><html:p>Moved to \n"
566       	"<html:a href='http://frob.com'>here.</html:a></html:p></html:body>\n"
567   	"</html:html>\n";
568 
569 	Sequence<sal_Int8> seqBytes( strlen( TestString ) );
570 	memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
571 
572 
573 	Reference< XInputStream >  rInStream;
574 	OUString sInput;
575 
576 	rInStream = createStreamFromSequence( seqBytes , m_rFactory );
577 	sInput = OUString( RTL_CONSTASCII_USTRINGPARAM( "internal" ));
578 
579 	if( rParser.is() ) {
580 		InputSource source;
581 
582 		source.aInputStream = rInStream;
583 		source.sSystemId 	= sInput;
584 
585 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , sal_False );
586 		Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler , UNO_QUERY );
587 		Reference< XEntityResolver >	rEntityResolver(
588 			(XEntityResolver *) pDocHandler , UNO_QUERY );
589 
590 		rParser->setDocumentHandler( rDocHandler );
591 		rParser->setEntityResolver( rEntityResolver );
592 
593 		try
594 		{
595 			rParser->parseStream( source );
596 			ERROR_ASSERT( pDocHandler->m_iElementCount 		== 6 , "wrong element count" 	);
597 			ERROR_ASSERT( pDocHandler->m_iAttributeCount 	== 2 , "wrong attribut count" 	);
598 			ERROR_ASSERT( pDocHandler->m_iCharCount 		== 33, "wrong char count" 		);
599 			ERROR_ASSERT( pDocHandler->m_iWhitespaceCount 	== 0 , "wrong whitespace count" );
600 		}
601 		catch( Exception & e ) {
602 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
603 			BUILD_ERROR( 1 , o1.getStr() );
604 		}
605 		catch( ... )
606 		{
607 			BUILD_ERROR( 1 , "unknown exception" );
608 		}
609 	}
610 }
611 
612 void OSaxParserTest::testEncoding( const Reference < XParser > &rParser )
613 {
614 	char TestString[] =
615 	"<?xml version='1.0' encoding=\"iso-8859-1\"?>\n"
616   	"<!-- all elements here are explicitly in the HTML namespace -->\n"
617   	"<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>\n"
618     	"<html:head><html:title>Frobnostication</html:title></html:head>\n"
619     	"<html:body><html:p>Moved to �\n"
620       	"<html:a href='http://frob.com'>here.</html:a></html:p></html:body>\n"
621   	"</html:html>\n";
622 
623 	Sequence<sal_Int8> seqBytes( strlen( TestString ) );
624 	memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
625 
626 
627 	Reference< XInputStream > rInStream;
628 	OUString sInput;
629 
630 	rInStream = createStreamFromSequence( seqBytes , m_rFactory );
631 	sInput = OUString( RTL_CONSTASCII_USTRINGPARAM("internal") );
632 
633 	if( rParser.is() ) {
634 		InputSource source;
635 
636 		source.aInputStream = rInStream;
637 		source.sSystemId 	= sInput;
638 
639 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , sal_False );
640 		Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler , UNO_QUERY );
641 		Reference< XEntityResolver >  rEntityResolver( (XEntityResolver *) pDocHandler , UNO_QUERY );
642 
643 		rParser->setDocumentHandler( rDocHandler );
644 		rParser->setEntityResolver( rEntityResolver );
645 		try
646 		{
647 			rParser->parseStream( source );
648 		}
649 		catch( Exception & e )
650 		{
651 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
652 			BUILD_ERROR( 1 , o1.getStr() );
653 		}
654 		catch ( ... )
655 		{
656 			BUILD_ERROR( 1 , "unknown exception" );
657 		}
658 	}
659 }
660 
661 void OSaxParserTest::testFile( const Reference < XParser > & rParser )
662 {
663 
664 	Reference< XInputStream > rInStream = createStreamFromFile( "testsax.xml" , m_rFactory );
665 	OUString sInput = OUString( RTL_CONSTASCII_USTRINGPARAM( "testsax.xml" ) );
666 
667 
668 	if( rParser.is() && rInStream.is() ) {
669 		InputSource source;
670 
671 		source.aInputStream = rInStream;
672 		source.sSystemId 	= sInput;
673 
674 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , sal_True );
675 		Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler , UNO_QUERY );
676 		Reference < XEntityResolver >	rEntityResolver( (XEntityResolver *) pDocHandler , UNO_QUERY );
677 		Reference < XErrorHandler >	rErrorHandler( ( XErrorHandler * )pDocHandler , UNO_QUERY );
678 
679 		rParser->setDocumentHandler( rDocHandler );
680 		rParser->setEntityResolver( rEntityResolver );
681 		rParser->setErrorHandler( rErrorHandler );
682 
683 		try
684 		{
685 			rParser->parseStream( source );
686 		}
687 		catch( SAXParseException & e ) {
688 			Any any;
689 			any <<= e;
690 
691 			while(sal_True) {
692 				SAXParseException *pEx;
693 				if( any.getValueType() == getCppuType( &e ) ) {
694 					pEx = ( SAXParseException * ) any.getValue();
695 					OString o1 = OUStringToOString(pEx->Message, RTL_TEXTENCODING_UTF8 );
696 					printf( "%s\n" , o1.getStr()  );
697 					any = pEx->WrappedException;
698 				}
699 				else {
700 					break;
701 				}
702 			}
703 		}
704 		catch( SAXException & e )
705 		{
706 			OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
707 			BUILD_ERROR( 1 , o1.getStr() );
708 
709 		}
710 		catch( Exception & e ) {
711 			printf( "normal exception ! %s\n", e.Message );
712 		}
713 		catch ( ... )
714 		{
715 			printf( "any exception !!!!\n" );
716 		}
717 	}
718 }
719 
720 void OSaxParserTest::testPerformance( const Reference < XParser > & rParser )
721 {
722 
723 	Reference < XInputStream > rInStream =
724 		createStreamFromFile( "testPerformance.xml" , m_rFactory );
725 	OUString sInput = OUString( RTL_CONSTASCII_USTRINGPARAM( "testperformance.xml") );
726 
727 	if( rParser.is() && rInStream.is() ) {
728 		InputSource source;
729 
730 		source.aInputStream = rInStream;
731 		source.sSystemId 	= sInput;
732 
733 		TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , sal_False );
734 		Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler , UNO_QUERY );
735 		Reference < XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler , UNO_QUERY );
736 		Reference < XErrorHandler >	rErrorHandler( ( XErrorHandler * )pDocHandler , UNO_QUERY );
737 
738 		rParser->setDocumentHandler( rDocHandler );
739 		rParser->setEntityResolver( rEntityResolver );
740 		rParser->setErrorHandler( rErrorHandler );
741 
742 		try
743 		{
744 			TimeValue aStartTime, aEndTime;
745 			osl_getSystemTime( &aStartTime );
746 			rParser->parseStream( source );
747 			osl_getSystemTime( &aEndTime );
748 
749 			double fStart = (double)aStartTime.Seconds + ((double)aStartTime.Nanosec / 1000000000.0);
750 			double fEnd = (double)aEndTime.Seconds + ((double)aEndTime.Nanosec / 1000000000.0);
751 
752 			printf( "Performance reading : %g s\n" , fEnd - fStart );
753 
754 		}
755 		catch( SAXParseException &e ) {
756 			Any any;
757 			any <<= e;
758 			while(sal_True) {
759 				if( any.getValueType() == getCppuType( &e ) ) {
760 					SAXParseException ex;
761 					any >>= ex;
762 					OString o = OUStringToOString( ex.Message , RTL_TEXTENCODING_ASCII_US );
763 					printf( "%s\n" , o.getStr()  );
764 					any <<= ex.WrappedException;
765 				}
766 				else {
767 					break;
768 				}
769 			}
770 		}
771 		catch( SAXException &e  ) {
772 			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
773 			printf( "%s\n" , o.getStr()  );
774 
775 		}
776 		catch( ... )
777 		{
778 			printf( "any exception !!!!\n" );
779 		}
780 	}
781 }
782 }
783 using namespace sax_test;
784 
785 extern "C"
786 {
787 
788 
789 void SAL_CALL component_getImplementationEnvironment(
790 	const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
791 {
792 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
793 }
794 
795 
796 sal_Bool SAL_CALL component_writeInfo(
797 	void * pServiceManager, void * pRegistryKey )
798 {
799 	if (pRegistryKey)
800 	{
801 		try
802 		{
803 			Reference< XRegistryKey > xKey(
804 				reinterpret_cast< XRegistryKey * >( pRegistryKey ) );
805 
806 			OUString str =
807 				OUString( RTL_CONSTASCII_USTRINGPARAM("/") ) +
808 				OSaxParserTest_getImplementationName() +
809 				OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES") );
810 			Reference< XRegistryKey > xNewKey = xKey->createKey( str );
811 			xNewKey->createKey( OSaxParserTest_getServiceName() );
812 
813 			str =
814 				OUString( RTL_CONSTASCII_USTRINGPARAM("/") ) +
815 				OSaxWriterTest_getImplementationName() +
816 				OUString( RTL_CONSTASCII_USTRINGPARAM("/UNO/SERVICES") );
817 
818 			xNewKey = xKey->createKey( str );
819 			xNewKey->createKey( OSaxWriterTest_getServiceName() );
820 
821 			return sal_True;
822 		}
823 		catch (InvalidRegistryException &)
824 		{
825 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
826 		}
827 	}
828 
829 	return sal_False;
830 }
831 
832 void * SAL_CALL component_getFactory(
833 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
834 {
835 	void * pRet = 0;
836 
837 	if (pServiceManager )
838 	{
839 		Reference< XSingleServiceFactory > xRet;
840 		Reference< XMultiServiceFactory > xSMgr =
841 			reinterpret_cast< XMultiServiceFactory * > ( pServiceManager );
842 
843 		OUString aImplementationName = OUString::createFromAscii( pImplName );
844 
845 
846 		if (aImplementationName == OSaxWriterTest_getImplementationName() )
847 		{
848 			xRet = createSingleFactory( xSMgr, aImplementationName,
849 										OSaxWriterTest_CreateInstance,
850 										OSaxWriterTest_getSupportedServiceNames() );
851 		}
852 		else if (aImplementationName == OSaxParserTest_getImplementationName() )
853 		{
854 			xRet = createSingleFactory( xSMgr, aImplementationName,
855 										OSaxParserTest_CreateInstance,
856 										OSaxParserTest_getSupportedServiceNames() );
857 		}
858 		if (xRet.is())
859 		{
860 			xRet->acquire();
861 			pRet = xRet.get();
862 		}
863 	}
864 
865 	return pRet;
866 }
867 
868 }
869 
870 
871