summaryrefslogtreecommitdiff
path: root/configmgr/workben/local_io/xmlimport.cxx
blob: f206490fdaf1f815f897bb4ac62b08d65a707407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: xmlimport.cxx,v $
 * $Revision: 1.7 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_configmgr.hxx"

#include <memory>
#include <vector>
#include <stack>
#include<osl/file.hxx>

#include <rtl/ustring.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <cppuhelper/implbase1.hxx>
#include <cppuhelper/implbase2.hxx>
#include <cppuhelper/servicefactory.hxx>

#ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
#include <com/sun/star/lang/XComponent.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HDL_
#include <com/sun/star/io/XActiveDataSource.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XACTIVEDATASINK_HDL_
#include <com/sun/star/io/XActiveDataSink.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HDL_
#include <com/sun/star/io/XActiveDataControl.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XDATATRANSFEREVENTLISTENER_HDL_
#include <com/sun/star/io/XDataTransferEventListener.hpp>
#endif
#ifndef _COM_SUN_STAR_IO_XDATAIMPORTER_HDL_
#include <com/sun/star/io/XDataImporter.hpp>
#endif

#ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HDL_
#include <com/sun/star/io/XInputStream.hpp>
#endif
#include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/xml/sax/SAXParseException.hpp>
#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>

#include <com/sun/star/xml/sax/InputSource.hpp>
#include <vos/thread.hxx>

#include <vos/pipe.hxx>
#include <osl/diagnose.h>
#include "oslstream.hxx"
#include <com/sun/star/xml/sax/XAttributeList.hpp>

#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XHierarchicalName.hpp>

#include <com/sun/star/container/XNameReplace.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <com/sun/star/script/XTypeConverter.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <osl/conditn.hxx>

#include "xmltreebuilder.hxx"

#include "dataimport.hxx"

#include "createpropertyvalue.hxx"
// -----------------------------------------------------------------------------
// --------------------------------- namespaces ---------------------------------
// -----------------------------------------------------------------------------
namespace uno = com::sun::star::uno;
namespace lang = com::sun::star::lang;
namespace io = com::sun::star::io;
namespace sax = com::sun::star::xml::sax;
namespace script = com::sun::star::script;

using ::rtl::OUString;
using ::osl::File;

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;

// -----------------------------------------------------------------------------
// ---------------------------------- defines ----------------------------------
// -----------------------------------------------------------------------------
#ifndef ASCII
#define ASCII(x) OUString::createFromAscii(x)
#endif


// -----------------------------------------------------------------------------
// ---------------------------------- a Class ----------------------------------
// -----------------------------------------------------------------------------
// <Name a="xyz" b="bar">

//==========================================================================
//= Visitors
//==========================================================================


// -----------------------------------------------------------------------------
class Listener : public ::cppu::WeakImplHelper1<io::XDataTransferEventListener>
{
    osl::Condition m_aCondition;
public:
    Listener()
        {
            m_aCondition.reset();				 // will block
        }
    
    virtual void SAL_CALL disposing( const lang::EventObject& Source )
        throw(::com::sun::star::uno::RuntimeException)
        {
            OSL_ENSURE(0, "disposing");
            m_aCondition.set();				 // will not block
        }
    
    virtual void SAL_CALL finished( const io::DataTransferEvent& aEvent ) 
        throw(uno::RuntimeException)
        {
            OSL_ENSURE(0, "finished");
            m_aCondition.set();				 // will not block
        }
    
    virtual void SAL_CALL cancelled( const io::DataTransferEvent& aEvent )
        throw(uno::RuntimeException)
        {
            OSL_ENSURE(0, "cancelled");
            m_aCondition.set();				 // will not block
        }
public:
    void wait()
        {
            m_aCondition.wait();
        }
};

class Component: public ::cppu::WeakImplHelper1<lang::XComponent>
{
    virtual void SAL_CALL dispose(  )
        throw(uno::RuntimeException)
        {
            OSL_ENSURE(0, "dispose");

        }
    virtual void SAL_CALL addEventListener( const uno::Reference< lang::XEventListener >& xListener )
        throw(uno::RuntimeException)
        {
            OSL_ENSURE(0, "addEventListener");
        }
    virtual void SAL_CALL removeEventListener( const uno::Reference< lang::XEventListener >& aListener )
        throw(uno::RuntimeException)
        {
            OSL_ENSURE(0, "removeEventListener");
        }
};



namespace configmgr
{
    
// -----------------------------------------------------------------------------
// ------------------------------------ Test ------------------------------------
// -----------------------------------------------------------------------------
#define ASCII_STRING(rtlOUString)	rtl::OUStringToOString(rtlOUString, RTL_TEXTENCODING_ASCII_US).getStr()
    static ::rtl::OUString	sRegistry = ::rtl::OUString::createFromAscii("applicat.rdb");
    
