merge the formfield patch from ooo-build
[ooovba.git] / configmgr / workben / local_io / xmlexport.cxx
blob8118b775f5a580df1c1223bba03bf79bacdf00ff
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: xmlexport.cxx,v $
10 * $Revision: 1.5 $
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<iostream>
35 #include<osl/file.hxx>
37 #include <rtl/ustring.hxx>
38 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
39 #include <cppuhelper/implbase1.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #include <cppuhelper/servicefactory.hxx>
43 #include <cppuhelper/implbase1.hxx>
45 #ifndef _COM_SUN_STAR_LANG_XCOMPONENT_HDL_
46 #include <com/sun/star/lang/XComponent.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_IO_XDATATRANSFEREVENTLISTENER_HDL_
49 #include <com/sun/star/io/XDataTransferEventListener.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_IO_XDATAIMPORTER_HDL_
52 #include <com/sun/star/io/XDataExporter.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_IO_XINPUTSTREAM_HDL_
55 #include <com/sun/star/io/XOutputStream.hpp>
56 #endif
57 #include <com/sun/star/io/XActiveDataSource.hpp>
58 #include <com/sun/star/xml/sax/SAXParseException.hpp>
59 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
60 #include <osl/diagnose.h>
61 #include <com/sun/star/xml/sax/XAttributeList.hpp>
63 #include "oslstream.hxx"
64 #include "attributes.hxx"
65 #include "typeconverter.hxx"
66 #include "xmlformater.hxx"
67 #include "filehelper.hxx"
70 #include <com/sun/star/container/XNameAccess.hpp>
71 #include <com/sun/star/lang/XServiceInfo.hpp>
72 #include <com/sun/star/beans/PropertyValue.hpp>
73 #include <com/sun/star/lang/IllegalArgumentException.hpp>
75 #include "createpropertyvalue.hxx"
76 // -----------------------------------------------------------------------------
77 // --------------------------------- namespaces ---------------------------------
78 // -----------------------------------------------------------------------------
79 namespace configmgr
82 namespace uno = com::sun::star::uno;
83 namespace lang = com::sun::star::lang;
84 namespace io = com::sun::star::io;
85 namespace sax = com::sun::star::xml::sax;
86 namespace beans = com::sun::star::beans;
88 using ::rtl::OUString;
89 using ::osl::File;
91 using namespace ::com::sun::star::uno;
92 using namespace ::com::sun::star::lang;
93 using namespace ::com::sun::star::container;
95 // -----------------------------------------------------------------------------
96 // ---------------------------------- defines ----------------------------------
97 // -----------------------------------------------------------------------------
98 #define ASCII(x) OUString::createFromAscii(x)
100 // -----------------------------------------------------------------------------
101 class Listener : public ::cppu::WeakImplHelper1<io::XDataTransferEventListener>
103 virtual void SAL_CALL disposing( const lang::EventObject& Source )
104 throw(::com::sun::star::uno::RuntimeException)
106 OSL_ENSURE(0, "disposing");
110 virtual void SAL_CALL finished( const io::DataTransferEvent& aEvent )
111 throw(uno::RuntimeException)
113 OSL_ENSURE(0, "finished");
116 virtual void SAL_CALL cancelled( const io::DataTransferEvent& aEvent )
117 throw(uno::RuntimeException)
119 OSL_ENSURE(0, "cancelled");
123 class Component: public ::cppu::WeakImplHelper1<lang::XComponent>
125 virtual void SAL_CALL dispose( )
126 throw(uno::RuntimeException)
128 OSL_ENSURE(0, "dispose");
131 virtual void SAL_CALL addEventListener( const uno::Reference< lang::XEventListener >& xListener )
132 throw(uno::RuntimeException)
134 OSL_ENSURE(0, "addEventListener");
136 virtual void SAL_CALL removeEventListener( const uno::Reference< lang::XEventListener >& aListener )
137 throw(uno::RuntimeException)
139 OSL_ENSURE(0, "removeEventListener");
147 #define ASCII_STRING(rtlOUString) rtl::OUStringToOString(rtlOUString, RTL_TEXTENCODING_ASCII_US).getStr()
148 static ::rtl::OUString sRegistry = ::rtl::OUString::createFromAscii("applicat.rdb");
157 // -----------------------------------------------------------------------------
159 // -----------------------------------------------------------------------------
160 void exportTest()
162 uno::Reference< lang::XMultiServiceFactory > xMSF;
165 xMSF = cppu::createRegistryServiceFactory(sRegistry, ::rtl::OUString());
167 catch (uno::Exception& e)
169 cout << "could not bootstrap the services from " << ASCII_STRING(sRegistry) << endl ;
170 cout << " (error message : " << ASCII_STRING(e.Message) << ")" << endl;
173 if (!xMSF.is())
175 cerr << "could not create the service factory !" << endl;
176 return;
179 OUString aPath = ASCII("l:/SRC601/configmgr/workben/local_io");
180 OUString sFilename = ASCII("exported_config");
181 OUString aExtension = ASCII("xml");
183 OUString aFullname = aPath + ASCII("/") + sFilename + ASCII(".") + aExtension;
185 // Filename convertieren
186 OUString aURL;
187 File aConvert(ASCII(""));
188 aConvert.normalizePath(aFullname, aURL);
190 FileHelper::tryToRemoveFile(aURL);
192 rtl::OUString sError;
194 // File oeffnen
195 File aFile(aURL);
196 osl::FileBase::RC eError = aFile.open(OpenFlag_Write | OpenFlag_Create);
198 if (eError != osl_File_E_None)
200 sError = ASCII("XMLExportTest:");
201 rtl::OString aStr = rtl::OUStringToOString(sError,RTL_TEXTENCODING_ASCII_US);
202 OSL_ENSURE(0, aStr.getStr());
203 return;
206 // create an outputstream
207 uno::Reference<io::XOutputStream> xOutputStream = new configmgr::OSLOutputStreamWrapper(aFile);
209 // Listener & Component
210 uno::Reference<io::XDataTransferEventListener> rListener = new Listener();
211 uno::Reference<lang::XComponent> rComponent = new Component();
214 OUString sPath = ASCII("com.sun.star.ucb.Hierarchy");
216 // Create a TypeConverter
217 uno::Reference<script::XTypeConverter> aConverter;
218 aConverter = aConverter.query(xMSF->createInstance(ASCII( "com.sun.star.script.Converter" )) );
220 // prepare parameters for DataExport
221 Sequence< uno::Any > aArgs(4);
222 aArgs[0] <<= createPropertyValue(ASCII("PackageName"), sFilename);
223 aArgs[1] <<= createPropertyValue(ASCII("Path"), sPath); // multiple occur possible
224 aArgs[2] <<= createPropertyValue(ASCII("TypeConverter"), aConverter); // optional
225 aArgs[3] <<= createPropertyValue(ASCII("Path"), ASCII("com.sun.star.office.Setup")); // multiple occur possible
227 // important: createInstanceWithArguments will throw an IllegalArgumentException() if
228 // parameters are wrong...
229 Reference< XInterface > xDataExport = xMSF->createInstanceWithArguments(
230 OUString::createFromAscii("com.sun.star.configuration.DataExport"),
231 aArgs);
233 uno::Reference<io::XDataExporter> rExporter(xDataExport, UNO_QUERY);
235 // Export Data
236 rExporter->exportData(xOutputStream, rComponent, rListener);
239 } // namespace configmgr