Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / xmlscript / test / imexp.cxx
blobeadedce6b0ff4654d2dd4a09935f9c104cf06273
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <rtl/memory.h>
32 #include "osl/file.h"
34 #include <rtl/ustrbuf.hxx>
36 #include <xmlscript/xmldlg_imexp.hxx>
37 #include <xmlscript/xml_helper.hxx>
39 #include <cppuhelper/servicefactory.hxx>
40 #include <cppuhelper/bootstrap.hxx>
41 #include <cppuhelper/implbase2.hxx>
43 #include <comphelper/processfactory.hxx>
45 #include <vcl/svapp.hxx>
47 #include <com/sun/star/io/XActiveDataSource.hpp>
49 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/lang/XInitialization.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/registry/XSimpleRegistry.hpp>
54 #include <com/sun/star/registry/XImplementationRegistration.hpp>
55 #include <com/sun/star/uno/XComponentContext.hpp>
57 #include <com/sun/star/awt/XToolkit.hpp>
58 #include <com/sun/star/awt/XControlModel.hpp>
59 #include <com/sun/star/awt/XControl.hpp>
60 #include <com/sun/star/awt/XDialog.hpp>
62 #include <com/sun/star/container/XNameContainer.hpp>
65 using namespace ::rtl;
66 using namespace ::cppu;
67 using namespace ::com::sun::star;
68 using namespace ::com::sun::star::uno;
73 Reference< XComponentContext > createInitialComponentContext(
74 OUString const & inst_dir )
76 Reference< XComponentContext > xContext;
78 try
80 OUString file_url;
81 oslFileError rc = osl_getFileURLFromSystemPath(
82 inst_dir.pData, &file_url.pData );
83 OSL_ASSERT( osl_File_E_None == rc );
85 ::rtl::OUString unorc = file_url + OUString(
86 RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
88 return defaultBootstrap_InitialComponentContext( unorc );
91 catch( const Exception& rExc )
93 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
94 OSL_FAIL( aStr.getStr() );
97 return xContext;
101 // -----------------------------------------------------------------------
103 Reference< container::XNameContainer > importFile(
104 char const * fname,
105 Reference< XComponentContext > const & xContext )
107 // create the input stream
108 FILE *f = ::fopen( fname, "rb" );
109 if (f)
111 ::fseek( f, 0 ,SEEK_END );
112 int nLength = ::ftell( f );
113 ::fseek( f, 0, SEEK_SET );
115 ByteSequence bytes( nLength );
116 ::fread( bytes.getArray(), nLength, 1, f );
117 ::fclose( f );
119 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
120 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
121 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
123 return xModel;
125 else
127 throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
128 Reference< XInterface >() );
132 void exportToFile(
133 char const * fname,
134 Reference< container::XNameContainer > const & xModel,
135 Reference< XComponentContext > const & xContext )
137 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
138 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
140 Sequence< sal_Int8 > bytes;
141 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
142 for (;;)
144 Sequence< sal_Int8 > readBytes;
145 nRead = xStream->readBytes( readBytes, 1024 );
146 if (! nRead)
147 break;
148 OSL_ASSERT( readBytes.getLength() >= nRead );
150 sal_Int32 nPos = bytes.getLength();
151 bytes.realloc( nPos + nRead );
152 ::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
155 FILE * f = ::fopen( fname, "w" );
156 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
157 ::fflush( f );
158 ::fclose( f );
163 class MyApp : public Application
165 public:
166 void Main();
169 MyApp aMyApp;
171 // -----------------------------------------------------------------------
173 void MyApp::Main()
175 if (GetCommandLineParamCount() < 2)
177 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
178 return;
181 Reference< XComponentContext > xContext(
182 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
183 Reference< lang::XMultiServiceFactory > xMSF(
184 xContext->getServiceManager(), UNO_QUERY );
188 ::comphelper::setProcessServiceFactory( xMSF );
190 Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
191 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );
193 // import dialogs
194 OString aParam1( OUStringToOString(
195 OUString( GetCommandLineParam( 1 ) ),
196 RTL_TEXTENCODING_ASCII_US ) );
197 Reference< container::XNameContainer > xModel(
198 importFile( aParam1.getStr(), xContext ) );
199 OSL_ASSERT( xModel.is() );
201 Reference< awt::XControl > xDlg( xMSF->createInstance(
202 OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
203 xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
204 xDlg->createPeer( xToolkit, 0 );
205 Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
206 xD->execute();
208 if (GetCommandLineParamCount() == 3)
210 // write modified dialogs
211 OString aParam2( OUStringToOString(
212 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
213 exportToFile( aParam2.getStr(), xModel, xContext );
216 catch (const xml::sax::SAXException & rExc)
218 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
219 uno::Exception exc;
220 if (rExc.WrappedException >>= exc)
222 aStr += OString( " >>> " );
223 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
225 OSL_FAIL( aStr.getStr() );
227 catch (const uno::Exception & rExc)
229 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
230 OSL_FAIL( aStr.getStr() );
233 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
234 if (xComp.is())
236 xComp->dispose();
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */