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 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_extensions.hxx" 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <smart/com/sun/star/test/XSimpleTest.hxx> 34 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> 42 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> 47 48 #include <smart/com/sun/star/lang/XMultiServiceFactory.hxx> // for the multiservice-factories 49 #include <usr/factoryhlp.hxx> 50 51 #include <usr/reflserv.hxx> // for EXTERN_SERVICE_CALLTYPE 52 53 #include "factory.hxx" 54 55 using namespace rtl; 56 using namespace vos; 57 using namespace usr; 58 59 60 #define BUILD_ERROR(expr, Message)\ 61 {\ 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 ); \ 75 }\ 76 ((void)0) 77 78 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; \ 94 }\ 95 ((void)0) 96 97 #define ERROR_ASSERT(expr, Message) \ 98 if( ! (expr) ) { \ 99 BUILD_ERROR(expr, Message );\ 100 return; \ 101 }\ 102 ((void)0) 103 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; \ 109 } \ 110 ((void)0) 111 112 /**** 113 * test szenarios : 114 * 115 * 116 * 117 ****/ 118 119 120 121 class OSaxParserTest : 122 public XSimpleTest, 123 public OWeakObject 124 { 125 public: 126 OSaxParserTest( const XMultiServiceFactoryRef & rFactory ) : m_rFactory( rFactory ) 127 { 128 129 } 130 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); } 136 137 public: 138 virtual void testInvariant(const UString& TestName, const XInterfaceRef& TestObject) 139 THROWS( ( IllegalArgumentException, 140 UsrSystemException) ); 141 142 virtual INT32 test( const UString& TestName, 143 const XInterfaceRef& TestObject, 144 INT32 hTestHandle) THROWS( ( IllegalArgumentException, 145 UsrSystemException) ); 146 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) ); 151 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 ); 158 159 private: 160 Sequence<UsrAny> m_seqExceptions; 161 Sequence<UString> m_seqErrors; 162 Sequence<UString> m_seqWarnings; 163 XMultiServiceFactoryRef m_rFactory; 164 }; 165 166 167 168 /** 169 * for external binding 170 * 171 * 172 **/ 173 XInterfaceRef OSaxParserTest_CreateInstance( const XMultiServiceFactoryRef & rSMgr ) THROWS((Exception)) 174 { 175 OSaxParserTest *p = new OSaxParserTest( rSMgr ); 176 XInterfaceRef xService = *p; 177 return xService; 178 } 179 180 181 UString OSaxParserTest_getServiceName( ) THROWS( () ) 182 { 183 return L"test.com.sun.star.xml.sax.Parser"; 184 } 185 186 UString OSaxParserTest_getImplementationName( ) THROWS( () ) 187 { 188 return L"test.extensions.xml.sax.Parser"; 189 } 190 191 Sequence<UString> OSaxParserTest_getSupportedServiceNames( ) THROWS( () ) 192 { 193 Sequence<UString> aRet(1); 194 195 aRet.getArray()[0] = OSaxParserTest_getImplementationName( ); 196 197 return aRet; 198 } 199 200 201 BOOL OSaxParserTest::queryInterface( Uik uik , XInterfaceRef &rOut ) 202 { 203 if( XSimpleTest::getSmartUik() == uik ) { 204 rOut = (XSimpleTest *) this; 205 } 206 else { 207 return OWeakObject::queryInterface( uik , rOut ); 208 } 209 return TRUE; 210 } 211 212 213 void OSaxParserTest::testInvariant( const UString& TestName, const XInterfaceRef& TestObject ) 214 THROWS( ( IllegalArgumentException, 215 UsrSystemException) ) 216 { 217 if( L"com.sun.star.xml.sax.Parser" == TestName ) { 218 XParserRef parser( TestObject , USR_QUERY ); 219 220 ERROR_ASSERT( parser.is() , "XDataInputStream cannot be queried" ); 221 } 222 } 223 224 225 INT32 OSaxParserTest::test( const UString& TestName, 226 const XInterfaceRef& TestObject, 227 INT32 hTestHandle) THROWS( ( IllegalArgumentException, 228 UsrSystemException) ) 229 { 230 if( L"com.sun.star.xml.sax.Parser" == TestName ) { 231 try { 232 if( 0 == hTestHandle ) { 233 testInvariant( TestName , TestObject ); 234 } 235 else { 236 237 XParserRef parser( TestObject , USR_QUERY ); 238 239 if( 1 == hTestHandle ) { 240 testSimple( parser ); 241 } 242 else if( 2 == hTestHandle ) { 243 testNamespaces( parser ); 244 } 245 else if( 3 == hTestHandle ) { 246 testEncoding( parser ); 247 } 248 else if( 4 == hTestHandle ) { 249 testFile( parser ); 250 } 251 else if( 5 == hTestHandle ) { 252 testPerformance( parser ); 253 } 254 } 255 } 256 catch( Exception& e ) { 257 BUILD_ERROR( 0 , UStringToString( e.getName() , CHARSET_SYSTEM ).GetCharStr() ); 258 } 259 catch(...) { 260 BUILD_ERROR( 0 , "unknown exception (Exception is not base class)" ); 261 } 262 263 hTestHandle ++; 264 265 if( hTestHandle >= 6) { 266 // all tests finished. 267 hTestHandle = -1; 268 } 269 } 270 else { 271 BUILD_ERROR( 0 , "service not supported by test." ); 272 } 273 return hTestHandle; 274 } 275 276 277 278 BOOL OSaxParserTest::testPassed(void) THROWS( (UsrSystemException) ) 279 { 280 return m_seqErrors.getLen() == 0; 281 } 282 283 284 Sequence< UString > OSaxParserTest::getErrors(void) THROWS( (UsrSystemException) ) 285 { 286 return m_seqErrors; 287 } 288 289 290 Sequence< UsrAny > OSaxParserTest::getErrorExceptions(void) THROWS( (UsrSystemException) ) 291 { 292 return m_seqExceptions; 293 } 294 295 296 Sequence< UString > OSaxParserTest::getWarnings(void) THROWS( (UsrSystemException) ) 297 { 298 return m_seqWarnings; 299 } 300 301 XInputStreamRef createStreamFromSequence( const Sequence<BYTE> seqBytes , XMultiServiceFactoryRef &xSMgr ) 302 { 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() ); 307 308 XInputStreamRef rInStream( xOutStreamService , USR_QUERY ); 309 OSL_ASSERT( rInStream.is() ); 310 311 rOutStream->writeBytes( seqBytes ); 312 rOutStream->flush(); 313 rOutStream->closeOutput(); 314 315 return rInStream; 316 } 317 318 XInputStreamRef createStreamFromFile( const char *pcFile , XMultiServiceFactoryRef &xSMgr ) 319 { 320 FILE *f = fopen( pcFile , "rb" ); 321 XInputStreamRef r; 322 323 if( f ) { 324 fseek( f , 0 , SEEK_END ); 325 int nLength = ftell( f ); 326 fseek( f , 0 , SEEK_SET ); 327 328 Sequence<BYTE> seqIn(nLength); 329 fread( seqIn.getArray() , nLength , 1 , f ); 330 331 r = createStreamFromSequence( seqIn , xSMgr ); 332 fclose( f ); 333 } 334 return r; 335 } 336 337 338 339 340 341 342 343 344 345 #define PCHAR_TO_USTRING(x) StringToUString(String(x),CHARSET_PC_1252) 346 #define USTRING_TO_PCHAR(x) UStringToString(x,CHARSET_PC_437).GetStr() 347 348 349 350 class TestDocumentHandler : 351 public XExtendedDocumentHandler, 352 public XEntityResolver, 353 public XErrorHandler, 354 public OWeakObject 355 { 356 public: 357 TestDocumentHandler( XMultiServiceFactoryRef &r , BOOL bPrint ) 358 { 359 m_xSMgr = r; 360 m_bPrint = bPrint; 361 } 362 363 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); } 369 370 371 public: // Error handler 372 virtual void error(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) ) 373 { 374 printf( "Error !\n" ); 375 THROW( SAXException( L"error from error handler" , XInterfaceRef() , aSAXParseException ) ); 376 } 377 virtual void fatalError(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) ) 378 { 379 printf( "Fatal Error !\n" ); 380 } 381 virtual void warning(const UsrAny& aSAXParseException) THROWS( (SAXException, UsrSystemException) ) 382 { 383 printf( "Warning !\n" ); 384 } 385 386 387 public: // ExtendedDocumentHandler 388 389 virtual void startDocument(void) THROWS( (SAXException, UsrSystemException) ) 390 { 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" ); 398 } 399 } 400 virtual void endDocument(void) THROWS( (SAXException, UsrSystemException) ) 401 { 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 ); 406 } 407 } 408 virtual void startElement(const UString& aName, const XAttributeListRef& xAttribs) 409 THROWS( (SAXException,UsrSystemException) ) 410 { 411 412 if( m_rLocator.is() ) { 413 if( m_bPrint ) 414 printf( "%s(%d):" , USTRING_TO_PCHAR( m_rLocator->getSystemId() ) , 415 m_rLocator->getLineNumber() ); 416 } 417 if( m_bPrint ) { 418 int i; 419 for( i = 0; i < m_iLevel ; i ++ ) { 420 printf( " " ); 421 } 422 printf( "<%s> " , USTRING_TO_PCHAR( aName ) ); 423 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 ) ) ); 428 } 429 printf( "\n" ); 430 } 431 m_iLevel ++; 432 m_iElementCount ++; 433 m_iAttributeCount += xAttribs->getLength(); 434 } 435 virtual void endElement(const UString& aName) THROWS( (SAXException,UsrSystemException) ) 436 { 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( " " ); 443 } 444 printf( "</%s>\n" , USTRING_TO_PCHAR( aName ) ); 445 } 446 } 447 448 virtual void characters(const UString& aChars) THROWS( (SAXException,UsrSystemException) ) 449 { 450 if( m_bPrint ) { 451 int i; 452 for( i = 0; i < m_iLevel ; i ++ ) { 453 printf( " " ); 454 } 455 printf( "%s\n" , USTRING_TO_PCHAR( aChars ) ); 456 } 457 m_iCharCount += aChars.len(); 458 } 459 virtual void ignorableWhitespace(const UString& aWhitespaces) THROWS( (SAXException,UsrSystemException) ) 460 { 461 m_iWhitespaceCount += aWhitespaces.len(); 462 } 463 464 virtual void processingInstruction(const UString& aTarget, const UString& aData) THROWS( (SAXException,UsrSystemException) ) 465 { 466 if( m_bPrint ) 467 printf( "PI : %s,%s\n" , USTRING_TO_PCHAR( aTarget ) , USTRING_TO_PCHAR( aData ) ); 468 } 469 470 virtual void setDocumentLocator(const XLocatorRef& xLocator) THROWS( (SAXException,UsrSystemException) ) 471 { 472 m_rLocator = xLocator; 473 } 474 475 virtual InputSource resolveEntity(const UString& sPublicId, const UString& sSystemId) 476 THROWS( (SAXException,UsrSystemException) ) 477 { 478 InputSource source; 479 source.sSystemId = sSystemId; 480 source.sPublicId = sPublicId; 481 source.aInputStream = createStreamFromFile( USTRING_TO_PCHAR( sSystemId ) , m_xSMgr ); 482 483 return source; 484 } 485 486 virtual void startCDATA(void) THROWS( (SAXException,UsrSystemException) ) 487 { 488 if( m_bPrint ) { 489 printf( "CDataStart :\n" ); 490 } 491 } 492 virtual void endCDATA(void) THROWS( (SAXException,UsrSystemException) ) 493 { 494 if( m_bPrint ) { 495 printf( "CEndStart :\n" ); 496 } 497 } 498 virtual void comment(const UString& sComment) THROWS( (SAXException,UsrSystemException) ) 499 { 500 if( m_bPrint ) { 501 printf( "<!--%s-->\n" , USTRING_TO_PCHAR( sComment ) ); 502 } 503 } 504 virtual void unknown(const UString& sString) THROWS( (SAXException,UsrSystemException) ) 505 { 506 if( m_bPrint ) { 507 printf( "UNKNOWN : {%s}\n" , USTRING_TO_PCHAR( sString ) ); 508 } 509 } 510 511 virtual void allowLineBreak( void) THROWS( (SAXException, UsrSystemException ) ) 512 { 513 514 } 515 516 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; 524 525 XMultiServiceFactoryRef m_xSMgr; 526 XLocatorRef m_rLocator; 527 }; 528 529 BOOL TestDocumentHandler::queryInterface( Uik aUik , XInterfaceRef & rOut ) 530 { 531 if( aUik == XDocumentHandler::getSmartUik() ) { 532 rOut = (XDocumentHandler * )this; 533 } 534 else if ( aUik == XExtendedDocumentHandler::getSmartUik() ) { 535 rOut = (XExtendedDocumentHandler *) this; 536 } 537 else if ( aUik == XEntityResolver::getSmartUik() ) { 538 rOut = (XEntityResolver *) this; 539 } 540 else if ( aUik == XErrorHandler::getSmartUik() ) { 541 rOut = (XErrorHandler * ) this; 542 } 543 else { 544 return OWeakObject::queryInterface( aUik , rOut ); 545 } 546 return TRUE; 547 } 548 549 550 551 552 void OSaxParserTest::testSimple( const XParserRef &rParser ) 553 { 554 555 char TestString[] = 556 "<!DOCTYPE personnel [\n" 557 "<!ENTITY testInternal \"internal Test!\">\n" 558 "<!ENTITY test SYSTEM \"external_entity.xml\">\n" 559 "]>\n" 560 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"; 576 577 Sequence<BYTE> seqBytes( strlen( TestString ) ); 578 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) ); 579 580 581 XInputStreamRef rInStream; 582 UString sInput; 583 rInStream = createStreamFromSequence( seqBytes , m_rFactory ); 584 sInput = UString( L"internal" ); 585 586 if( rParser.is() ) { 587 InputSource source; 588 589 source.aInputStream = rInStream; 590 source.sSystemId = sInput; 591 592 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE ); 593 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY ); 594 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY ); 595 596 rParser->setDocumentHandler( rDocHandler ); 597 rParser->setEntityResolver( rEntityResolver ); 598 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" ); 605 } 606 catch( SAXParseException& e ) { 607 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 608 } 609 catch( SAXException& e ) { 610 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 611 612 } 613 catch( Exception& e ) { 614 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 615 } 616 catch(...) { 617 BUILD_ERROR( 1 , "unknown exception" ); 618 } 619 620 } 621 622 623 } 624 625 void OSaxParserTest::testNamespaces( const XParserRef &rParser ) 626 { 627 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"; 636 637 Sequence<BYTE> seqBytes( strlen( TestString ) ); 638 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) ); 639 640 641 XInputStreamRef rInStream; 642 UString sInput; 643 644 rInStream = createStreamFromSequence( seqBytes , m_rFactory ); 645 sInput = UString( L"internal" ); 646 647 if( rParser.is() ) { 648 InputSource source; 649 650 source.aInputStream = rInStream; 651 source.sSystemId = sInput; 652 653 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE ); 654 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY ); 655 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY ); 656 657 rParser->setDocumentHandler( rDocHandler ); 658 rParser->setEntityResolver( rEntityResolver ); 659 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" ); 666 } 667 catch( SAXParseException& e ) { 668 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 669 } 670 catch( SAXException& e ) { 671 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 672 673 } 674 catch( Exception& e ) { 675 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 676 } 677 catch(...) { 678 BUILD_ERROR( 1 , "unknown exception" ); 679 } 680 } 681 } 682 683 void OSaxParserTest::testEncoding( const XParserRef &rParser ) 684 { 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"; 693 694 Sequence<BYTE> seqBytes( strlen( TestString ) ); 695 memcpy( seqBytes.getArray() , TestString , strlen( TestString ) ); 696 697 698 XInputStreamRef rInStream; 699 UString sInput; 700 701 rInStream = createStreamFromSequence( seqBytes , m_rFactory ); 702 sInput = UString( L"internal" ); 703 704 if( rParser.is() ) { 705 InputSource source; 706 707 source.aInputStream = rInStream; 708 source.sSystemId = sInput; 709 710 TestDocumentHandler *pDocHandler = new TestDocumentHandler( m_rFactory , FALSE ); 711 XDocumentHandlerRef rDocHandler( (XDocumentHandler *) pDocHandler , USR_QUERY ); 712 XEntityResolverRef rEntityResolver( (XEntityResolver *) pDocHandler , USR_QUERY ); 713 714 rParser->setDocumentHandler( rDocHandler ); 715 rParser->setEntityResolver( rEntityResolver ); 716 try { 717 rParser->parseStream( source ); 718 } 719 catch( SAXParseException& e ) { 720 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 721 } 722 catch( SAXException& e ) { 723 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 724 725 } 726 catch( Exception& e ) { 727 BUILD_ERROR( 1 , USTRING_TO_PCHAR( e.Message ) ); 728 } 729 catch(...) { 730 BUILD_ERROR( 1 , "unknown exception" ); 731 } 732 733 } 734 735 } 736 737 void OSaxParserTest::testFile( const XParserRef & rParser ) 738 { 739 740 XInputStreamRef rInStream = createStreamFromFile( "testsax.xml" , m_rFactory ); 741 UString sInput = UString( PCHAR_TO_USTRING( "testsax.xml" ) ); 742 743 744 if( rParser.is() && rInStream.is() ) { 745 InputSource source; 746 747 source.aInputStream = rInStream; 748 source.sSystemId = sInput; 749 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 ); 754 755 rParser->setDocumentHandler( rDocHandler ); 756 rParser->setEntityResolver( rEntityResolver ); 757 rParser->setErrorHandler( rErrorHandler ); 758 759 try { 760 rParser->parseStream( source ); 761 } 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; 771 } 772 else { 773 break; 774 } 775 } 776 } 777 catch( SAXException& e ) { 778 printf( "%s\n" , UStringToString( e.Message , CHARSET_SYSTEM ).GetStr() ); 779 780 } 781 catch( Exception& e ) { 782 printf( "normal exception ! %s\n", e.getName() ); 783 } 784 catch(...) { 785 printf( "any exception !!!!\n" ); 786 } 787 } 788 } 789 790 void OSaxParserTest::testPerformance( const XParserRef & rParser ) 791 { 792 793 XInputStreamRef rInStream = createStreamFromFile( "testPerformance.xml" , m_rFactory ); 794 UString sInput = UString( PCHAR_TO_USTRING( "testperformance.xml" ) ); 795 796 if( rParser.is() && rInStream.is() ) { 797 InputSource source; 798 799 source.aInputStream = rInStream; 800 source.sSystemId = sInput; 801 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 ); 806 807 rParser->setDocumentHandler( rDocHandler ); 808 rParser->setEntityResolver( rEntityResolver ); 809 rParser->setErrorHandler( rErrorHandler ); 810 811 try { 812 TimeValue aStartTime, aEndTime; 813 osl_getSystemTime( &aStartTime ); 814 rParser->parseStream( source ); 815 osl_getSystemTime( &aEndTime ); 816 817 double fStart = (double)aStartTime.Seconds + ((double)aStartTime.Nanosec / 1000000000.0); 818 double fEnd = (double)aEndTime.Seconds + ((double)aEndTime.Nanosec / 1000000000.0); 819 820 printf( "Performance reading : %g s\n" , fEnd - fStart ); 821 822 } 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; 832 } 833 else { 834 break; 835 } 836 } 837 } 838 catch( SAXException& e ) { 839 printf( "%s\n" , UStringToString( e.Message , CHARSET_SYSTEM ).GetStr() ); 840 841 } 842 catch( Exception& e ) { 843 printf( "normal exception ! %s\n", e.getName() ); 844 } 845 catch(...) { 846 printf( "any exception !!!!\n" ); 847 } 848 } 849 850 } 851 852 853 extern "C" 854 { 855 BOOL EXTERN_SERVICE_CALLTYPE exService_writeRegEntry( 856 const UNO_INTERFACE(XRegistryKey)* xUnoKey) 857 858 { 859 XRegistryKeyRef xKey; 860 uno2smart(xKey, *xUnoKey); 861 862 UString str = UString( L"/" ) + OSaxParserTest_getImplementationName() + UString( L"/UNO/SERVICES" ); 863 XRegistryKeyRef xNewKey = xKey->createKey( str ); 864 xNewKey->createKey( OSaxParserTest_getServiceName() ); 865 866 str = UString( L"/" ) + OSaxWriterTest_getImplementationName() + UString( L"/UNO/SERVICES" ); 867 xNewKey = xKey->createKey( str ); 868 xNewKey->createKey( OSaxWriterTest_getServiceName() ); 869 870 return TRUE; 871 } 872 873 874 UNO_INTERFACE(XInterface) EXTERN_SERVICE_CALLTYPE exService_getFactory 875 ( 876 const wchar_t* implementationName, 877 const UNO_INTERFACE(XMultiServiceFactory)* xUnoFact, 878 const UNO_INTERFACE(XRegistryKey)* 879 ) 880 { 881 UNO_INTERFACE(XInterface) xUnoRet = {0, 0}; 882 883 XInterfaceRef xRet; 884 XMultiServiceFactoryRef xSMgr; 885 UString aImplementationName(implementationName); 886 887 uno2smart(xSMgr, *xUnoFact); 888 889 if (aImplementationName == OSaxWriterTest_getImplementationName() ) 890 { 891 xRet = createSingleFactory( xSMgr, implementationName, 892 OSaxWriterTest_CreateInstance, 893 OSaxWriterTest_getSupportedServiceNames() ); 894 } 895 else if (aImplementationName == OSaxParserTest_getImplementationName() ) 896 { 897 xRet = createSingleFactory( xSMgr, implementationName, 898 OSaxParserTest_CreateInstance, 899 OSaxParserTest_getSupportedServiceNames() ); 900 } 901 if (xRet.is()) 902 { 903 smart2uno(xRet, xUnoRet); 904 } 905 906 return xUnoRet; 907 } 908 909 } 910 911 912