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