    void importTest()
    {
        uno::Reference< lang::XMultiServiceFactory > xMSF;
        try
        {
            xMSF = cppu::createRegistryServiceFactory(sRegistry, ::rtl::OUString());
        }
        catch (uno::Exception& e)
        {
            cout << "could not bootstrap the services from " << ASCII_STRING(sRegistry) << endl ;
            cout << "    (error message : " << ASCII_STRING(e.Message) << ")" << endl;
        }
        
        if (!xMSF.is())
        {
            cerr << "could not create the service factory !" << endl;
            return;
        }
        
        OUString aPath = ASCII("l:/SRC601/configmgr/workben/local_io");
        OUString aFilename = ASCII("hierarchy");
        OUString aExtension = ASCII("xml");
        
        OUString aFullname = aPath + ASCII("/") + aFilename + ASCII(".") + aExtension;
        
        // Filename convertieren
        OUString aURL;
        File aConvert(ASCII(""));
        aConvert.normalizePath(aFullname, aURL);
        
        // File oeffnen
        File aFile(aURL);
        aFile.open(osl_File_OpenFlag_Read);
        
        // sal_uInt64 nBytesRead;
        // uno::Sequence< sal_Int8 > aBufferSeq(2000);
        // sal_Int8 *pBuff = aBufferSeq.getArray();
        // aFile.read(pBuff, 2000, nBytesRead);
        // 	   
        // aFile.close();
        
        uno::Reference<io::XInputStream> xInputStream = new configmgr::OSLInputStreamWrapper(aFile);
        
        uno::Reference <uno::XInterface> xPump = xMSF->createInstance( L"com.sun.star.io.Pump" );
        OSL_ENSURE(xPump.is(), "there is no pump");
        
        uno::Reference<io::XActiveDataSink> xPumpSink(xPump, uno::UNO_QUERY);
        xPumpSink->setInputStream(xInputStream);
        
        uno::Reference<io::XActiveDataSource> xPumpSource(xPump, uno::UNO_QUERY);
        
        Listener *pListener = new Listener();
        uno::Reference<io::XDataTransferEventListener>  rListener = pListener;
        uno::Reference<lang::XComponent> rComponent = new Component();
        
        // Create a TypeConverter
        uno::Reference<script::XTypeConverter> aConverter;
        aConverter = aConverter.query(xMSF->createInstance(ASCII( "com.sun.star.script.Converter" )) );
        
        Sequence< uno::Any > aArgs(1);			 // optional arg.
        aArgs[0] <<= configmgr::createPropertyValue(ASCII("TypeConverter"), aConverter);
        
        Reference< XInterface > xDataImport = xMSF->createInstanceWithArguments(
            OUString::createFromAscii("com.sun.star.configuration.DataImport"),
            aArgs);
        
        if (xDataImport.is())
        {
            uno::Reference<io::XDataImporter>  rImporter(xDataImport, UNO_QUERY);
            
            // Import Data
            rImporter->importData(xPumpSource, rComponent, rListener);
            
            // lets pump
            uno::Reference<io::XActiveDataControl> xControl(xPump, uno::UNO_QUERY);
            xControl->start();
            
            // TEST:
            // rImporter->cancel();
            
            // pImporter->wait();
            // Wait until the listener send ready
            pListener->wait();
        }
        return;
    }

// -----------------------------------------------------------------------------
// -------------------------------- Import Test --------------------------------
// -----------------------------------------------------------------------------

