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: xmlimport.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 <com/sun/star/uno/Sequence.hxx>
41 #include <com/sun/star/uno/Any.hxx>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <cppuhelper/implbase1.hxx>
44 #include <cppuhelper/implbase2.hxx>
45 #include <cppuhelper/servicefactory.hxx>
47 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
48 #include <com/sun/star/lang/XComponent.hpp>
50 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HDL_
51 #include <com/sun/star/io/XActiveDataSource.hpp>
53 #ifndef _COM_SUN_STAR_IO_XACTIVEDATASINK_HDL_
54 #include <com/sun/star/io/XActiveDataSink.hpp>
56 #ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HDL_
57 #include <com/sun/star/io/XActiveDataControl.hpp>
59 #ifndef _COM_SUN_STAR_IO_XDATATRANSFEREVENTLISTENER_HDL_
60 #include <com/sun/star/io/XDataTransferEventListener.hpp>
62 #ifndef _COM_SUN_STAR_IO_XDATAIMPORTER_HDL_
63 #include <com/sun/star/io/XDataImporter.hpp>
66 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HDL_
67 #include <com/sun/star/io/XInputStream.hpp>
69 #include <com/sun/star/xml/sax/XParser.hpp>
70 #include <com/sun/star/xml/sax/SAXParseException.hpp>
71 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
73 #include <com/sun/star/xml/sax/InputSource.hpp>
74 #include <vos/thread.hxx>
76 #include <vos/pipe.hxx>
77 #include <osl/diagnose.h>
78 #include "oslstream.hxx"
79 #include <com/sun/star/xml/sax/XAttributeList.hpp>
81 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
82 #include <com/sun/star/container/XNameAccess.hpp>
83 #include <com/sun/star/container/XHierarchicalName.hpp>
85 #include <com/sun/star/container/XNameReplace.hpp>
86 #include <com/sun/star/util/XChangesBatch.hpp>
87 #include <com/sun/star/script/XTypeConverter.hpp>
88 #include <com/sun/star/container/XNameContainer.hpp>
89 #include <osl/conditn.hxx>
91 #include "xmltreebuilder.hxx"
93 #include "dataimport.hxx"
95 #include "createpropertyvalue.hxx"
96 // -----------------------------------------------------------------------------
97 // --------------------------------- namespaces ---------------------------------
98 // -----------------------------------------------------------------------------
99 namespace uno
= com::sun::star::uno
;
100 namespace lang
= com::sun::star::lang
;
101 namespace io
= com::sun::star::io
;
102 namespace sax
= com::sun::star::xml::sax
;
103 namespace script
= com::sun::star::script
;
105 using ::rtl::OUString
;
108 using namespace ::com::sun::star::uno
;
109 using namespace ::com::sun::star::lang
;
110 using namespace ::com::sun::star::container
;
111 using namespace ::com::sun::star::util
;
113 // -----------------------------------------------------------------------------
114 // ---------------------------------- defines ----------------------------------
115 // -----------------------------------------------------------------------------
117 #define ASCII(x) OUString::createFromAscii(x)
121 // -----------------------------------------------------------------------------
122 // ---------------------------------- a Class ----------------------------------
123 // -----------------------------------------------------------------------------
124 // <Name a="xyz" b="bar">
126 //==========================================================================
128 //==========================================================================
131 // -----------------------------------------------------------------------------
132 class Listener
: public ::cppu::WeakImplHelper1
<io::XDataTransferEventListener
>
134 osl::Condition m_aCondition
;
138 m_aCondition
.reset(); // will block
141 virtual void SAL_CALL
disposing( const lang::EventObject
& Source
)
142 throw(::com::sun::star::uno::RuntimeException
)
144 OSL_ENSURE(0, "disposing");
145 m_aCondition
.set(); // will not block
148 virtual void SAL_CALL
finished( const io::DataTransferEvent
& aEvent
)
149 throw(uno::RuntimeException
)
151 OSL_ENSURE(0, "finished");
152 m_aCondition
.set(); // will not block
155 virtual void SAL_CALL
cancelled( const io::DataTransferEvent
& aEvent
)
156 throw(uno::RuntimeException
)
158 OSL_ENSURE(0, "cancelled");
159 m_aCondition
.set(); // will not block
168 class Component
: public ::cppu::WeakImplHelper1
<lang::XComponent
>
170 virtual void SAL_CALL
dispose( )
171 throw(uno::RuntimeException
)
173 OSL_ENSURE(0, "dispose");
176 virtual void SAL_CALL
addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
177 throw(uno::RuntimeException
)
179 OSL_ENSURE(0, "addEventListener");
181 virtual void SAL_CALL
removeEventListener( const uno::Reference
< lang::XEventListener
>& aListener
)
182 throw(uno::RuntimeException
)
184 OSL_ENSURE(0, "removeEventListener");
193 // -----------------------------------------------------------------------------
194 // ------------------------------------ Test ------------------------------------
195 // -----------------------------------------------------------------------------
196 #define ASCII_STRING(rtlOUString) rtl::OUStringToOString(rtlOUString, RTL_TEXTENCODING_ASCII_US).getStr()
197 static ::rtl::OUString sRegistry
= ::rtl::OUString::createFromAscii("applicat.rdb");
201 uno::Reference
< lang::XMultiServiceFactory
> xMSF
;
204 xMSF
= cppu::createRegistryServiceFactory(sRegistry
, ::rtl::OUString());
206 catch (uno::Exception
& e
)
208 cout
<< "could not bootstrap the services from " << ASCII_STRING(sRegistry
) << endl
;
209 cout
<< " (error message : " << ASCII_STRING(e
.Message
) << ")" << endl
;
214 cerr
<< "could not create the service factory !" << endl
;
218 OUString aPath
= ASCII("l:/SRC601/configmgr/workben/local_io");
219 OUString aFilename
= ASCII("hierarchy");
220 OUString aExtension
= ASCII("xml");
222 OUString aFullname
= aPath
+ ASCII("/") + aFilename
+ ASCII(".") + aExtension
;
224 // Filename convertieren
226 File
aConvert(ASCII(""));
227 aConvert
.normalizePath(aFullname
, aURL
);
231 aFile
.open(osl_File_OpenFlag_Read
);
233 // sal_uInt64 nBytesRead;
234 // uno::Sequence< sal_Int8 > aBufferSeq(2000);
235 // sal_Int8 *pBuff = aBufferSeq.getArray();
236 // aFile.read(pBuff, 2000, nBytesRead);
240 uno::Reference
<io::XInputStream
> xInputStream
= new configmgr::OSLInputStreamWrapper(aFile
);
242 uno::Reference
<uno::XInterface
> xPump
= xMSF
->createInstance( L
"com.sun.star.io.Pump" );
243 OSL_ENSURE(xPump
.is(), "there is no pump");
245 uno::Reference
<io::XActiveDataSink
> xPumpSink(xPump
, uno::UNO_QUERY
);
246 xPumpSink
->setInputStream(xInputStream
);
248 uno::Reference
<io::XActiveDataSource
> xPumpSource(xPump
, uno::UNO_QUERY
);
250 Listener
*pListener
= new Listener();
251 uno::Reference
<io::XDataTransferEventListener
> rListener
= pListener
;
252 uno::Reference
<lang::XComponent
> rComponent
= new Component();
254 // Create a TypeConverter
255 uno::Reference
<script::XTypeConverter
> aConverter
;
256 aConverter
= aConverter
.query(xMSF
->createInstance(ASCII( "com.sun.star.script.Converter" )) );
258 Sequence
< uno::Any
> aArgs(1); // optional arg.
259 aArgs
[0] <<= configmgr::createPropertyValue(ASCII("TypeConverter"), aConverter
);
261 Reference
< XInterface
> xDataImport
= xMSF
->createInstanceWithArguments(
262 OUString::createFromAscii("com.sun.star.configuration.DataImport"),
265 if (xDataImport
.is())
267 uno::Reference
<io::XDataImporter
> rImporter(xDataImport
, UNO_QUERY
);
270 rImporter
->importData(xPumpSource
, rComponent
, rListener
);
273 uno::Reference
<io::XActiveDataControl
> xControl(xPump
, uno::UNO_QUERY
);
277 // rImporter->cancel();
279 // pImporter->wait();
280 // Wait until the listener send ready
286 // -----------------------------------------------------------------------------
287 // -------------------------------- Import Test --------------------------------
288 // -----------------------------------------------------------------------------
290 // ----------- TEST ReadAccess -----------
291 void showSequence(const Sequence
<OUString
> &aSeq
);
296 uno::Reference
< lang::XMultiServiceFactory
> xMSF
;
299 xMSF
= cppu::createRegistryServiceFactory(sRegistry
, ::rtl::OUString());
301 catch (uno::Exception
& e
)
303 cout
<< "could not bootstrap the services from " << ASCII_STRING(sRegistry
) << endl
;
304 cout
<< " (error message : " << ASCII_STRING(e
.Message
) << ")" << endl
;
309 cerr
<< "could not create the service factory !" << endl
;
314 Reference
< XMultiServiceFactory
> xCfgProvider( xMSF
->createInstance(
315 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
318 if (!xCfgProvider
.is())
320 OSL_ENSURE(0, "No Configuration Provider");
323 OUString sPath
= ASCII("com.sun.star.ucb.Hierarchy");
325 Sequence
< Any
> aArgs(1);
328 Reference
< XInterface
> xCfgUpdt
= xCfgProvider
->createInstanceWithArguments(
329 OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
332 Reference
< XNameAccess
> xNameAccess(xCfgUpdt
, UNO_QUERY
);
334 // which Names are exist?
335 showSequence(xNameAccess
->getElementNames());
337 OUString aNamePath
= ASCII("Root");
338 if (xNameAccess
->hasByName(aNamePath
))
341 aAny
= xNameAccess
->getByName(aNamePath
);
342 TypeClass aTypeClass
= aAny
.getValueTypeClass();
343 if (aAny
.getValueTypeClass() == TypeClass_INTERFACE
)
345 Reference
< XInterface
> xInterface
;
347 Reference
< XNameAccess
> xNameAccess2(xInterface
, UNO_QUERY
);
348 if (xNameAccess
.is())
350 Sequence
<OUString
> aSeq
= xNameAccess2
->getElementNames();
355 Reference<XHierarchicalName> xHierarchical(xNameAccess, UNO_QUERY);
356 OUString aHierachicalName = xHierarchical->getHierarchicalName();
358 Sequence< Any > aArgs(2);
359 aArgs[0] <<= aHierachicalName;
361 aArgs[1] <<= nLevels;
363 Reference< XInterface > xCfgNewUpdt = xCfgProvider->createInstanceWithArguments(
364 OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
367 Reference
< lang::XSingleServiceFactory
> xChildFactory(xNameAccess2
, UNO_QUERY
);
368 if (xChildFactory
.is())
370 Reference
< XInterface
> xChild
= xChildFactory
->createInstance();
373 Reference
<XNameAccess
> xObjectOnTheMedow(xChild
, UNO_QUERY
);
378 // which Names are exist?
379 showSequence(xObjectOnTheMedow
->getElementNames());
381 OUString aChildren
= ASCII("Children");
382 if (xObjectOnTheMedow
->hasByName(aChildren
))
385 aAny
= xObjectOnTheMedow
->getByName(aChildren
);
386 TypeClass aTypeClass
= aAny
.getValueTypeClass();
387 if (aAny
.getValueTypeClass() == TypeClass_INTERFACE
)
389 Reference
< XInterface
> xInterface
;
391 Reference
< XNameAccess
> xChildFromOOM(xInterface
, UNO_QUERY
);
393 if (xChildFromOOM
.is())
395 // insert a new Object in the new Object
397 Reference
< lang::XSingleServiceFactory
> xChildFactory(xChildFromOOM
, UNO_QUERY
);
398 if (xChildFactory
.is())
400 Reference
< XInterface
> xChild
= xChildFactory
->createInstance();
403 Reference
<XNameContainer
> xNameContainer(xChildFromOOM
, UNO_QUERY
);
404 if (xNameContainer
.is())
406 xNameContainer
->insertByName(ASCII("Test2"), makeAny(xChild
));
415 Reference
<XNameContainer
> xNameContainer(xNameAccess2
, UNO_QUERY
);
416 if (xNameContainer
.is())
418 xNameContainer
->insertByName(ASCII("Test"), makeAny(xObjectOnTheMedow
));
423 Reference
< XChangesBatch
> xChangesBatch(xCfgUpdt
, UNO_QUERY
);
424 if (xChangesBatch
.is())
426 xChangesBatch
->commitChanges();
430 Sequence
<OUString
> aSeq2
= xNameAccess2
->getElementNames();
434 OUString *pStr = aSeq.getArray();
435 for (int i=0;i<aSeq.getLength();i++)
437 OUString aStr = pStr[i];
439 aAny = xNameAccess->getByName(aStr);
440 TypeClass aTypeClass = aAny.getValueTypeClass();
442 Reference< XNameReplace > xNameReplace(xNameAccess, UNO_QUERY);
445 sal_Bool bValue = false;
447 xNameReplace->replaceByName(aStr, aNewAny);
449 volatile int dummy = 0;
454 volatile int dummy
= 0;
458 // ------------------------------ Helperfunctions ------------------------------
460 void showSequence(const Sequence
<OUString
> &aSeq
)
463 const OUString
*pStr
= aSeq
.getConstArray();
464 for (int i
=0;i
<aSeq
.getLength();i
++)
466 OUString aStr
= pStr
[i
];
467 aArray
+= aStr
+ ASCII(", ");
469 volatile int dummy
= 0;
472 } // namespace configmgr