Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / xmlscript / test / imexp.cxx
blobd4dfd1679423722c7faf3ae64ba719573dcb7fa4
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 .
20 #include <config_folders.h>
22 #include <stdio.h>
23 #include <osl/file.h>
25 #include <rtl/ustrbuf.hxx>
26 #include <sal/log.hxx>
28 #include <xmlscript/xmldlg_imexp.hxx>
29 #include <xmlscript/xml_helper.hxx>
31 #include <cppuhelper/servicefactory.hxx>
32 #include <cppuhelper/bootstrap.hxx>
34 #include <comphelper/processfactory.hxx>
36 #include <vcl/svapp.hxx>
38 #include <com/sun/star/awt/UnoControlDialog.hpp>
39 #include <com/sun/star/awt/Toolkit.hpp>
40 #include <com/sun/star/awt/XToolkit.hpp>
41 #include <com/sun/star/awt/XControlModel.hpp>
42 #include <com/sun/star/awt/XControl.hpp>
43 #include <com/sun/star/awt/XDialog.hpp>
44 #include <com/sun/star/container/XNameContainer.hpp>
45 #include <com/sun/star/lang/XComponent.hpp>
46 #include <com/sun/star/lang/XInitialization.hpp>
47 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
48 #include <com/sun/star/io/XActiveDataSource.hpp>
49 #include <com/sun/star/registry/XSimpleRegistry.hpp>
50 #include <com/sun/star/registry/XImplementationRegistration.hpp>
51 #include <com/sun/star/uno/XComponentContext.hpp>
53 using namespace ::cppu;
54 using namespace ::com::sun::star;
55 using namespace ::com::sun::star::uno;
57 Reference< XComponentContext > createInitialComponentContext(
58 OUString const & inst_dir )
60 Reference< XComponentContext > xContext;
62 try
64 OUString file_url;
65 oslFileError rc = osl_getFileURLFromSystemPath(
66 inst_dir.pData, &file_url.pData );
67 OSL_ASSERT( osl_File_E_None == rc );
69 OUString unorc = file_url + ("/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("louno") );
71 return defaultBootstrap_InitialComponentContext( unorc );
74 catch( const Exception& rExc )
76 SAL_WARN( "xmlscript", rExc );
79 return xContext;
82 Reference< container::XNameContainer > importFile(
83 char const * fname,
84 Reference< XComponentContext > const & xContext )
86 // create the input stream
87 FILE *f = ::fopen( fname, "rb" );
88 if (f)
90 ::fseek( f, 0 ,SEEK_END );
91 int nLength = ::ftell( f );
92 ::fseek( f, 0, SEEK_SET );
94 ByteSequence bytes( nLength );
95 ::fread( bytes.getArray(), nLength, 1, f );
96 ::fclose( f );
98 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
99 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
100 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
102 return xModel;
104 else
106 throw Exception( "### Cannot read file!" );
110 void exportToFile(
111 char const * fname,
112 Reference< container::XNameContainer > const & xModel,
113 Reference< XComponentContext > const & xContext )
115 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
116 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
118 Sequence< sal_Int8 > bytes;
119 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
120 for (;;)
122 Sequence< sal_Int8 > readBytes;
123 nRead = xStream->readBytes( readBytes, 1024 );
124 if (! nRead)
125 break;
126 OSL_ASSERT( readBytes.getLength() >= nRead );
128 sal_Int32 nPos = bytes.getLength();
129 bytes.realloc( nPos + nRead );
130 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
133 FILE * f = ::fopen( fname, "w" );
134 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
135 ::fclose( f );
138 class MyApp : public Application
140 public:
141 void Main();
144 MyApp aMyApp;
146 void MyApp::Main()
148 if (GetCommandLineParamCount() < 2)
150 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]" );
151 return;
154 Reference< XComponentContext > xContext(
155 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
156 Reference< lang::XMultiServiceFactory > xMSF(
157 xContext->getServiceManager(), UNO_QUERY );
161 ::comphelper::setProcessServiceFactory( xMSF );
163 Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
165 // import dialogs
166 OString aParam1( OUStringToOString(
167 OUString( GetCommandLineParam( 1 ) ),
168 RTL_TEXTENCODING_ASCII_US ) );
169 Reference< container::XNameContainer > xModel(
170 importFile( aParam1.getStr(), xContext ) );
171 OSL_ASSERT( xModel.is() );
173 Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );
174 xDlg->setModel( xModel );
175 xDlg->createPeer( xToolkit, 0 );
176 xDlg->execute();
178 if (GetCommandLineParamCount() == 3)
180 // write modified dialogs
181 OString aParam2( OUStringToOString(
182 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
183 exportToFile( aParam2.getStr(), xModel, xContext );
186 catch (const xml::sax::SAXException & rExc)
188 OUString aStr( rExc );
189 uno::Exception exc;
190 if (rExc.WrappedException >>= exc)
192 aStr += " >>> ";
193 aStr += exc;
195 SAL_WARN( "xmlscript", aStr );
197 catch (const uno::Exception & rExc)
199 SAL_WARN( "xmlscript", rExc );
202 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
203 if (xComp.is())
205 xComp->dispose();
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */