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