merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / test / sax / testsax.cxx
blobe7f52fbb0902d22488e19c99d1790188b9a851f8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
31 #include <stdio.h>
32 #include <string.h>
33 #include <smart/com/sun/star/test/XSimpleTest.hxx>
35 #include <rtl/wstring.hxx>
36 #include <osl/time.h>
37 //#include <vos/dynload.hxx>
38 //#include <vos/diagnose.hxx>
39 #include <usr/weak.hxx>
40 #include <tools/string.hxx>
41 #include <vos/conditn.hxx>
43 #include <smart/com/sun/star/io/XOutputStream.hxx>
44 #include <smart/com/sun/star/xml/sax/SAXParseException.hxx>
45 #include <smart/com/sun/star/xml/sax/XParser.hxx>
46 #include <smart/com/sun/star/xml/sax/XExtendedDocumentHandler.hxx>
48 #include <smart/com/sun/star/lang/XMultiServiceFactory.hxx> // for the multiservice-factories
49 #include <usr/factoryhlp.hxx>
51 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE
53 #include "factory.hxx"
55 using namespace rtl;
56 using namespace vos;
57 using namespace usr;
60 #define BUILD_ERROR(expr, Message)\
62 m_seqErrors.realloc( m_seqErrors.getLen() + 1 ); \
63 m_seqExceptions.realloc( m_seqExceptions.getLen() + 1 ); \
64 String str; \
65 str += __FILE__;\
66 str += " "; \
67 str += "(" ; \
68 str += __LINE__ ;\
69 str += ")\n";\
70 str += "[ " ; \
71 str += #expr; \
72 str += " ] : " ; \
73 str += Message; \
74 m_seqErrors.getArray()[ m_seqErrors.getLen()-1] = StringToUString( str , CHARSET_SYSTEM ); \
76 ((void)0)
79 #define WARNING_ASSERT(expr, Message) \
80 if( ! (expr) ) { \
81 m_seqWarnings.realloc( m_seqErrors.getLen() +1 ); \
82 String str;\
83 str += __FILE__;\
84 str += " "; \
85 str += "(" ; \
86 str += __LINE__ ;\
87 str += ")\n";\
88 str += "[ " ; \
89 str += #expr; \
90 str += " ] : " ; \
91 str += Message; \
92 m_seqWarnings.getArray()[ m_seqWarnings.getLen()-1] = StringToUString( str , CHARSET_SYSTEM ); \
93 return; \
95 ((void)0)
97 #define ERROR_ASSERT(expr, Message) \
98 if( ! (expr) ) { \
99 BUILD_ERROR(expr, Message );\
100 return; \
102 ((void)0)
104 #define ERROR_EXCEPTION_ASSERT(expr, Message, Exception) \
105 if( !(expr)) { \
106 BUILD_ERROR(expr,Message);\
107 m_seqExceptions.getArray()[ m_seqExceptions.getLen()-1] = UsrAny( Exception );\
108 return; \
110 ((void)0)
112 /****
113 * test szenarios :
117 ****/
121 class OSaxParserTest :
122 public XSimpleTest,
123 public OWeakObject
125 public:
126 OSaxParserTest( const XMultiServiceFactoryRef & rFactory ) : m_rFactory( rFactory )
131 public: // refcounting
132 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
133 void acquire() { OWeakObject::acquire(); }
134 void release() { OWeakObject::release(); }
135 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
137 public:
138 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject)
139 THROWS( ( IllegalArgumentException,
140 UsrSystemException) );
142 virtual INT32 test( const UString& TestName,
143 const XInterfaceRef& TestObject,
144 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
145 UsrSystemException) );
147 virtual BOOL testPassed(void) THROWS( ( UsrSystemException) );
148 virtual Sequence< UString > getErrors(void) THROWS( (UsrSystemException) );
149 virtual Sequence< UsrAny > getErrorExceptions(void) THROWS( (UsrSystemException) );
150 virtual Sequence< UString > getWarnings(void) THROWS( (UsrSystemException) );
152 private:
153 void testSimple( const XParserRef &r );
154 void testNamespaces( const XParserRef &r );
155 void testFile( const XParserRef &r );
156 void testEncoding( const XParserRef &rParser );
157 void testPerformance( const XParserRef &rParser );
159 private:
160 Sequence<UsrAny> m_seqExceptions;
161 Sequence<UString> m_seqErrors;
162 Sequence<UString> m_seqWarnings;
163 XMultiServiceFactoryRef m_rFactory;
169 * for external binding
173 XInterfaceRef OSaxParserTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception))
175 OSaxParserTest *p = new OSaxParserTest( rSMgr );
176 XInterfaceRef xService = *p;
177 return xService;
181 UString OSaxParserTest_getServiceName( ) THROWS( () )
183 return L"test.com.sun.star.xml.sax.Parser";
186 UString OSaxParserTest_getImplementationName( ) THROWS( () )
188 return L"test.extensions.xml.sax.Parser";
191 Sequence<UString> OSaxParserTest_getSupportedServiceNames( ) THROWS( () )
193 Sequence<UString> aRet(1);
195 aRet.getArray()[0] = OSaxParserTest_getImplementationName( );
197 return aRet;
201 BOOL OSaxParserTest::queryInterface( Uik uik , XInterfaceRef &rOut )
203 if( XSimpleTest::getSmartUik() == uik ) {
204 rOut = (XSimpleTest *) this;
206 else {
207 return OWeakObject::queryInterface( uik , rOut );
209 return TRUE;
213 void OSaxParserTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject )
214 THROWS( ( IllegalArgumentException,
215 UsrSystemException) )
217 if( L"com.sun.star.xml.sax.Parser" == TestName ) {
218 XParserRef parser( TestObject , USR_QUERY );
220 ERROR_ASSERT( parser.is() , "XDataInputStream cannot be queried" );
225 INT32 OSaxParserTest::test( const UString& TestName,
226 const XInterfaceRef& TestObject,
227 INT32 hTestHandle) THROWS( ( IllegalArgumentException,
228 UsrSystemException) )
230 if( L"com.sun.star.xml.sax.Parser" == TestName ) {
231 try {
232 if( 0 == hTestHandle ) {
233 testInvariant( TestName , TestObject );
235 else {
237 XParserRef parser( TestObject , USR_QUERY );
239 if( 1 == hTestHandle ) {
240 testSimple( parser );
242 else if( 2 == hTestHandle ) {
243 testNamespaces( parser );
245 else if( 3 == hTestHandle ) {
246 testEncoding( parser );
248 else if( 4 == hTestHandle ) {
249 testFile( parser );
251 else if( 5 == hTestHandle ) {
252 testPerformance( parser );
256 catch( Exception& e ) {
257 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() );
259 catch(...) {
260 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" );
263 hTestHandle ++;
265 if( hTestHandle >= 6) {
266 // all tests finished.
267 hTestHandle = -1;
270 else {
271 BUILD_ERROR( 0 , "service not supported by test." );
273 return hTestHandle;
278 BOOL OSaxParserTest::testPassed(void) THROWS( (UsrSystemException) )
280 return m_seqErrors.getLen() == 0;
284 Sequence< UString > OSaxParserTest::getErrors(void) THROWS( (UsrSystemException) )
286 return m_seqErrors;
290 Sequence< UsrAny > OSaxParserTest::getErrorExceptions(void) THROWS( (UsrSystemException) )
292 return m_seqExceptions;
296 Sequence< UString > OSaxParserTest::getWarnings(void) THROWS( (UsrSystemException) )
298 return m_seqWarnings;
301 XInputStreamRef createStreamFromSequence( const Sequence<BYTE> seqBytes , XMultiServiceFactoryRef &xSMgr )
303 XInterfaceRef xOutStreamService = xSMgr->createInstance( L"com.sun.star.io.Pipe" );
304 OSL_ASSERT( xOutStreamService.is() );
305 XOutputStreamRef rOutStream( xOutStreamService , USR_QUERY );
306 OSL_ASSERT( rOutStream.is() );
308 XInputStreamRef rInStream( xOutStreamService , USR_QUERY );
309 OSL_ASSERT( rInStream.is() );
311 rOutStream->writeBytes( seqBytes );
312 rOutStream->flush();
313 rOutStream->closeOutput();
315 return rInStream;
318 XInputStreamRef createStreamFromFile( const char *pcFile , XMultiServiceFactoryRef &xSMgr )
320 FILE *f = fopen( pcFile , "rb" );
321 XInputStreamRef r;
323 if( f ) {
324 fseek( f , 0 , SEEK_END );
325 int nLength = ftell( f );
326 fseek( f , 0 , SEEK_SET );
328 Sequence<BYTE> seqIn(nLength);
329 fread( seqIn.getArray() , nLength , 1 , f );
331 r = createStreamFromSequence( seqIn , xSMgr );
332 fclose( f );
334 return r;
345 #define PCHAR_TO_USTRING(x) StringToUString(String(x),CHARSET_PC_1252)
346 #define USTRING_TO_PCHAR(x) UStringToString(x,CHARSET_PC_437).GetStr()
350 class TestDocumentHandler :
351 public XExtendedDocumentHandler,
352 public XEntityResolver,
353 public XErrorHandler,
354 public OWeakObject
356 public:
357 TestDocumentHandler( XMultiServiceFactoryRef &r , BOOL bPrint )
359 m_xSMgr = r;
360 m_bPrint = bPrint;
364 public:
365 BOOL queryInterface( Uik aUik, XInterfaceRef & rOut );
366 void acquire() { OWeakObject::acquire(); }
367 void release() { OWeakObject::release(); }
368 void* getImplementation(Reflection *p) { return OWeakObject::getImplementation(p); }
371 public: // Error handler
372 virtual void error(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) )
374 printf( "Error !\n" );
375 THROW( SAXException( L"error from error handler" , XInterfaceRef() , aSAXParseException ) );
377 virtual void fatalError(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) )
379 printf( "Fatal Error !\n" );
381 virtual void warning(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) )
383 printf( "Warning !\n" );
387 public: // ExtendedDocumentHandler
389 virtual void startDocument(void) THROWS( (SAXException, UsrSystemException) )
391 m_iLevel = 0;
392 m_iElementCount = 0;
393 m_iAttributeCount = 0;
394 m_iWhitespaceCount =0;
395 m_iCharCount=0;
396 if( m_bPrint ) {
397 printf( "document started\n" );
400 virtual void endDocument(void) THROWS( (SAXException, UsrSystemException) )
402 if( m_bPrint ) {
403 printf( "document finished\n" );
404 printf( "(ElementCount %d),(AttributeCount %d),(WhitespaceCount %d),(CharCount %d)\n",
405 m_iElementCount, m_iAttributeCount, m_iWhitespaceCount , m_iCharCount );
408 virtual void startElement(const UString& aName, const XAttributeListRef& xAttribs)
409 THROWS( (SAXException,UsrSystemException) )
412 if( m_rLocator.is() ) {
413 if( m_bPrint )
414 printf( "%s(%d):" , USTRING_TO_PCHAR( m_rLocator->getSystemId() ) ,
415 m_rLocator->getLineNumber() );
417 if( m_bPrint ) {
418 int i;
419 for( i = 0; i < m_iLevel ; i ++ ) {
420 printf( " " );
422 printf( "<%s> " , USTRING_TO_PCHAR( aName ) );
424 for( i = 0 ; i < xAttribs->getLength() ; i ++ ) {
425 printf( "(%s,%s,'%s')" , USTRING_TO_PCHAR( xAttribs->getNameByIndex( i ) ) ,
426 USTRING_TO_PCHAR( xAttribs->getTypeByIndex( i ) ) ,
427 USTRING_TO_PCHAR( xAttribs->getValueByIndex( i ) ) );
429 printf( "\n" );
431 m_iLevel ++;
432 m_iElementCount ++;
433 m_iAttributeCount += xAttribs->getLength();
435 virtual void endElement(const UString& aName) THROWS( (SAXException,UsrSystemException) )
437 OSL_ASSERT( m_iLevel );
438 m_iLevel --;
439 if( m_bPrint ) {
440 int i;
441 for( i = 0; i < m_iLevel ; i ++ ) {
442 printf( " " );
444 printf( "</%s>\n" , USTRING_TO_PCHAR( aName ) );
448 virtual void characters(const UString& aChars) THROWS( (SAXException,UsrSystemException) )
450 if( m_bPrint ) {
451 int i;
452 for( i = 0; i < m_iLevel ; i ++ ) {
453 printf( " " );
455 printf( "%s\n" , USTRING_TO_PCHAR( aChars ) );
457 m_iCharCount += aChars.len();
459 virtual void ignorableWhitespace(const UString& aWhitespaces) THROWS( (SAXException,UsrSystemException) )
461 m_iWhitespaceCount += aWhitespaces.len();
464 virtual void processingInstruction(const UString& aTarget, const UString& aData) THROWS( (SAXException,UsrSystemException) )
466 if( m_bPrint )
467 printf( "PI : %s,%s\n" , USTRING_TO_PCHAR( aTarget ) , USTRING_TO_PCHAR( aData ) );
470 virtual void setDocumentLocator(const XLocatorRef& xLocator) THROWS( (SAXException,UsrSystemException) )
472 m_rLocator = xLocator;
475 virtual InputSource resolveEntity(const UString& sPublicId, const UString& sSystemId)
476 THROWS( (SAXException,UsrSystemException) )
478 InputSource source;
479 source.sSystemId = sSystemId;
480 source.sPublicId = sPublicId;
481 source.aInputStream = createStreamFromFile( USTRING_TO_PCHAR( sSystemId ) , m_xSMgr );
483 return source;
486 virtual void startCDATA(void) THROWS( (SAXException,UsrSystemException) )
488 if( m_bPrint ) {
489 printf( "CDataStart :\n" );
492 virtual void endCDATA(void) THROWS( (SAXException,UsrSystemException) )
494 if( m_bPrint ) {
495 printf( "CEndStart :\n" );
498 virtual void comment(const UString& sComment) THROWS( (SAXException,UsrSystemException) )
500 if( m_bPrint ) {
501 printf( "<!--%s-->\n" , USTRING_TO_PCHAR( sComment ) );
504 virtual void unknown(const UString& sString) THROWS( (SAXException,UsrSystemException) )
506 if( m_bPrint ) {
507 printf( "UNKNOWN : {%s}\n" , USTRING_TO_PCHAR( sString ) );
511 virtual void allowLineBreak( void) THROWS( (SAXException, UsrSystemException ) )
517 public:
518 int m_iLevel;
519 int m_iElementCount;
520 int m_iAttributeCount;
521 int m_iWhitespaceCount;
522 int m_iCharCount;
523 BOOL m_bPrint;
525 XMultiServiceFactoryRef m_xSMgr;
526 XLocatorRef m_rLocator;
529 BOOL TestDocumentHandler::queryInterface( Uik aUik , XInterfaceRef & rOut )
531 if( aUik == XDocumentHandler::getSmartUik() ) {
532 rOut = (XDocumentHandler * )this;
534 else if ( aUik == XExtendedDocumentHandler::getSmartUik() ) {
535 rOut = (XExtendedDocumentHandler *) this;
537 else if ( aUik == XEntityResolver::getSmartUik() ) {
538 rOut = (XEntityResolver *) this;
540 else if ( aUik == XErrorHandler::getSmartUik() ) {
541 rOut = (XErrorHandler * ) this;
543 else {
544 return OWeakObject::queryInterface( aUik , rOut );
546 return TRUE;
552 void OSaxParserTest::testSimple( const XParserRef &rParser )
555 char TestString[] =
556 "<!DOCTYPE personnel [\n"
557 "<!ENTITY testInternal \"internal Test!\">\n"
558 "<!ENTITY test SYSTEM \"external_entity.xml\">\n"
559 "]>\n"
561 "<personnel>\n"
562 "<person> fjklsfdklsdfkl\n"
563 "fjklsfdklsdfkl\n"
564 "<?testpi pidata?>\n"
565 "&testInternal;\n"
566 "<HUHU x='5' y='kjfd'> blahuhu\n"
567 "<HI> blahi\n"
568 " <![CDATA[<greeting>Hello, '+1+12world!</greeting>]]>\n"
569 " <!-- huhu <jdk> -->\n"
570 "<?testpi pidata?>\n"
571 "</HI>\n"
572 "aus XMLTest\n"
573 "</HUHU>\n"
574 "</person>\n"
575 "</personnel>\n\n\n";
577 Sequence<BYTE> seqBytes( strlen( TestString ) );
578 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
581 XInputStreamRef rInStream;
582 UString sInput;
583 rInStream = createStreamFromSequence( seqBytes , m_rFactory );
584 sInput = UString( L"internal" );
586 if( rParser.is() ) {
587 InputSource source;
589 source.aInputStream = rInStream;
590 source.sSystemId = sInput;
592 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE );
593 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY );
594 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY );
596 rParser->setDocumentHandler( rDocHandler );
597 rParser->setEntityResolver( rEntityResolver );
599 try {
600 rParser->parseStream( source );
601 ERROR_ASSERT( pDocHandler->m_iElementCount == 4 , "wrong element count" );
602 ERROR_ASSERT( pDocHandler->m_iAttributeCount == 2 , "wrong attribut count" );
603 ERROR_ASSERT( pDocHandler->m_iCharCount == 130 , "wrong char count" );
604 ERROR_ASSERT( pDocHandler->m_iWhitespaceCount == 0, "wrong whitespace count" );
606 catch( SAXParseException& e ) {
607 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
609 catch( SAXException& e ) {
610 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
613 catch( Exception& e ) {
614 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
616 catch(...) {
617 BUILD_ERROR( 1 , "unknown exception" );
625 void OSaxParserTest::testNamespaces( const XParserRef &rParser )
628 char TestString[] =
629 "<?xml version='1.0'?>\n"
630 "<!-- all elements here are explicitly in the HTML namespace -->\n"
631 "<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>\n"
632 "<html:head><html:title>Frobnostication</html:title></html:head>\n"
633 "<html:body><html:p>Moved to \n"
634 "<html:a href='http://frob.com'>here.</html:a></html:p></html:body>\n"
635 "</html:html>\n";
637 Sequence<BYTE> seqBytes( strlen( TestString ) );
638 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
641 XInputStreamRef rInStream;
642 UString sInput;
644 rInStream = createStreamFromSequence( seqBytes , m_rFactory );
645 sInput = UString( L"internal" );
647 if( rParser.is() ) {
648 InputSource source;
650 source.aInputStream = rInStream;
651 source.sSystemId = sInput;
653 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE );
654 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY );
655 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY );
657 rParser->setDocumentHandler( rDocHandler );
658 rParser->setEntityResolver( rEntityResolver );
660 try {
661 rParser->parseStream( source );
662 ERROR_ASSERT( pDocHandler->m_iElementCount == 6 , "wrong element count" );
663 ERROR_ASSERT( pDocHandler->m_iAttributeCount == 2 , "wrong attribut count" );
664 ERROR_ASSERT( pDocHandler->m_iCharCount == 33, "wrong char count" );
665 ERROR_ASSERT( pDocHandler->m_iWhitespaceCount == 0 , "wrong whitespace count" );
667 catch( SAXParseException& e ) {
668 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
670 catch( SAXException& e ) {
671 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
674 catch( Exception& e ) {
675 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
677 catch(...) {
678 BUILD_ERROR( 1 , "unknown exception" );
683 void OSaxParserTest::testEncoding( const XParserRef &rParser )
685 char TestString[] =
686 "<?xml version='1.0' encoding=\"iso-8859-1\"?>\n"
687 "<!-- all elements here are explicitly in the HTML namespace -->\n"
688 "<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>\n"
689 "<html:head><html:title>Frobnostication</html:title></html:head>\n"
690 "<html:body><html:p>Moved to ß\n"
691 "<html:a href='http://frob.com'>here.</html:a></html:p></html:body>\n"
692 "</html:html>\n";
694 Sequence<BYTE> seqBytes( strlen( TestString ) );
695 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) );
698 XInputStreamRef rInStream;
699 UString sInput;
701 rInStream = createStreamFromSequence( seqBytes , m_rFactory );
702 sInput = UString( L"internal" );
704 if( rParser.is() ) {
705 InputSource source;
707 source.aInputStream = rInStream;
708 source.sSystemId = sInput;
710 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE );
711 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY );
712 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY );
714 rParser->setDocumentHandler( rDocHandler );
715 rParser->setEntityResolver( rEntityResolver );
716 try {
717 rParser->parseStream( source );
719 catch( SAXParseException& e ) {
720 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
722 catch( SAXException& e ) {
723 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
726 catch( Exception& e ) {
727 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) );
729 catch(...) {
730 BUILD_ERROR( 1 , "unknown exception" );
737 void OSaxParserTest::testFile( const XParserRef & rParser )
740 XInputStreamRef rInStream = createStreamFromFile( "testsax.xml" , m_rFactory );
741 UString sInput = UString( PCHAR_TO_USTRING( "testsax.xml" ) );
744 if( rParser.is() && rInStream.is() ) {
745 InputSource source;
747 source.aInputStream = rInStream;
748 source.sSystemId = sInput;
750 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , TRUE );
751 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY );
752 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY );
753 XErrorHandlerRef rErrorHandler( ( XErrorHandler * )pDocHandler , USR_QUERY );
755 rParser->setDocumentHandler( rDocHandler );
756 rParser->setEntityResolver( rEntityResolver );
757 rParser->setErrorHandler( rErrorHandler );
759 try {
760 rParser->parseStream( source );
762 catch( SAXParseException& e ) {
763 UsrAny any;
764 any.set( &e , SAXParseException_getReflection() );
765 while(TRUE) {
766 SAXParseException *pEx;
767 if( any.getReflection() == SAXParseException_getReflection() ) {
768 pEx = ( SAXParseException * ) any.get();
769 printf( "%s\n" , UStringToString( pEx->Message , CHARSET_SYSTEM ).GetStr() );
770 any = pEx->WrappedException;
772 else {
773 break;
777 catch( SAXException& e ) {
778 printf( "%s\n" , UStringToString( e.Message , CHARSET_SYSTEM ).GetStr() );
781 catch( Exception& e ) {
782 printf( "normal exception ! %s\n", e.getName() );
784 catch(...) {
785 printf( "any exception !!!!\n" );
790 void OSaxParserTest::testPerformance( const XParserRef & rParser )
793 XInputStreamRef rInStream = createStreamFromFile( "testPerformance.xml" , m_rFactory );
794 UString sInput = UString( PCHAR_TO_USTRING( "testperformance.xml" ) );
796 if( rParser.is() && rInStream.is() ) {
797 InputSource source;
799 source.aInputStream = rInStream;
800 source.sSystemId = sInput;
802 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE );
803 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY );
804 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY );
805 XErrorHandlerRef rErrorHandler( ( XErrorHandler * )pDocHandler , USR_QUERY );
807 rParser->setDocumentHandler( rDocHandler );
808 rParser->setEntityResolver( rEntityResolver );
809 rParser->setErrorHandler( rErrorHandler );
811 try {
812 TimeValue aStartTime, aEndTime;
813 osl_getSystemTime( &aStartTime );
814 rParser->parseStream( source );
815 osl_getSystemTime( &aEndTime );
817 double fStart = (double)aStartTime.Seconds + ((double)aStartTime.Nanosec / 1000000000.0);
818 double fEnd = (double)aEndTime.Seconds + ((double)aEndTime.Nanosec / 1000000000.0);
820 printf( "Performance reading : %g s\n" , fEnd - fStart );
823 catch( SAXParseException& e ) {
824 UsrAny any;
825 any.set( &e , SAXParseException_getReflection() );
826 while(TRUE) {
827 SAXParseException *pEx;
828 if( any.getReflection() == SAXParseException_getReflection() ) {
829 pEx = ( SAXParseException * ) any.get();
830 printf( "%s\n" , UStringToString( pEx->Message , CHARSET_SYSTEM ).GetStr() );
831 any = pEx->WrappedException;
833 else {
834 break;
838 catch( SAXException& e ) {
839 printf( "%s\n" , UStringToString( e.Message , CHARSET_SYSTEM ).GetStr() );
842 catch( Exception& e ) {
843 printf( "normal exception ! %s\n", e.getName() );
845 catch(...) {
846 printf( "any exception !!!!\n" );
853 extern "C"
855 BOOL EXTERN_SERVICE_CALLTYPE exService_writeRegEntry(
856 const UNO_INTERFACE(XRegistryKey)* xUnoKey)
859 XRegistryKeyRef xKey;
860 uno2smart(xKey, *xUnoKey);
862 UString str = UString( L"/" ) + OSaxParserTest_getImplementationName() + UString( L"/UNO/SERVICES" );
863 XRegistryKeyRef xNewKey = xKey->createKey( str );
864 xNewKey->createKey( OSaxParserTest_getServiceName() );
866 str = UString( L"/" ) + OSaxWriterTest_getImplementationName() + UString( L"/UNO/SERVICES" );
867 xNewKey = xKey->createKey( str );
868 xNewKey->createKey( OSaxWriterTest_getServiceName() );
870 return TRUE;
874 UNO_INTERFACE(XInterface) EXTERN_SERVICE_CALLTYPE exService_getFactory
876 const wchar_t* implementationName,
877 const UNO_INTERFACE(XMultiServiceFactory)* xUnoFact,
878 const UNO_INTERFACE(XRegistryKey)*
881 UNO_INTERFACE(XInterface) xUnoRet = {0, 0};
883 XInterfaceRef xRet;
884 XMultiServiceFactoryRef xSMgr;
885 UString aImplementationName(implementationName);
887 uno2smart(xSMgr, *xUnoFact);
889 if (aImplementationName == OSaxWriterTest_getImplementationName() )
891 xRet = createSingleFactory( xSMgr, implementationName,
892 OSaxWriterTest_CreateInstance,
893 OSaxWriterTest_getSupportedServiceNames() );
895 else if (aImplementationName == OSaxParserTest_getImplementationName() )
897 xRet = createSingleFactory( xSMgr, implementationName,
898 OSaxParserTest_CreateInstance,
899 OSaxParserTest_getSupportedServiceNames() );
901 if (xRet.is())
903 smart2uno(xRet, xUnoRet);
906 return xUnoRet;