update dev300-m58
[ooovba.git] / configmgr / workben / local_io / simpletest.cxx
blob8a69ed9e3b44339ca2a83e3cab32f1a74f6a861e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: simpletest.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include <memory>
35 #include <vector>
36 #include <stack>
37 #include<osl/file.hxx>
39 #include <rtl/ustring.hxx>
40 #include <rtl/string.hxx>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include <com/sun/star/uno/Any.hxx>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <cppuhelper/implbase1.hxx>
45 #include <cppuhelper/implbase2.hxx>
46 #include <cppuhelper/servicefactory.hxx>
48 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
49 #include <com/sun/star/lang/XComponent.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HDL_
52 #include <com/sun/star/io/XActiveDataSource.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASINK_HDL_
55 #include <com/sun/star/io/XActiveDataSink.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HDL_
58 #include <com/sun/star/io/XActiveDataControl.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_IO_XDATATRANSFEREVENTLISTENER_HDL_
61 #include <com/sun/star/io/XDataTransferEventListener.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_IO_XDATAIMPORTER_HDL_
64 #include <com/sun/star/io/XDataImporter.hpp>
65 #endif
67 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HDL_
68 #include <com/sun/star/io/XInputStream.hpp>
69 #endif
70 #include <com/sun/star/xml/sax/XParser.hpp>
71 #include <com/sun/star/xml/sax/SAXParseException.hpp>
72 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
74 #include <com/sun/star/xml/sax/InputSource.hpp>
75 #include <vos/thread.hxx>
77 #include <vos/pipe.hxx>
78 #include <osl/diagnose.h>
79 #include "oslstream.hxx"
80 #include <com/sun/star/xml/sax/XAttributeList.hpp>
82 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
83 #include <com/sun/star/container/XNameAccess.hpp>
84 #include <com/sun/star/container/XHierarchicalName.hpp>
86 #include <com/sun/star/container/XNameReplace.hpp>
87 #include <com/sun/star/util/XChangesBatch.hpp>
88 #include <com/sun/star/script/XTypeConverter.hpp>
89 #include <com/sun/star/container/XNameContainer.hpp>
90 #include <osl/conditn.hxx>
92 #include "xmltreebuilder.hxx"
94 #include "dataimport.hxx"
96 #include "createpropertyvalue.hxx"
97 #include "strdecl.hxx"
99 #include "confname.hxx"
101 #include "FileHelper.hxx"
103 // -----------------------------------------------------------------------------
104 // --------------------------------- namespaces ---------------------------------
105 // -----------------------------------------------------------------------------
106 namespace uno = com::sun::star::uno;
107 namespace lang = com::sun::star::lang;
108 namespace io = com::sun::star::io;
109 namespace sax = com::sun::star::xml::sax;
110 namespace script = com::sun::star::script;
112 using namespace ::com::sun::star::uno;
113 using namespace ::com::sun::star::lang;
114 using namespace ::com::sun::star::container;
115 using namespace ::com::sun::star::util;
116 using namespace ::com::sun::star::xml::sax;
117 using namespace ::com::sun::star::io;
118 using namespace rtl;
119 using namespace osl;
121 // -----------------------------------------------------------------------------
122 // ---------------------------------- defines ----------------------------------
123 // -----------------------------------------------------------------------------
124 #ifndef ASCII
125 #define ASCII(x) OUString::createFromAscii(x)
126 #endif
128 ostream& operator << (ostream& out, rtl::OUString const& aStr)
130 sal_Unicode const* const pStr = aStr.getStr();
131 sal_Unicode const* const pEnd = pStr + aStr.getLength();
132 for (sal_Unicode const* p = pStr; p < pEnd; ++p)
133 if (0 < *p && *p < 127) // ASCII
134 out << char(*p);
135 else
136 out << "[\\u" << hex << *p << "]";
137 return out;
141 // -----------------------------------------------------------------------------
142 // ---------------------------------- a Class ----------------------------------
143 // -----------------------------------------------------------------------------
144 // <Name a="xyz" b="bar">
146 //==========================================================================
147 //= Visitors
148 //==========================================================================
151 namespace configmgr
154 class XMLSimpleDocHandler : public ::cppu::WeakImplHelper1<sax::XDocumentHandler>
156 // uno::Reference< sax::XDocumentHandler > m_xWriter; // the service object for writing XML code
158 sal_Int32 m_nElementDepth;
159 sal_Int32 m_nIgnoreLevel;
160 public:
161 XMLSimpleDocHandler::XMLSimpleDocHandler()
165 // DECLARE_UNO3_DEFAULTS(XMLReadFilter, CmDocumentHandler_BASE);
167 // XDocumentHandler
168 virtual void SAL_CALL startDocument(void)
169 throw (sax::SAXException, uno::RuntimeException)
173 virtual void SAL_CALL endDocument(void)
174 throw(sax::SAXException, uno::RuntimeException)
178 virtual void SAL_CALL startElement(const rtl::OUString& aName,
179 const uno::Reference< sax::XAttributeList > &_xAttrList)
180 throw(sax::SAXException, uno::RuntimeException)
182 sal_Int16 nAttrCount = _xAttrList.is() ? _xAttrList->getLength() : 0;
183 for( sal_Int16 i=0; i < nAttrCount; i++ )
185 OUString aParamName( _xAttrList->getNameByIndex( i ) );
186 OUString aParamValue( _xAttrList->getValueByIndex( i ) );
187 volatile int dummy = 0;
191 virtual void SAL_CALL endElement(const rtl::OUString& aName)
192 throw(sax::SAXException, uno::RuntimeException)
196 virtual void SAL_CALL characters(const rtl::OUString& aChars)
197 throw(sax::SAXException, uno::RuntimeException)
201 virtual void SAL_CALL ignorableWhitespace(const rtl::OUString& aWhitespaces)
202 throw(sax::SAXException, uno::RuntimeException)
206 virtual void SAL_CALL processingInstruction(const rtl::OUString& aTarget,
207 const rtl::OUString& aData)
208 throw(sax::SAXException, uno::RuntimeException)
211 virtual void SAL_CALL setDocumentLocator(const uno::Reference< sax::XLocator > &xLocator)
212 throw(sax::SAXException, uno::RuntimeException)
216 // -----------------------------------------------------------------------------
217 // ------------------------------------ Test ------------------------------------
218 // -----------------------------------------------------------------------------
219 #define ASCII_STRING(rtlOUString) rtl::OUStringToOString(rtlOUString, RTL_TEXTENCODING_ASCII_US).getStr()
220 static ::rtl::OUString sRegistry = ::rtl::OUString::createFromAscii("applicat.rdb");
222 void simpleTest()
224 uno::Reference< lang::XMultiServiceFactory > xMSF;
227 xMSF = cppu::createRegistryServiceFactory(sRegistry, ::rtl::OUString());
229 catch (uno::Exception& e)
231 cout << "could not bootstrap the services from " << ASCII_STRING(sRegistry) << endl ;
232 cout << " (error message : " << ASCII_STRING(e.Message) << ")" << endl;
235 if (!xMSF.is())
237 cerr << "could not create the service factory !" << endl;
238 return;
241 OUString aPath = ASCII("e:/temp/Test");
242 OUString aFilename = ASCII("calc");
243 OUString aExtension = ASCII("xml");
245 OUString aFullname = aPath + ASCII("/") + aFilename + ASCII(".") + aExtension;
247 // Filename convertieren
248 OUString aURL;
249 File aConvert(ASCII(""));
250 aConvert.normalizePath(aFullname, aURL);
252 // File oeffnen
253 File aFile(aURL);
254 aFile.open(osl_File_OpenFlag_Read);
256 uno::Reference<io::XInputStream> xInputStream =
257 new configmgr::OSLInputStreamWrapper(aFile);
259 // connect stream to input stream to the parser
260 InputSource aInputSource;
261 Reference<XInputStream> xPipeInput( xInputStream, UNO_QUERY );
262 aInputSource.aInputStream = xPipeInput;
264 Reference< sax::XParser > xParser;
265 xParser = Reference< sax::XParser > (
266 xMSF->createInstance(
267 ::rtl::OUString::createFromAscii("com.sun.star.xml.sax.Parser")), UNO_QUERY);
269 XMLSimpleDocHandler *pTest = new XMLSimpleDocHandler();
271 // get filter
272 Reference<XDocumentHandler> xFilter = pTest;
274 // connect parser and filter
275 xParser->setDocumentHandler( xFilter );
277 // parse
278 sal_Int16 nRet = 0;
279 OUString sError;
282 xParser->parseStream( aInputSource );
284 catch( SAXParseException &e )
286 OUString sLine = OUString::valueOf(e.LineNumber);
287 OUString aStr = ASCII("SAXParseException occured in ");
288 sError = aStr + ASCII(" Line: (") + sLine + ASCII(")");
290 OSL_ENSURE(0, rtl::OUStringToOString(sError,RTL_TEXTENCODING_ASCII_US).getStr());
291 nRet = 3;
293 catch( SAXException &e )
295 sError = e.Message;
296 OSL_ENSURE(0, rtl::OUStringToOString(sError,RTL_TEXTENCODING_ASCII_US).getStr());
297 nRet = 4;
299 catch( IOException &e )
301 sError = e.Message;
302 OSL_ENSURE(0, rtl::OUStringToOString(sError,RTL_TEXTENCODING_ASCII_US).getStr());
303 nRet = 5;
308 #include <osl/time.h>
309 #include <rtl/string.hxx>
311 class TimeTest
313 TimeValue m_aStartTime, m_aEndTime;
314 bool m_bStarted;
315 public:
316 TimeTest()
317 :m_bStarted(false)
321 void start()
323 m_bStarted = true;
324 osl_getSystemTime(&m_aStartTime);
326 void stop()
328 osl_getSystemTime(&m_aEndTime);
329 OSL_ENSURE(m_bStarted, "Not Started.");
330 m_bStarted = false;
332 void showTime(const rtl::OString & aWhatStr)
334 OSL_ENSURE(!m_bStarted, "Not Stopped.");
336 sal_Int32 nSeconds = m_aEndTime.Seconds - m_aStartTime.Seconds;
337 sal_Int32 nNanoSec = m_aEndTime.Nanosec - m_aStartTime.Nanosec;
338 if (nNanoSec < 0)
340 nNanoSec = 1000000000 - nNanoSec;
341 nSeconds++;
343 rtl::OString aStr = "Time for ";
344 aStr += aWhatStr;
345 aStr += " : ";
346 aStr += rtl::OString::valueOf(nSeconds);
347 aStr += ".";
348 aStr += rtl::OString::valueOf(nNanoSec);
350 cout << aStr.getStr() << endl;
355 // -----------------------------------------------------------------------------
356 // -------------------------------- Mapping Test --------------------------------
357 // -----------------------------------------------------------------------------
358 // Simple Map created with a stl::vector
360 typedef ::std::pair< rtl::OUString, rtl::OUString > Assoc;
361 // typedef ::std::set<Assoc, ltNode> MappingTable;
362 typedef std::vector<Assoc> MappingTable;
364 rtl::OUString mapTo(const rtl::OUString& aFrom, bool bToNew)
366 static MappingTable aMap;
367 if (aMap.empty())
369 // Fill Map old, new
371 aMap.push_back(Assoc(ASCII("value"), TAG_VALUE));
372 aMap.push_back(Assoc(ASCII("type"), ATTR_TYPE));
373 aMap.push_back(Assoc(ASCII("instance"), ATTR_INSTANCE));
375 if (bToNew)
377 // check, if we should convert first to second
378 for (std::vector<Assoc>::const_iterator it = aMap.begin();it != aMap.end();++it)
380 if ((*it).first.equals(aFrom))
381 return (*it).second;
384 else
386 // check if we should convert second to first
387 for (std::vector<Assoc>::const_iterator it = aMap.begin();it != aMap.end();++it)
389 if ((*it).second.equals(aFrom))
390 return (*it).first;
394 // do nothing!
395 return aFrom;
397 // -----------------------------------------------------------------------------
398 void simpleMappingTest()
400 OUString aValue = ASCII("value");
401 OUString aNew;
403 aNew = mapTo(aValue, true); // true for ToNew
404 aNew = mapTo(aNew, false);
405 aNew = mapTo(aNew, false);
407 volatile int dummy = 0;
410 // -----------------------------------------------------------------------------
411 void speedTest()
413 // check speed of:
414 // ASCII("value")
415 // TAG_VALUE
416 // ...
418 sal_Int32 nCount = 1000 * 1000 * 100;
419 sal_Int32 n;
421 cout << "Starting Timetest" << endl;
422 TimeTest tt;
423 tt.start();
424 for(n=0;n<nCount;n++)
427 tt.stop();
428 tt.showTime("Leere Schleife: ");
431 nCount = 1000 * 1000 * 5;
432 rtl::OUString aStr;
434 tt.start();
435 for(n=0;n<nCount;n++)
437 aStr = ASCII("value");
439 tt.stop();
440 tt.showTime("ASCII() ");
443 OUString aValue = ASCII("value");
444 tt.start();
445 for(n=0;n<nCount;n++)
447 aStr = TAG_VALUE;
449 tt.stop();
450 tt.showTime("TAG_VALUE: ");
454 OUString changeToComSunStarPath(const OUString &aPath)
456 static OUString aOO = ASCII("org.OpenOffice");
457 static OUString aCSS = ASCII("com.sun.star");
458 OUString aNewPath;
460 // compare
461 if (aPath.compareTo( aOO, aOO.getLength() ) == 0)
463 aNewPath = aCSS;
464 aNewPath += aPath.copy(aOO.getLength());
465 return aNewPath;
467 return aPath;
470 void stringTest2()
472 OUString aPath = ASCII("org.OpenOffice.Setup/blah/blub");
474 OUString aNewPath = changeToComSunStarPath(aPath);
475 volatile int dummy = 0;
480 class A
482 public:
483 static void run() {
484 cout << "This is A::run();" << endl;
489 class B : public A
491 public:
493 static void run() {
494 cout << "This is B::run();" << endl;
498 void classTest()
500 A a;
501 B b;
502 B::run();
506 // -----------------------------------------------------------------------------
507 // ------------------------------------ Map ------------------------------------
508 // -----------------------------------------------------------------------------
509 struct ltstr
511 bool operator()(const rtl::OUString &s1, const rtl::OUString &s2) const
513 return s1.compareTo(s2) < 0 ? true : false;
517 void stringTest()
519 map<const OUString, int, ltstr> months;
521 months[ASCII("january")] = 31;
522 months[ASCII("february")] = 28;
523 months[ASCII("march")] = 31;
524 months[ASCII("april")] = 30;
525 months[ASCII("may")] = 31;
526 months[ASCII("june")] = 30;
527 months[ASCII("july")] = 31;
528 months[ASCII("august")] = 31;
529 months[ASCII("september")] = 30;
530 months[ASCII("october")] = 31;
531 months[ASCII("november")] = 30;
532 months[ASCII("december")] = 31;
534 cout << "june -> " << months[ASCII("june")] << endl;
535 map<const OUString, int, ltstr>::iterator cur = months.find(ASCII("april"));
536 // map<const OUString, int, ltstr>::iterator prev = cur;
537 map<const OUString, int, ltstr>::iterator next = cur;
538 // ++next;
539 // --prev;
540 // cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
541 for(int i=0;i<12;i++)
543 cout << "Next (in alphabetical order) is " << (*next).first << " days " << (*next).second << endl;
544 ++next;
548 // -----------------------------------------------------------------------------
549 // ---------------------------------- HashMap ----------------------------------
550 // -----------------------------------------------------------------------------
551 #include <hash_map>
554 namespace test {
556 struct eqstr
558 bool operator()(const rtl::OUString &s1, const rtl::OUString &s2) const
560 return s1.equals(s2) == sal_True ? true : false;
564 struct hash_oustring
566 // This hash funktion is a copy of hash<char*> from SGI-STL
567 size_t operator()(const rtl::OUString &_s) const
569 sal_Int64 nStrLen = _s.getLength();
570 const sal_Unicode *pStr = _s.getStr();
571 unsigned long h = 0;
572 for (sal_Int64 i=0;i<nStrLen; ++i)
573 h = 5*h + *pStr++;
575 return size_t(h);
579 void hash_test()
581 hash_map<const rtl::OUString, rtl::OUString, hash_oustring, eqstr> months;
583 rtl::OUString sJanuary = ASCII("january");
584 months[sJanuary] = ASCII("31");
585 months[ASCII("february")] = ASCII("28");
586 months[ASCII("march")] = ASCII("31");
587 months[ASCII("april")] = ASCII("30");
588 months[ASCII("may")] = ASCII("31");
589 months[ASCII("june")] = ASCII("30");
590 months[ASCII("july")] = ASCII("31");
591 months[ASCII("august")] = ASCII("31");
592 months[ASCII("september")] = ASCII("30");
593 months[ASCII("october")] = ASCII("31");
594 months[ASCII("november")] = ASCII("30");
595 months[ASCII("december")] = ASCII("31");
597 cout << "september -> " << months[ASCII("september")] << endl;
598 cout << "april -> " << months[ASCII("april")] << endl;
599 cout << "june -> " << months[ASCII("june")] << endl;
600 cout << "november -> " << months[ASCII("november")] << endl;
605 // -----------------------------------------------------------------------------
606 // Fri Nov 10 15:10:45 2000
607 // -----------------------------------------------------------------------------
609 #include <vos/ref.hxx>
611 class Options : public vos::OReference
613 int m_aValue;
614 public:
615 int getValue() {return m_aValue;}
616 void setValue(int _aValue) {m_aValue = _aValue;}
621 class A
623 vos::ORef<Options> m_aOptions;
624 public:
626 vos::ORef<Options> getOptions() {return m_aOptions;}
627 void setOptions(vos::ORef<Options>& _aOptions) {
628 m_aOptions = _aOptions;
632 void testRefs()
634 vos::ORef<Options> aO = new Options;
635 aO->setValue(10);
637 A a,b;
638 a.setOptions(aO);
639 b.setOptions(aO);
640 cout << "Options from a : " << a.getOptions()->getValue() << endl;
641 cout << "Options from b : " << b.getOptions()->getValue() << endl;
643 aO->setValue(20);
644 cout << "Options from a : " << a.getOptions()->getValue() << endl;
645 cout << "Options from b : " << b.getOptions()->getValue() << endl;
650 void ConfigName()
652 // OUString aSubtreePath = ASCII("/org.openoffice.office.common/path/blah/blub");
653 OUString aSubtreePath = ASCII("/org.openoffice.office.common");
654 ConfigurationName aName(aSubtreePath);
655 OUString a = aName.localName();
656 OUString b = aName.fullName();
657 OUString c = aName.moduleName();
658 ConfigurationName aParentName(aName.getParentName());
659 OUString d = aParentName.fullName();
663 ConfigurationName aName( ASCII("/" ));
665 std::stack< rtl::OUString, std::vector<rtl::OUString> > m_aStringStack;
667 if (aName.localName().getLength() != 0)
669 for (ConfigurationName::Iterator it = aName.begin();
670 it != aName.end();
671 ++it)
673 rtl::OUString aName = *it;
674 m_aStringStack.push(aName);
675 // m_xHandler->startElement(*it, rList);
676 volatile int dummy = 0;
680 // bBack = writeChanges();
682 while(!m_aStringStack.empty())
684 OUString aName = m_aStringStack.top();
685 m_aStringStack.pop();
690 void ConfigName2()
692 OUString aSubtreePath = ASCII("/org.openoffice.office.common/path/blah/blub");
693 ConfigurationName aName(aSubtreePath);
694 ConfigurationName aParent = aName.getParentName();
696 for (ConfigurationName::Iterator it = aName.begin();
697 it != aName.end();
698 ++it)
700 rtl::OUString aName = *it;
701 volatile int dummy = 0;
705 // -----------------------------------------------------------------------------
706 inline void operator <<= (rtl::OUString& _rUnicodeString, const sal_Char* _pAsciiString)
708 _rUnicodeString = ::rtl::OUString::createFromAscii(_pAsciiString);
711 inline void operator <<= (rtl::OUString& _rUnicodeString, const rtl::OString& _rAsciiString)
713 _rUnicodeString <<= _rAsciiString.getStr();
716 inline void operator <<= (rtl::OString& _rAsciiString, const rtl::OUString& _rUnicodeString)
718 _rAsciiString = rtl::OUStringToOString(_rUnicodeString,RTL_TEXTENCODING_ASCII_US);
721 // -----------------------------------------------------------------------------
722 bool isBTimeGreaterATime(TimeValue const& A, TimeValue const& B)
724 if (B.Seconds > A.Seconds) return true;
725 if (B.Nanosec > A.Nanosec) return true;
727 // lower or equal
728 return false;
731 // -----------------------------------------------------------------------------
733 void oslTest()
736 OUString aDirectory(FileHelper::convertFilenameToFileURL(ASCII("c:/temp/file.out.1")));
737 TimeValue a = FileHelper::getFileModificationStamp(aDirectory);
739 OUString aDirectory2(FileHelper::convertFilenameToFileURL(ASCII("c:/temp/file.out.2")));
740 TimeValue b = FileHelper::getFileModificationStamp(aDirectory2);
742 if (isBTimeGreaterATime(a,b))
744 OSL_ENSURE(false, "FileB ist neuer als FileA");
749 OUString aDirectory(FileHelper::convertFilenameToFileURL(ASCII("c:/temp/dies")));
750 osl::FileBase::RC eError = osl::Directory::create(aDirectory);
751 if (eError != osl::FileBase::E_None)
753 OUString aUStr = FileHelper::createOSLErrorString(eError);
754 OString aStr;
755 aStr <<= aUStr;
756 OSL_ENSURE(false, aStr.getStr());
761 } // namespace configmgr