Bump for 4.0-15
[LibreOffice.git] / xmlscript / test / imexp.cxx
blob42cc8851a86cefbaf4c36955dc33627527a6a76a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
22 #include "osl/file.h"
24 #include <rtl/ustrbuf.hxx>
26 #include <xmlscript/xmldlg_imexp.hxx>
27 #include <xmlscript/xml_helper.hxx>
29 #include <cppuhelper/servicefactory.hxx>
30 #include <cppuhelper/bootstrap.hxx>
31 #include <cppuhelper/implbase2.hxx>
33 #include <comphelper/processfactory.hxx>
35 #include <vcl/svapp.hxx>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/lang/XInitialization.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/registry/XSimpleRegistry.hpp>
44 #include <com/sun/star/registry/XImplementationRegistration.hpp>
45 #include <com/sun/star/uno/XComponentContext.hpp>
47 #include <com/sun/star/awt/XToolkit.hpp>
48 #include <com/sun/star/awt/XControlModel.hpp>
49 #include <com/sun/star/awt/XControl.hpp>
50 #include <com/sun/star/awt/XDialog.hpp>
52 #include <com/sun/star/container/XNameContainer.hpp>
55 using namespace ::rtl;
56 using namespace ::cppu;
57 using namespace ::com::sun::star;
58 using namespace ::com::sun::star::uno;
63 Reference< XComponentContext > createInitialComponentContext(
64 OUString const & inst_dir )
66 Reference< XComponentContext > xContext;
68 try
70 OUString file_url;
71 oslFileError rc = osl_getFileURLFromSystemPath(
72 inst_dir.pData, &file_url.pData );
73 OSL_ASSERT( osl_File_E_None == rc );
75 OUString unorc = file_url + OUString("/program/" SAL_CONFIGFILE("uno") );
77 return defaultBootstrap_InitialComponentContext( unorc );
80 catch( const Exception& rExc )
82 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
83 OSL_FAIL( aStr.getStr() );
86 return xContext;
90 // -----------------------------------------------------------------------
92 Reference< container::XNameContainer > importFile(
93 char const * fname,
94 Reference< XComponentContext > const & xContext )
96 // create the input stream
97 FILE *f = ::fopen( fname, "rb" );
98 if (f)
100 ::fseek( f, 0 ,SEEK_END );
101 int nLength = ::ftell( f );
102 ::fseek( f, 0, SEEK_SET );
104 ByteSequence bytes( nLength );
105 ::fread( bytes.getArray(), nLength, 1, f );
106 ::fclose( f );
108 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
109 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
110 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
112 return xModel;
114 else
116 throw Exception( "### Cannot read file!", Reference< XInterface >() );
120 void exportToFile(
121 char const * fname,
122 Reference< container::XNameContainer > const & xModel,
123 Reference< XComponentContext > const & xContext )
125 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
126 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
128 Sequence< sal_Int8 > bytes;
129 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
130 for (;;)
132 Sequence< sal_Int8 > readBytes;
133 nRead = xStream->readBytes( readBytes, 1024 );
134 if (! nRead)
135 break;
136 OSL_ASSERT( readBytes.getLength() >= nRead );
138 sal_Int32 nPos = bytes.getLength();
139 bytes.realloc( nPos + nRead );
140 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
143 FILE * f = ::fopen( fname, "w" );
144 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
145 ::fflush( f );
146 ::fclose( f );
151 class MyApp : public Application
153 public:
154 void Main();
157 MyApp aMyApp;
159 // -----------------------------------------------------------------------
161 void MyApp::Main()
163 if (GetCommandLineParamCount() < 2)
165 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
166 return;
169 Reference< XComponentContext > xContext(
170 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
171 Reference< lang::XMultiServiceFactory > xMSF(
172 xContext->getServiceManager(), UNO_QUERY );
176 ::comphelper::setProcessServiceFactory( xMSF );
178 Reference< awt::XToolkit> xToolkit( xMSF->createInstance( "com.sun.star.awt.ExtToolkit" ), UNO_QUERY );
180 // import dialogs
181 OString aParam1( OUStringToOString(
182 OUString( GetCommandLineParam( 1 ) ),
183 RTL_TEXTENCODING_ASCII_US ) );
184 Reference< container::XNameContainer > xModel(
185 importFile( aParam1.getStr(), xContext ) );
186 OSL_ASSERT( xModel.is() );
188 Reference< awt::XControl > xDlg( xMSF->createInstance( "com.sun.star.awt.UnoControlDialog" ), UNO_QUERY );
189 xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
190 xDlg->createPeer( xToolkit, 0 );
191 Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
192 xD->execute();
194 if (GetCommandLineParamCount() == 3)
196 // write modified dialogs
197 OString aParam2( OUStringToOString(
198 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
199 exportToFile( aParam2.getStr(), xModel, xContext );
202 catch (const xml::sax::SAXException & rExc)
204 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
205 uno::Exception exc;
206 if (rExc.WrappedException >>= exc)
208 aStr += OString( " >>> " );
209 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
211 OSL_FAIL( aStr.getStr() );
213 catch (const uno::Exception & rExc)
215 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
216 OSL_FAIL( aStr.getStr() );
219 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
220 if (xComp.is())
222 xComp->dispose();
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */