1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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
;
56 * Sequence of bytes -> InputStream
58 class OInputStream
: public WeakImplHelper
< XInputStream
>
61 explicit OInputStream( const Sequence
< sal_Int8
>&seq
) :
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
:
73 aData
= Sequence
< sal_Int8
> ( &(m_seq
.getConstArray()[nPos
]) , 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
)
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
)
99 Sequence
< sal_Int8
> m_seq
;
104 // Helper : create an input stream from a file
106 Reference
< XInputStream
> createStreamFromFile(
109 FILE *f
= fopen( pcFile
, "rb" );
110 Reference
< XInputStream
> r
;
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
) );
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
>
134 TestDocumentHandler( )
138 public: // Error handler
139 virtual void SAL_CALL
error(const Any
& aSAXParseException
) throw (SAXException
, RuntimeException
)
141 printf( "Error !\n" );
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
)
162 m_iAttributeCount
= 0;
163 m_iWhitespaceCount
=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
)
179 m_iAttributeCount
+= xAttribs
->getLength();
182 virtual void SAL_CALL
endElement(const OUString
& /* aName */) throw (SAXException
,RuntimeException
)
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
)
201 virtual void SAL_CALL
setDocumentLocator(const Reference
< XLocator
> & /* xLocator */)
202 throw (SAXException
,RuntimeException
)
207 virtual InputSource SAL_CALL
resolveEntity(
208 const OUString
& sPublicId
,
209 const OUString
& sSystemId
)
210 throw (RuntimeException
)
213 source
.sSystemId
= sSystemId
;
214 source
.sPublicId
= sPublicId
;
216 source
.aInputStream
= createStreamFromFile(
217 OUStringToOString( sSystemId
, RTL_TEXTENCODING_ASCII_US
).getStr() );
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
)
242 int m_iAttributeCount
;
243 int m_iWhitespaceCount
;
248 // helper implementation for writing
249 // implements an XAttributeList
251 struct AttributeListImpl_impl
;
252 class AttributeListImpl
: public WeakImplHelper
< XAttributeList
>
256 AttributeListImpl( const AttributeListImpl
& );
257 ~AttributeListImpl();
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
);
268 void addAttribute( const OUString
&sName
,
269 const OUString
&sType
,
270 const OUString
&sValue
);
274 struct AttributeListImpl_impl
*m_pImpl
;
281 TagAttribute( const OUString
&s_Name
,
282 const OUString
&s_Type
,
283 const OUString
&s_Value
)
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
;
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
;
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
;
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
) {
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
) {
369 AttributeListImpl::AttributeListImpl()
371 m_pImpl
= new AttributeListImpl_impl
;
375 AttributeListImpl::~AttributeListImpl()
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
,
404 int nMax
= s
.getLength();
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
] ) {
414 r
->characters( s
.copy( nStart
, n
- nStart
) );
419 r
->characters( s
.copy( nStart
, n
- nStart
) );
423 // helper implementation for SAX-Writer
424 // writes data to a file
427 public WeakImplHelper
< XOutputStream
>
430 explicit OFileWriter( char *pcFile
) { strncpy( m_pcFile
, pcFile
, 256 - 1 ); m_f
= 0; }
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
);
446 void OFileWriter::writeBytes(const Sequence
< sal_Int8
>& aData
)
447 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
)
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
)
463 void OFileWriter::closeOutput()
464 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
)
471 // Needed to switch on solaris threads
473 extern "C" void ChangeGlobalInit();
475 int main (int argc
, char **argv
)
479 printf( "usage : saxdemo inputfile outputfile\n" );
483 // switch on threads in solaris
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" );
506 // Load dll for the tested component
507 OUString
aDllName( "sax.uno" SAL_DLLEXTENSION
);
508 xReg
->registerImplementation(
509 OUString("com.sun.star.loader.SharedLibrary"),
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() );
522 // read xml from a file and count elements
524 Reference
< XInterface
> x
= xSMgr
->createInstance( "com.sun.star.xml.sax.Parser" );
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
540 source
.aInputStream
= createStreamFromFile( argv
[1] );
541 source
.sSystemId
= OUString::createFromAscii( argv
[1] );
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() );
557 printf( "couldn't create sax-parser component\n" );
561 // The SAX-Writer demo
563 x
= xSMgr
->createInstance("com.sun.star.xml.sax.Writer");
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
);
578 pList
->addAttribute( OUString( "Arg1" ),
580 OUString( "foo\n u") );
581 pList
->addAttribute( OUString( "Arg2") ,
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 & -> &
596 r
->characters( OUString( "ü") );
597 r
->ignorableWhitespace( OUString() );
599 // '>' must not be converted
601 r
->characters( OUString( " > foo < ") );
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") );
624 printf( "finished writing\n" );
628 printf( "couldn't create sax-writer component\n" );
632 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */