1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: simpletest.cxx,v $
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"
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>
51 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HDL_
52 #include <com/sun/star/io/XActiveDataSource.hpp>
54 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASINK_HDL_
55 #include <com/sun/star/io/XActiveDataSink.hpp>
57 #ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HDL_
58 #include <com/sun/star/io/XActiveDataControl.hpp>
60 #ifndef _COM_SUN_STAR_IO_XDATATRANSFEREVENTLISTENER_HDL_
61 #include <com/sun/star/io/XDataTransferEventListener.hpp>
63 #ifndef _COM_SUN_STAR_IO_XDATAIMPORTER_HDL_
64 #include <com/sun/star/io/XDataImporter.hpp>
67 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HDL_
68 #include <com/sun/star/io/XInputStream.hpp>
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
;
121 // -----------------------------------------------------------------------------
122 // ---------------------------------- defines ----------------------------------
123 // -----------------------------------------------------------------------------
125 #define ASCII(x) OUString::createFromAscii(x)
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
136 out
<< "[\\u" << hex
<< *p
<< "]";
141 // -----------------------------------------------------------------------------
142 // ---------------------------------- a Class ----------------------------------
143 // -----------------------------------------------------------------------------
144 // <Name a="xyz" b="bar">
146 //==========================================================================
148 //==========================================================================
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
;
161 XMLSimpleDocHandler::XMLSimpleDocHandler()
165 // DECLARE_UNO3_DEFAULTS(XMLReadFilter, CmDocumentHandler_BASE);
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");
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
;
237 cerr
<< "could not create the service factory !" << endl
;
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
249 File
aConvert(ASCII(""));
250 aConvert
.normalizePath(aFullname
, 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();
272 Reference
<XDocumentHandler
> xFilter
= pTest
;
274 // connect parser and filter
275 xParser
->setDocumentHandler( xFilter
);
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());
293 catch( SAXException
&e
)
296 OSL_ENSURE(0, rtl::OUStringToOString(sError
,RTL_TEXTENCODING_ASCII_US
).getStr());
299 catch( IOException
&e
)
302 OSL_ENSURE(0, rtl::OUStringToOString(sError
,RTL_TEXTENCODING_ASCII_US
).getStr());
308 #include <osl/time.h>
309 #include <rtl/string.hxx>
313 TimeValue m_aStartTime
, m_aEndTime
;
324 osl_getSystemTime(&m_aStartTime
);
328 osl_getSystemTime(&m_aEndTime
);
329 OSL_ENSURE(m_bStarted
, "Not Started.");
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
;
340 nNanoSec
= 1000000000 - nNanoSec
;
343 rtl::OString aStr
= "Time for ";
346 aStr
+= rtl::OString::valueOf(nSeconds
);
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
;
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
));
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
))
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
))
397 // -----------------------------------------------------------------------------
398 void simpleMappingTest()
400 OUString aValue
= ASCII("value");
403 aNew
= mapTo(aValue
, true); // true for ToNew
404 aNew
= mapTo(aNew
, false);
405 aNew
= mapTo(aNew
, false);
407 volatile int dummy
= 0;
410 // -----------------------------------------------------------------------------
418 sal_Int32 nCount
= 1000 * 1000 * 100;
421 cout
<< "Starting Timetest" << endl
;
424 for(n
=0;n
<nCount
;n
++)
428 tt
.showTime("Leere Schleife: ");
431 nCount
= 1000 * 1000 * 5;
435 for(n
=0;n
<nCount
;n
++)
437 aStr
= ASCII("value");
440 tt
.showTime("ASCII() ");
443 OUString aValue
= ASCII("value");
445 for(n
=0;n
<nCount
;n
++)
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");
461 if (aPath
.compareTo( aOO
, aOO
.getLength() ) == 0)
464 aNewPath
+= aPath
.copy(aOO
.getLength());
472 OUString aPath
= ASCII("org.OpenOffice.Setup/blah/blub");
474 OUString aNewPath
= changeToComSunStarPath(aPath
);
475 volatile int dummy
= 0;
484 cout << "This is A::run();" << endl;
494 cout << "This is B::run();" << endl;
506 // -----------------------------------------------------------------------------
507 // ------------------------------------ Map ------------------------------------
508 // -----------------------------------------------------------------------------
511 bool operator()(const rtl::OUString
&s1
, const rtl::OUString
&s2
) const
513 return s1
.compareTo(s2
) < 0 ? true : false;
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
;
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
;
548 // -----------------------------------------------------------------------------
549 // ---------------------------------- HashMap ----------------------------------
550 // -----------------------------------------------------------------------------
558 bool operator()(const rtl::OUString
&s1
, const rtl::OUString
&s2
) const
560 return s1
.equals(s2
) == sal_True
? true : false;
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();
572 for (sal_Int64 i
=0;i
<nStrLen
; ++i
)
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
615 int getValue() {return m_aValue
;}
616 void setValue(int _aValue
) {m_aValue
= _aValue
;}
623 vos::ORef
<Options
> m_aOptions
;
626 vos::ORef
<Options
> getOptions() {return m_aOptions
;}
627 void setOptions(vos::ORef
<Options
>& _aOptions
) {
628 m_aOptions
= _aOptions
;
634 vos::ORef
<Options
> aO
= new Options
;
640 cout
<< "Options from a : " << a
.getOptions()->getValue() << endl
;
641 cout
<< "Options from b : " << b
.getOptions()->getValue() << endl
;
644 cout
<< "Options from a : " << a
.getOptions()->getValue() << endl
;
645 cout
<< "Options from b : " << b
.getOptions()->getValue() << endl
;
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();
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();
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();
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;
731 // -----------------------------------------------------------------------------
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);
756 OSL_ENSURE(false, aStr.getStr());
761 } // namespace configmgr