Update ooo320-m1
[ooovba.git] / xmlscript / test / imexp.cxx
blobae4853b118e4b0c5f6941363ca1cd92b74becf3f
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: imexp.cxx,v $
10 * $Revision: 1.20 $
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_xmlscript.hxx"
34 #include <stdio.h>
35 #include <rtl/memory.h>
36 #include "osl/file.h"
38 #include <rtl/ustrbuf.hxx>
40 #include <xmlscript/xmldlg_imexp.hxx>
41 #include <xmlscript/xml_helper.hxx>
43 #include <cppuhelper/servicefactory.hxx>
44 #include <cppuhelper/bootstrap.hxx>
45 #include <cppuhelper/implbase2.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <comphelper/regpathhelper.hxx>
50 #include <tools/debug.hxx>
51 #include <vcl/svapp.hxx>
53 #include <com/sun/star/io/XActiveDataSource.hpp>
55 #include <com/sun/star/lang/XComponent.hpp>
56 #include <com/sun/star/lang/XInitialization.hpp>
57 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/registry/XSimpleRegistry.hpp>
60 #include <com/sun/star/registry/XImplementationRegistration.hpp>
61 #include <com/sun/star/uno/XComponentContext.hpp>
63 #include <com/sun/star/awt/XToolkit.hpp>
64 #include <com/sun/star/awt/XControlModel.hpp>
65 #include <com/sun/star/awt/XControl.hpp>
66 #include <com/sun/star/awt/XDialog.hpp>
68 #include <com/sun/star/container/XNameContainer.hpp>
71 using namespace ::rtl;
72 using namespace ::cppu;
73 using namespace ::com::sun::star;
74 using namespace ::com::sun::star::uno;
79 Reference< XComponentContext > createInitialComponentContext(
80 OUString const & inst_dir )
82 Reference< XComponentContext > xContext;
84 try
86 OUString file_url;
87 oslFileError rc = osl_getFileURLFromSystemPath(
88 inst_dir.pData, &file_url.pData );
89 OSL_ASSERT( osl_File_E_None == rc );
91 ::rtl::OUString unorc = file_url + OUString(
92 RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
94 return defaultBootstrap_InitialComponentContext( unorc );
97 catch( Exception& rExc )
99 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
100 OSL_ENSURE( 0, aStr.getStr() );
103 return xContext;
107 // -----------------------------------------------------------------------
109 Reference< container::XNameContainer > importFile(
110 char const * fname,
111 Reference< XComponentContext > const & xContext )
113 // create the input stream
114 FILE *f = ::fopen( fname, "rb" );
115 if (f)
117 ::fseek( f, 0 ,SEEK_END );
118 int nLength = ::ftell( f );
119 ::fseek( f, 0, SEEK_SET );
121 ByteSequence bytes( nLength );
122 ::fread( bytes.getArray(), nLength, 1, f );
123 ::fclose( f );
125 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
126 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
127 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
129 return xModel;
131 else
133 throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
134 Reference< XInterface >() );
138 void exportToFile(
139 char const * fname,
140 Reference< container::XNameContainer > const & xModel,
141 Reference< XComponentContext > const & xContext )
143 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
144 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
146 Sequence< sal_Int8 > bytes;
147 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
148 for (;;)
150 Sequence< sal_Int8 > readBytes;
151 nRead = xStream->readBytes( readBytes, 1024 );
152 if (! nRead)
153 break;
154 OSL_ASSERT( readBytes.getLength() >= nRead );
156 sal_Int32 nPos = bytes.getLength();
157 bytes.realloc( nPos + nRead );
158 ::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
161 FILE * f = ::fopen( fname, "w" );
162 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
163 ::fflush( f );
164 ::fclose( f );
169 class MyApp : public Application
171 public:
172 void Main();
175 MyApp aMyApp;
177 // -----------------------------------------------------------------------
179 void MyApp::Main()
181 if (GetCommandLineParamCount() < 2)
183 OSL_ENSURE( 0, "usage: imexp inst_dir inputfile [outputfile]\n" );
184 return;
187 Reference< XComponentContext > xContext(
188 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
189 Reference< lang::XMultiServiceFactory > xMSF(
190 xContext->getServiceManager(), UNO_QUERY );
194 ::comphelper::setProcessServiceFactory( xMSF );
196 Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
197 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );
199 // import dialogs
200 OString aParam1( OUStringToOString(
201 OUString( GetCommandLineParam( 1 ) ),
202 RTL_TEXTENCODING_ASCII_US ) );
203 Reference< container::XNameContainer > xModel(
204 importFile( aParam1.getStr(), xContext ) );
205 OSL_ASSERT( xModel.is() );
207 Reference< awt::XControl > xDlg( xMSF->createInstance(
208 OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
209 xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
210 xDlg->createPeer( xToolkit, 0 );
211 Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
212 xD->execute();
214 if (GetCommandLineParamCount() == 3)
216 // write modified dialogs
217 OString aParam2( OUStringToOString(
218 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
219 exportToFile( aParam2.getStr(), xModel, xContext );
222 catch (xml::sax::SAXException & rExc)
224 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
225 uno::Exception exc;
226 if (rExc.WrappedException >>= exc)
228 aStr += OString( " >>> " );
229 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
231 OSL_ENSURE( 0, aStr.getStr() );
233 catch (uno::Exception & rExc)
235 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
236 OSL_ENSURE( 0, aStr.getStr() );
239 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
240 if (xComp.is())
242 xComp->dispose();