    // ----------- TEST ReadAccess ----------- 
    void showSequence(const Sequence<OUString> &aSeq);

void hierarchyTest()
{

    uno::Reference< lang::XMultiServiceFactory > xMSF;
    try
    {
        xMSF = cppu::createRegistryServiceFactory(sRegistry, ::rtl::OUString());
    }
    catch (uno::Exception& e)
    {
        cout << "could not bootstrap the services from " << ASCII_STRING(sRegistry) << endl ;
        cout << "    (error message : " << ASCII_STRING(e.Message) << ")" << endl;
    }
    
    if (!xMSF.is())
    {
        cerr << "could not create the service factory !" << endl;
        return;
    }
        

    Reference< XMultiServiceFactory > xCfgProvider( xMSF->createInstance(
        ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
                                                    UNO_QUERY);
    
    if (!xCfgProvider.is())
    {
        OSL_ENSURE(0, "No Configuration Provider");
    }
    
    OUString sPath = ASCII("com.sun.star.ucb.Hierarchy");
    
    Sequence< Any > aArgs(1);
    aArgs[0] <<= sPath;

    Reference< XInterface > xCfgUpdt = xCfgProvider->createInstanceWithArguments(
        OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
        aArgs);

    Reference< XNameAccess > xNameAccess(xCfgUpdt, UNO_QUERY);

    // which Names are exist?
    showSequence(xNameAccess->getElementNames());

    OUString aNamePath = ASCII("Root");
    if (xNameAccess->hasByName(aNamePath))
    {
        Any aAny;
        aAny = xNameAccess->getByName(aNamePath);
        TypeClass aTypeClass = aAny.getValueTypeClass();
        if (aAny.getValueTypeClass() == TypeClass_INTERFACE)
        {
            Reference< XInterface > xInterface;
            aAny >>= xInterface;
            Reference< XNameAccess > xNameAccess2(xInterface, UNO_QUERY);
            if (xNameAccess.is())
            {
                Sequence<OUString> aSeq = xNameAccess2->getElementNames();
                showSequence(aSeq);

                // insert a new set
/*
                Reference<XHierarchicalName> xHierarchical(xNameAccess, UNO_QUERY);
                OUString aHierachicalName = xHierarchical->getHierarchicalName();

                Sequence< Any > aArgs(2);
                aArgs[0] <<= aHierachicalName;
                sal_Int32 nLevels=1;
                aArgs[1] <<= nLevels;

                Reference< XInterface > xCfgNewUpdt = xCfgProvider->createInstanceWithArguments(
                    OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
                    aArgs);
*/				
                Reference< lang::XSingleServiceFactory > xChildFactory(xNameAccess2, UNO_QUERY);
                if (xChildFactory.is())
                {
                    Reference< XInterface > xChild = xChildFactory->createInstance();
                    if (xChild.is())
                    {
                        Reference<XNameAccess> xObjectOnTheMedow(xChild, UNO_QUERY);
                        



                        // which Names are exist?
                        showSequence(xObjectOnTheMedow->getElementNames());

                        OUString aChildren = ASCII("Children");
                        if (xObjectOnTheMedow->hasByName(aChildren))
                        {
                            Any aAny;
                            aAny = xObjectOnTheMedow->getByName(aChildren);
                            TypeClass aTypeClass = aAny.getValueTypeClass();
                            if (aAny.getValueTypeClass() == TypeClass_INTERFACE)
                            {
                                Reference< XInterface > xInterface;
                                aAny >>= xInterface;
                                Reference< XNameAccess > xChildFromOOM(xInterface, UNO_QUERY);

                                if (xChildFromOOM.is())
                                {
                                    // insert a new Object in the new Object

                                    Reference< lang::XSingleServiceFactory > xChildFactory(xChildFromOOM, UNO_QUERY);
                                    if (xChildFactory.is())
                                    {
                                        Reference< XInterface > xChild = xChildFactory->createInstance();
                                        if (xChild.is())
                                        {
                                            Reference<XNameContainer> xNameContainer(xChildFromOOM, UNO_QUERY);
                                            if (xNameContainer.is())
                                            {
                                                xNameContainer->insertByName(ASCII("Test2"), makeAny(xChild));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        

                        Reference<XNameContainer> xNameContainer(xNameAccess2, UNO_QUERY);
                        if (xNameContainer.is())
                        {
                            xNameContainer->insertByName(ASCII("Test"), makeAny(xObjectOnTheMedow));
                        }
                    }

                    // commit changes
                    Reference< XChangesBatch > xChangesBatch(xCfgUpdt, UNO_QUERY);
                    if (xChangesBatch.is())
                    {
                        xChangesBatch->commitChanges();
                    }
                }

                Sequence<OUString> aSeq2 = xNameAccess2->getElementNames();
                showSequence(aSeq2);

/*
                OUString *pStr = aSeq.getArray();
                for (int i=0;i<aSeq.getLength();i++)
                {
                    OUString aStr = pStr[i];
                    Any aAny;
                    aAny = xNameAccess->getByName(aStr);
                    TypeClass aTypeClass = aAny.getValueTypeClass();

                    Reference< XNameReplace > xNameReplace(xNameAccess, UNO_QUERY);

                    Any aNewAny;
                    sal_Bool bValue = false;
                    aNewAny <<= bValue;
                    xNameReplace->replaceByName(aStr, aNewAny);

                    volatile int dummy = 0;
                }
*/
            }
        }
        volatile int dummy = 0;
    }
}

// ------------------------------ Helperfunctions ------------------------------

    void showSequence(const Sequence<OUString> &aSeq)
    {
        OUString aArray;
        const OUString *pStr = aSeq.getConstArray();
        for (int i=0;i<aSeq.getLength();i++)
        {
            OUString aStr = pStr[i];
            aArray += aStr + ASCII(", ");
        }
        volatile int dummy = 0;
    }
    
} // namespace configmgr