forcepoint#92 fix crash on layout of specific doc
[LibreOffice.git] / sax / test / saxdemo.cxx
blob7139d60ef608a0566d406abaaa7d4e0f5760e034
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 )
284 : sName(s_Name),
285 sType(s_Type),
286 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 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())
349 return (*ii).sType;
350 return OUString();
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())
358 return (*ii).sValue;
359 return OUString();
363 AttributeListImpl::AttributeListImpl()
365 m_pImpl = new AttributeListImpl_impl;
369 AttributeListImpl::~AttributeListImpl()
371 delete m_pImpl;
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 ,
396 const OUString & s)
398 int nMax = s.getLength();
399 int nStart = 0;
400 int n = 1;
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] ) {
407 r->allowLineBreak();
408 r->characters( s.copy( nStart , n - nStart ) );
409 nStart = n;
412 r->allowLineBreak();
413 r->characters( s.copy( nStart , n - nStart ) );
417 // helper implementation for SAX-Writer
418 // writes data to a file
420 class OFileWriter :
421 public WeakImplHelper< XOutputStream >
423 public:
424 explicit OFileWriter( char *pcFile ) { strncpy( m_pcFile , pcFile, 256 - 1 ); m_f = 0; }
427 public:
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);
434 private:
435 char m_pcFile[256];
436 FILE *m_f;
440 void OFileWriter::writeBytes(const Sequence< sal_Int8 >& aData)
441 throw (NotConnectedException, BufferSizeExceededException, RuntimeException)
443 if( ! m_f ) {
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)
454 fflush( m_f );
457 void OFileWriter::closeOutput()
458 throw (NotConnectedException, BufferSizeExceededException, RuntimeException)
460 fclose( m_f );
461 m_f = 0;
465 // Needed to switch on solaris threads
466 #ifdef __sun
467 extern "C" void ChangeGlobalInit();
468 #endif
469 int main (int argc, char **argv)
472 if( argc < 3) {
473 printf( "usage : saxdemo inputfile outputfile\n" );
474 exit( 0 );
476 #ifdef __sun
477 // switch on threads in solaris
478 ChangeGlobalInit();
479 #endif
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" );
494 exit(1);
497 OString sTestName;
500 // Load dll for the tested component
501 OUString aDllName( "sax.uno" SAL_DLLEXTENSION );
502 xReg->registerImplementation(
503 OUString("com.sun.star.loader.SharedLibrary"),
504 aDllName,
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() );
511 exit(1);
515 // parser demo
516 // read xml from a file and count elements
518 Reference< XInterface > x = xSMgr->createInstance( "com.sun.star.xml.sax.Parser" );
519 if( x.is() )
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
533 InputSource source;
534 source.aInputStream = createStreamFromFile( argv[1] );
535 source.sSystemId = OUString::createFromAscii( argv[1] );
539 // start parsing
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() );
549 else
551 printf( "couldn't create sax-parser component\n" );
555 // The SAX-Writer demo
557 x= xSMgr->createInstance("com.sun.star.xml.sax.Writer");
558 if( x.is() )
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 );
570 r->startDocument();
572 pList->addAttribute( OUString( "Arg1" ),
573 OUString( "CDATA") ,
574 OUString( "foo\n u") );
575 pList->addAttribute( OUString( "Arg2") ,
576 OUString( "CDATA") ,
577 OUString( "foo2") );
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 & -> &amp;
590 r->characters( OUString( "&#252;") );
591 r->ignorableWhitespace( OUString() );
593 // '>' must not be converted
594 r->startCDATA();
595 r->characters( OUString( " > foo < ") );
596 r->endCDATA();
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") );
616 r->endDocument();
618 printf( "finished writing\n" );
620 else
622 printf( "couldn't create sax-writer component\n" );
626 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */