bump product version to 4.1.6.2
[LibreOffice.git] / xmlscript / test / imexp.cxx
blobde88975f119198eb6e224c7a21ae34c4bb6ab1f6
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/awt/Toolkit.hpp>
38 #include <com/sun/star/io/XActiveDataSource.hpp>
40 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/lang/XInitialization.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/registry/XSimpleRegistry.hpp>
45 #include <com/sun/star/registry/XImplementationRegistration.hpp>
46 #include <com/sun/star/uno/XComponentContext.hpp>
48 #include <com/sun/star/awt/XToolkit.hpp>
49 #include <com/sun/star/awt/XControlModel.hpp>
50 #include <com/sun/star/awt/XControl.hpp>
51 #include <com/sun/star/awt/XDialog.hpp>
53 #include <com/sun/star/container/XNameContainer.hpp>
56 using namespace ::rtl;
57 using namespace ::cppu;
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
64 Reference< XComponentContext > createInitialComponentContext(
65 OUString const & inst_dir )
67 Reference< XComponentContext > xContext;
69 try
71 OUString file_url;
72 oslFileError rc = osl_getFileURLFromSystemPath(
73 inst_dir.pData, &file_url.pData );
74 OSL_ASSERT( osl_File_E_None == rc );
76 OUString unorc = file_url + OUString("/program/" SAL_CONFIGFILE("uno") );
78 return defaultBootstrap_InitialComponentContext( unorc );
81 catch( const Exception& rExc )
83 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
84 OSL_FAIL( aStr.getStr() );
87 return xContext;
91 // -----------------------------------------------------------------------
93 Reference< container::XNameContainer > importFile(
94 char const * fname,
95 Reference< XComponentContext > const & xContext )
97 // create the input stream
98 FILE *f = ::fopen( fname, "rb" );
99 if (f)
101 ::fseek( f, 0 ,SEEK_END );
102 int nLength = ::ftell( f );
103 ::fseek( f, 0, SEEK_SET );
105 ByteSequence bytes( nLength );
106 ::fread( bytes.getArray(), nLength, 1, f );
107 ::fclose( f );
109 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
110 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
111 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
113 return xModel;
115 else
117 throw Exception( "### Cannot read file!", Reference< XInterface >() );
121 void exportToFile(
122 char const * fname,
123 Reference< container::XNameContainer > const & xModel,
124 Reference< XComponentContext > const & xContext )
126 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
127 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
129 Sequence< sal_Int8 > bytes;
130 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
131 for (;;)
133 Sequence< sal_Int8 > readBytes;
134 nRead = xStream->readBytes( readBytes, 1024 );
135 if (! nRead)
136 break;
137 OSL_ASSERT( readBytes.getLength() >= nRead );
139 sal_Int32 nPos = bytes.getLength();
140 bytes.realloc( nPos + nRead );
141 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
144 FILE * f = ::fopen( fname, "w" );
145 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
146 ::fflush( f );
147 ::fclose( f );
152 class MyApp : public Application
154 public:
155 void Main();
158 MyApp aMyApp;
160 // -----------------------------------------------------------------------
162 void MyApp::Main()
164 if (GetCommandLineParamCount() < 2)
166 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
167 return;
170 Reference< XComponentContext > xContext(
171 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
172 Reference< lang::XMultiServiceFactory > xMSF(
173 xContext->getServiceManager(), UNO_QUERY );
177 ::comphelper::setProcessServiceFactory( xMSF );
179 Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
181 // import dialogs
182 OString aParam1( OUStringToOString(
183 OUString( GetCommandLineParam( 1 ) ),
184 RTL_TEXTENCODING_ASCII_US ) );
185 Reference< container::XNameContainer > xModel(
186 importFile( aParam1.getStr(), xContext ) );
187 OSL_ASSERT( xModel.is() );
189 Reference< awt::XControl > xDlg( xMSF->createInstance( "com.sun.star.awt.UnoControlDialog" ), UNO_QUERY );
190 xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
191 xDlg->createPeer( xToolkit, 0 );
192 Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
193 xD->execute();
195 if (GetCommandLineParamCount() == 3)
197 // write modified dialogs
198 OString aParam2( OUStringToOString(
199 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
200 exportToFile( aParam2.getStr(), xModel, xContext );
203 catch (const xml::sax::SAXException & rExc)
205 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
206 uno::Exception exc;
207 if (rExc.WrappedException >>= exc)
209 aStr += OString( " >>> " );
210 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
212 OSL_FAIL( aStr.getStr() );
214 catch (const uno::Exception & rExc)
216 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
217 OSL_FAIL( aStr.getStr() );
220 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
221 if (xComp.is())
223 xComp->dispose();
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */