lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sax / test / saxdemo.cxx
blobf00d598b722f3e95bf17b5987b0a0e08fde06337
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // testcomponent - Loads a service and its testcomponent from dlls performs a test.
22 // Expands the dll-names depending on the actual environment.
23 // Example : testcomponent com.sun.star.io.Pipe stm
25 // Therefore the testcode must exist in teststm and the testservice must be named test.com.sun.star.uno.io.Pipe
28 #include <stdio.h>
29 #include <vector>
30 #include <cstring>
32 #include <com/sun/star/registry/XImplementationRegistration.hpp>
33 #include <com/sun/star/lang/XComponent.hpp>
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>
39 #include <com/sun/star/io/XOutputStream.hpp>
40 #include <com/sun/star/io/XActiveDataSource.hpp>
42 #include <cppuhelper/servicefactory.hxx>
43 #include <cppuhelper/implbase.hxx>
46 using namespace ::std;
47 using namespace ::cppu;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::registry;
51 using namespace ::com::sun::star::xml::sax;
52 using namespace ::com::sun::star::io;
55 /************
56 * Sequence of bytes -> InputStream
57 ************/
58 class OInputStream : public WeakImplHelper < XInputStream >
60 public:
61 explicit OInputStream( const Sequence< sal_Int8 >&seq ) :
62 m_seq( seq ),
63 nPos( 0 )
66 public:
67 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
68 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
70 nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ?
71 m_seq.getLength() - nPos :
72 nBytesToRead;
73 aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead );
74 nPos += nBytesToRead;
75 return nBytesToRead;
77 virtual sal_Int32 SAL_CALL readSomeBytes(
78 css::uno::Sequence< sal_Int8 >& aData,
79 sal_Int32 nMaxBytesToRead )
80 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
82 return readBytes( aData, nMaxBytesToRead );
84 virtual void SAL_CALL skipBytes( sal_Int32 /* nBytesToSkip */ )
85 throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
87 // not implemented
89 virtual sal_Int32 SAL_CALL available( )
90 throw(NotConnectedException, IOException, RuntimeException)
92 return m_seq.getLength() - nPos;
94 virtual void SAL_CALL closeInput( )
95 throw(NotConnectedException, IOException, RuntimeException)
97 // not needed
99 Sequence< sal_Int8> m_seq;
100 sal_Int32 nPos;
104 // Helper : create an input stream from a file
106 Reference< XInputStream > createStreamFromFile(
107 const char *pcFile )
109 FILE *f = fopen( pcFile , "rb" );
110 Reference< XInputStream > r;
112 if( f ) {
113 fseek( f , 0 , SEEK_END );
114 int nLength = ftell( f );
115 fseek( f , 0 , SEEK_SET );
117 Sequence<sal_Int8> seqIn(nLength);
118 fread( seqIn.getArray() , nLength , 1 , f );
120 r.set( new OInputStream( seqIn ) );
121 fclose( f );
123 return r;
127 // The document handler, which is needed for the saxparser
128 // The Documenthandler for reading sax
130 class TestDocumentHandler :
131 public WeakImplHelper< XExtendedDocumentHandler , XEntityResolver , XErrorHandler >
133 public:
134 TestDocumentHandler( )
138 public: // Error handler
139 virtual void SAL_CALL error(const Any& aSAXParseException) throw (SAXException, RuntimeException)
141 printf( "Error !\n" );
142 throw SAXException(
143 OUString( "error from error handler") ,
144 Reference < XInterface >() ,
145 aSAXParseException );
147 virtual void SAL_CALL fatalError(const Any& /* aSAXParseException */) throw (SAXException, RuntimeException)
149 printf( "Fatal Error !\n" );
151 virtual void SAL_CALL warning(const Any& /* aSAXParseException */) throw (SAXException, RuntimeException)
153 printf( "Warning !\n" );
157 public: // ExtendedDocumentHandler
159 virtual void SAL_CALL startDocument() throw (SAXException, RuntimeException)
161 m_iElementCount = 0;
162 m_iAttributeCount = 0;
163 m_iWhitespaceCount =0;
164 m_iCharCount=0;
165 printf( "document started\n" );
167 virtual void SAL_CALL endDocument() throw (SAXException, RuntimeException)
169 printf( "document finished\n" );
170 printf( "(ElementCount %d),(AttributeCount %d),(WhitespaceCount %d),(CharCount %d)\n",
171 m_iElementCount, m_iAttributeCount, m_iWhitespaceCount , m_iCharCount );
174 virtual void SAL_CALL startElement(const OUString& /* aName */,
175 const Reference< XAttributeList > & xAttribs)
176 throw (SAXException,RuntimeException)
178 m_iElementCount ++;
179 m_iAttributeCount += xAttribs->getLength();
182 virtual void SAL_CALL endElement(const OUString& /* aName */) throw (SAXException,RuntimeException)
184 // ignored
187 virtual void SAL_CALL characters(const OUString& aChars) throw (SAXException,RuntimeException)
189 m_iCharCount += aChars.getLength();
191 virtual void SAL_CALL ignorableWhitespace(const OUString& aWhitespaces) throw (SAXException,RuntimeException)
193 m_iWhitespaceCount += aWhitespaces.getLength();
196 virtual void SAL_CALL processingInstruction(const OUString& /* aTarget */, const OUString& /* aData */) throw (SAXException,RuntimeException)
198 // ignored
201 virtual void SAL_CALL setDocumentLocator(const Reference< XLocator> & /* xLocator */)
202 throw (SAXException,RuntimeException)
204 // ignored
207 virtual InputSource SAL_CALL resolveEntity(
208 const OUString& sPublicId,
209 const OUString& sSystemId)
210 throw (RuntimeException)
212 InputSource source;
213 source.sSystemId = sSystemId;
214 source.sPublicId = sPublicId;
216 source.aInputStream = createStreamFromFile(
217 OUStringToOString( sSystemId, RTL_TEXTENCODING_ASCII_US).getStr() );
219 return source;
222 virtual void SAL_CALL startCDATA() throw (SAXException,RuntimeException)
225 virtual void SAL_CALL endCDATA() throw (SAXException,RuntimeException)
228 virtual void SAL_CALL comment(const OUString& /* sComment */) throw (SAXException,RuntimeException)
231 virtual void SAL_CALL unknown(const OUString& /* sString */) throw (SAXException,RuntimeException)
235 virtual void SAL_CALL allowLineBreak() throw (SAXException, RuntimeException )
240 public:
241 int m_iElementCount;
242 int m_iAttributeCount;
243 int m_iWhitespaceCount;
244 int m_iCharCount;
248 // helper implementation for writing
249 // implements an XAttributeList
251 struct AttributeListImpl_impl;
252 class AttributeListImpl : public WeakImplHelper< XAttributeList >
254 public:
255 AttributeListImpl();
256 AttributeListImpl( const AttributeListImpl & );
257 ~AttributeListImpl();
259 public:
260 virtual sal_Int16 SAL_CALL getLength() throw (RuntimeException);
261 virtual OUString SAL_CALL getNameByIndex(sal_Int16 i) throw (RuntimeException);
262 virtual OUString SAL_CALL getTypeByIndex(sal_Int16 i) throw (RuntimeException);
263 virtual OUString SAL_CALL getTypeByName(const OUString& aName) throw (RuntimeException);
264 virtual OUString SAL_CALL getValueByIndex(sal_Int16 i) throw (RuntimeException);
265 virtual OUString SAL_CALL getValueByName(const OUString& aName) throw (RuntimeException);
267 public:
268 void addAttribute( const OUString &sName ,
269 const OUString &sType ,
270 const OUString &sValue );
271 void clear();
273 private:
274 struct AttributeListImpl_impl *m_pImpl;
278 struct TagAttribute
280 TagAttribute(){}
281 TagAttribute( const OUString &s_Name,
282 const OUString &s_Type ,
283 const OUString &s_Value )
285 sName = s_Name;
286 sType = s_Type;
287 sValue = s_Value;
290 OUString sName;
291 OUString sType;
292 OUString sValue;
295 struct AttributeListImpl_impl
297 AttributeListImpl_impl()
299 // performance improvement during adding
300 vecAttribute.reserve(20);
302 vector<struct TagAttribute> vecAttribute;
306 sal_Int16 AttributeListImpl::getLength() throw (RuntimeException)
308 return (sal_Int16) m_pImpl->vecAttribute.size();
312 AttributeListImpl::AttributeListImpl( const AttributeListImpl &r )
314 m_pImpl = new AttributeListImpl_impl;
315 *m_pImpl = *(r.m_pImpl);
318 OUString AttributeListImpl::getNameByIndex(sal_Int16 i) throw (RuntimeException)
320 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
321 return m_pImpl->vecAttribute[i].sName;
323 return OUString();
327 OUString AttributeListImpl::getTypeByIndex(sal_Int16 i) throw (RuntimeException)
329 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
330 return m_pImpl->vecAttribute[i].sType;
332 return OUString();
335 OUString AttributeListImpl::getValueByIndex(sal_Int16 i) throw (RuntimeException)
337 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
338 return m_pImpl->vecAttribute[i].sValue;
340 return OUString();
344 OUString AttributeListImpl::getTypeByName( const OUString& sName ) throw (RuntimeException)
346 vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
348 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
349 if( (*ii).sName == sName ) {
350 return (*ii).sType;
353 return OUString();
356 OUString AttributeListImpl::getValueByName(const OUString& sName) throw (RuntimeException)
358 vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
360 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
361 if( (*ii).sName == sName ) {
362 return (*ii).sValue;
365 return OUString();
369 AttributeListImpl::AttributeListImpl()
371 m_pImpl = new AttributeListImpl_impl;
375 AttributeListImpl::~AttributeListImpl()
377 delete m_pImpl;
381 void AttributeListImpl::addAttribute( const OUString &sName ,
382 const OUString &sType ,
383 const OUString &sValue )
385 m_pImpl->vecAttribute.push_back( TagAttribute( sName , sType , sValue ) );
388 void AttributeListImpl::clear()
390 m_pImpl->vecAttribute.clear();
394 // helper function for writing
395 // ensures that linebreaks are inserted
396 // when writing a long text.
397 // Note: this implementation may be a bit slow,
398 // but it shows, how the SAX-Writer handles the allowLineBreak calls.
400 void writeParagraphHelper(
401 const Reference< XExtendedDocumentHandler > &r ,
402 const OUString & s)
404 int nMax = s.getLength();
405 int nStart = 0;
406 int n = 1;
408 Sequence<sal_uInt16> seq( s.getLength() );
409 memcpy( seq.getArray() , s.getStr() , s.getLength() * sizeof( sal_uInt16 ) );
411 for( n = 1 ; n < nMax ; n++ ){
412 if( 32 == seq.getArray()[n] ) {
413 r->allowLineBreak();
414 r->characters( s.copy( nStart , n - nStart ) );
415 nStart = n;
418 r->allowLineBreak();
419 r->characters( s.copy( nStart , n - nStart ) );
423 // helper implementation for SAX-Writer
424 // writes data to a file
426 class OFileWriter :
427 public WeakImplHelper< XOutputStream >
429 public:
430 explicit OFileWriter( char *pcFile ) { strncpy( m_pcFile , pcFile, 256 - 1 ); m_f = 0; }
433 public:
434 virtual void SAL_CALL writeBytes(const Sequence< sal_Int8 >& aData)
435 throw (NotConnectedException, BufferSizeExceededException, RuntimeException);
436 virtual void SAL_CALL flush()
437 throw (NotConnectedException, BufferSizeExceededException, RuntimeException);
438 virtual void SAL_CALL closeOutput()
439 throw (NotConnectedException, BufferSizeExceededException, RuntimeException);
440 private:
441 char m_pcFile[256];
442 FILE *m_f;
446 void OFileWriter::writeBytes(const Sequence< sal_Int8 >& aData)
447 throw (NotConnectedException, BufferSizeExceededException, RuntimeException)
449 if( ! m_f ) {
450 m_f = fopen( m_pcFile , "w" );
453 fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f );
457 void OFileWriter::flush()
458 throw (NotConnectedException, BufferSizeExceededException, RuntimeException)
460 fflush( m_f );
463 void OFileWriter::closeOutput()
464 throw (NotConnectedException, BufferSizeExceededException, RuntimeException)
466 fclose( m_f );
467 m_f = 0;
471 // Needed to switch on solaris threads
472 #ifdef __sun
473 extern "C" void ChangeGlobalInit();
474 #endif
475 int main (int argc, char **argv)
478 if( argc < 3) {
479 printf( "usage : saxdemo inputfile outputfile\n" );
480 exit( 0 );
482 #ifdef __sun
483 // switch on threads in solaris
484 ChangeGlobalInit();
485 #endif
487 // create service manager
488 Reference< XMultiServiceFactory > xSMgr = createRegistryServiceFactory(
489 OUString( "applicat.rdb" ) );
491 Reference < XImplementationRegistration > xReg;
494 // Create registration service
495 Reference < XInterface > x = xSMgr->createInstance( "com.sun.star.registry.ImplementationRegistration" );
496 xReg.set( x , UNO_QUERY );
498 catch( Exception & ) {
499 printf( "Couldn't create ImplementationRegistration service\n" );
500 exit(1);
503 OString sTestName;
506 // Load dll for the tested component
507 OUString aDllName( "sax.uno" SAL_DLLEXTENSION );
508 xReg->registerImplementation(
509 OUString("com.sun.star.loader.SharedLibrary"),
510 aDllName,
511 Reference< XSimpleRegistry > () );
513 catch( Exception &e ) {
514 printf( "Couldn't reach sax dll\n" );
515 printf( "%s\n" , OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() );
517 exit(1);
521 // parser demo
522 // read xml from a file and count elements
524 Reference< XInterface > x = xSMgr->createInstance( "com.sun.star.xml.sax.Parser" );
525 if( x.is() )
527 Reference< XParser > rParser( x , UNO_QUERY );
529 // create and connect the document handler to the parser
530 TestDocumentHandler *pDocHandler = new TestDocumentHandler( );
532 Reference < XDocumentHandler > rDocHandler( (XDocumentHandler *) pDocHandler );
533 Reference< XEntityResolver > rEntityResolver( (XEntityResolver *) pDocHandler );
535 rParser->setDocumentHandler( rDocHandler );
536 rParser->setEntityResolver( rEntityResolver );
538 // create the input stream
539 InputSource source;
540 source.aInputStream = createStreamFromFile( argv[1] );
541 source.sSystemId = OUString::createFromAscii( argv[1] );
545 // start parsing
546 rParser->parseStream( source );
549 catch( Exception & e )
551 OString o1 = OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8 );
552 printf( "Exception during parsing : %s\n" , o1.getStr() );
555 else
557 printf( "couldn't create sax-parser component\n" );
561 // The SAX-Writer demo
563 x= xSMgr->createInstance("com.sun.star.xml.sax.Writer");
564 if( x.is() )
566 printf( "start writing to %s\n" , argv[2] );
568 OFileWriter *pw = new OFileWriter( argv[2] );
569 Reference< XActiveDataSource > source( x , UNO_QUERY );
570 source->setOutputStream( Reference< XOutputStream> ( (XOutputStream*) pw ) );
572 AttributeListImpl *pList = new AttributeListImpl;
573 Reference< XAttributeList > rList( (XAttributeList *) pList );
575 Reference< XExtendedDocumentHandler > r( x , UNO_QUERY );
576 r->startDocument();
578 pList->addAttribute( OUString( "Arg1" ),
579 OUString( "CDATA") ,
580 OUString( "foo\n u") );
581 pList->addAttribute( OUString( "Arg2") ,
582 OUString( "CDATA") ,
583 OUString( "foo2") );
585 r->startElement( OUString( "tag1") , rList );
586 // tells the writer to insert a linefeed
587 r->ignorableWhitespace( OUString() );
589 r->characters( OUString( "huhu") );
590 r->ignorableWhitespace( OUString() );
592 r->startElement( OUString( "hi") , rList );
593 r->ignorableWhitespace( OUString() );
595 // the enpassant must be converted & -> &amp;
596 r->characters( OUString( "&#252;") );
597 r->ignorableWhitespace( OUString() );
599 // '>' must not be converted
600 r->startCDATA();
601 r->characters( OUString( " > foo < ") );
602 r->endCDATA();
603 r->ignorableWhitespace( OUString() );
605 OUString testParagraph = OUString(
606 "This is only a test to check, if the writer inserts line feeds "
607 "if needed or if the writer puts the whole text into one line." );
608 writeParagraphHelper( r , testParagraph );
610 r->ignorableWhitespace( OUString() );
611 r->comment( OUString( "This is a comment !") );
612 r->ignorableWhitespace( OUString() );
614 r->startElement( OUString( "emptytagtest") , rList );
615 r->endElement( OUString( "emptytagtest") );
616 r->ignorableWhitespace( OUString() );
618 r->endElement( OUString( "hi") );
619 r->ignorableWhitespace( OUString() );
621 r->endElement( OUString( "tag1") );
622 r->endDocument();
624 printf( "finished writing\n" );
626 else
628 printf( "couldn't create sax-writer component\n" );
632 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */