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 auto ii
= std::find_if(m_pImpl
->vecAttribute
.begin(), m_pImpl
->vecAttribute
.end(),
347 [&sName
](const struct TagAttribute
& rAttr
) { return rAttr
.sName
== sName
; });
348 if (ii
!= m_pImpl
->vecAttribute
.end())
353 OUString
AttributeListImpl::getValueByName(const OUString
& sName
) throw (RuntimeException
)
355 auto ii
= std::find_if(m_pImpl
->vecAttribute
.begin(), m_pImpl
->vecAttribute
.end(),
356 [&sName
](const struct TagAttribute
& rAttr
) { return rAttr
.sName
== sName
; });
357 if (ii
!= m_pImpl
->vecAttribute
.end())
363 AttributeListImpl::AttributeListImpl()
365 m_pImpl
= new AttributeListImpl_impl
;
369 AttributeListImpl::~AttributeListImpl()
375 void AttributeListImpl::addAttribute( const OUString
&sName
,
376 const OUString
&sType
,
377 const OUString
&sValue
)
379 m_pImpl
->vecAttribute
.push_back( TagAttribute( sName
, sType
, sValue
) );
382 void AttributeListImpl::clear()
384 m_pImpl
->vecAttribute
.clear();
388 // helper function for writing
389 // ensures that linebreaks are inserted
390 // when writing a long text.
391 // Note: this implementation may be a bit slow,
392 // but it shows, how the SAX-Writer handles the allowLineBreak calls.
394 void writeParagraphHelper(
395 const Reference
< XExtendedDocumentHandler
> &r
,
398 int nMax
= s
.getLength();
402 Sequence
<sal_uInt16
> seq( s
.getLength() );
403 memcpy( seq
.getArray() , s
.getStr() , s
.getLength() * sizeof( sal_uInt16
) );
405 for( n
= 1 ; n
< nMax
; n
++ ){
406 if( 32 == seq
.getArray()[n
] ) {
408 r
->characters( s
.copy( nStart
, n
- nStart
) );
413 r
->characters( s
.copy( nStart
, n
- nStart
) );
417 // helper implementation for SAX-Writer
418 // writes data to a file
421 public WeakImplHelper
< XOutputStream
>
424 explicit OFileWriter( char *pcFile
) { strncpy( m_pcFile
, pcFile
, 256 - 1 ); m_f
= 0; }
428 virtual void SAL_CALL
writeBytes(const Sequence
< sal_Int8
>& aData
)
429 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
);
430 virtual void SAL_CALL
flush()
431 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
);
432 virtual void SAL_CALL
closeOutput()
433 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
);
440 void OFileWriter::writeBytes(const Sequence
< sal_Int8
>& aData
)
441 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
)
444 m_f
= fopen( m_pcFile
, "w" );
447 fwrite( aData
.getConstArray() , 1 , aData
.getLength() , m_f
);
451 void OFileWriter::flush()
452 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
)
457 void OFileWriter::closeOutput()
458 throw (NotConnectedException
, BufferSizeExceededException
, RuntimeException
)
465 // Needed to switch on solaris threads
467 extern "C" void ChangeGlobalInit();
469 int main (int argc
, char **argv
)
473 printf( "usage : saxdemo inputfile outputfile\n" );
477 // switch on threads in solaris
481 // create service manager
482 Reference
< XMultiServiceFactory
> xSMgr
= createRegistryServiceFactory(
483 OUString( "applicat.rdb" ) );
485 Reference
< XImplementationRegistration
> xReg
;
488 // Create registration service
489 Reference
< XInterface
> x
= xSMgr
->createInstance( "com.sun.star.registry.ImplementationRegistration" );
490 xReg
.set( x
, UNO_QUERY
);
492 catch( Exception
& ) {
493 printf( "Couldn't create ImplementationRegistration service\n" );
500 // Load dll for the tested component
501 OUString
aDllName( "sax.uno" SAL_DLLEXTENSION
);
502 xReg
->registerImplementation(
503 OUString("com.sun.star.loader.SharedLibrary"),
505 Reference
< XSimpleRegistry
> () );
507 catch( Exception
&e
) {
508 printf( "Couldn't reach sax dll\n" );
509 printf( "%s\n" , OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).getStr() );
516 // read xml from a file and count elements
518 Reference
< XInterface
> x
= xSMgr
->createInstance( "com.sun.star.xml.sax.Parser" );
521 Reference
< XParser
> rParser( x
, UNO_QUERY
);
523 // create and connect the document handler to the parser
524 TestDocumentHandler
*pDocHandler
= new TestDocumentHandler( );
526 Reference
< XDocumentHandler
> rDocHandler( (XDocumentHandler
*) pDocHandler
);
527 Reference
< XEntityResolver
> rEntityResolver( (XEntityResolver
*) pDocHandler
);
529 rParser
->setDocumentHandler( rDocHandler
);
530 rParser
->setEntityResolver( rEntityResolver
);
532 // create the input stream
534 source
.aInputStream
= createStreamFromFile( argv
[1] );
535 source
.sSystemId
= OUString::createFromAscii( argv
[1] );
540 rParser
->parseStream( source
);
543 catch( Exception
& e
)
545 OString o1
= OUStringToOString(e
.Message
, RTL_TEXTENCODING_UTF8
);
546 printf( "Exception during parsing : %s\n" , o1
.getStr() );
551 printf( "couldn't create sax-parser component\n" );
555 // The SAX-Writer demo
557 x
= xSMgr
->createInstance("com.sun.star.xml.sax.Writer");
560 printf( "start writing to %s\n" , argv
[2] );
562 OFileWriter
*pw
= new OFileWriter( argv
[2] );
563 Reference
< XActiveDataSource
> source( x
, UNO_QUERY
);
564 source
->setOutputStream( Reference
< XOutputStream
> ( (XOutputStream
*) pw
) );
566 AttributeListImpl
*pList
= new AttributeListImpl
;
567 Reference
< XAttributeList
> rList( (XAttributeList
*) pList
);
569 Reference
< XExtendedDocumentHandler
> r( x
, UNO_QUERY
);
572 pList
->addAttribute( OUString( "Arg1" ),
574 OUString( "foo\n u") );
575 pList
->addAttribute( OUString( "Arg2") ,
579 r
->startElement( OUString( "tag1") , rList
);
580 // tells the writer to insert a linefeed
581 r
->ignorableWhitespace( OUString() );
583 r
->characters( OUString( "huhu") );
584 r
->ignorableWhitespace( OUString() );
586 r
->startElement( OUString( "hi") , rList
);
587 r
->ignorableWhitespace( OUString() );
589 // the enpassant must be converted & -> &
590 r
->characters( OUString( "ü") );
591 r
->ignorableWhitespace( OUString() );
593 // '>' must not be converted
595 r
->characters( OUString( " > foo < ") );
597 r
->ignorableWhitespace( OUString() );
599 OUString testParagraph
= OUString(
600 "This is only a test to check, if the writer inserts line feeds "
601 "if needed or if the writer puts the whole text into one line." );
602 writeParagraphHelper( r
, testParagraph
);
604 r
->ignorableWhitespace( OUString() );
605 r
->comment( OUString( "This is a comment !") );
606 r
->ignorableWhitespace( OUString() );
608 r
->startElement( OUString( "emptytagtest") , rList
);
609 r
->endElement( OUString( "emptytagtest") );
610 r
->ignorableWhitespace( OUString() );
612 r
->endElement( OUString( "hi") );
613 r
->ignorableWhitespace( OUString() );
615 r
->endElement( OUString( "tag1") );
618 printf( "finished writing\n" );
622 printf( "couldn't create sax-writer component\n" );
626 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */