Stop leaking all ScPostIt instances.
[LibreOffice.git] / xmlscript / test / imexp.cxx
blob41defc66b4d918c6da021af3e0dcb534d911fd71
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>
27 #include <xmlscript/xmldlg_imexp.hxx>
28 #include <xmlscript/xml_helper.hxx>
30 #include <cppuhelper/servicefactory.hxx>
31 #include <cppuhelper/bootstrap.hxx>
32 #include <cppuhelper/implbase2.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 ::rtl;
54 using namespace ::cppu;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
58 Reference< XComponentContext > createInitialComponentContext(
59 OUString const & inst_dir )
61 Reference< XComponentContext > xContext;
63 try
65 OUString file_url;
66 oslFileError rc = osl_getFileURLFromSystemPath(
67 inst_dir.pData, &file_url.pData );
68 OSL_ASSERT( osl_File_E_None == rc );
70 OUString unorc = file_url + OUString("/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("uno") );
72 return defaultBootstrap_InitialComponentContext( unorc );
75 catch( const Exception& rExc )
77 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
78 OSL_FAIL( aStr.getStr() );
81 return xContext;
84 Reference< container::XNameContainer > importFile(
85 char const * fname,
86 Reference< XComponentContext > const & xContext )
88 // create the input stream
89 FILE *f = ::fopen( fname, "rb" );
90 if (f)
92 ::fseek( f, 0 ,SEEK_END );
93 int nLength = ::ftell( f );
94 ::fseek( f, 0, SEEK_SET );
96 ByteSequence bytes( nLength );
97 ::fread( bytes.getArray(), nLength, 1, f );
98 ::fclose( f );
100 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
101 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
102 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
104 return xModel;
106 else
108 throw Exception( "### Cannot read file!", Reference< XInterface >() );
112 void exportToFile(
113 char const * fname,
114 Reference< container::XNameContainer > const & xModel,
115 Reference< XComponentContext > const & xContext )
117 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
118 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
120 Sequence< sal_Int8 > bytes;
121 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
122 for (;;)
124 Sequence< sal_Int8 > readBytes;
125 nRead = xStream->readBytes( readBytes, 1024 );
126 if (! nRead)
127 break;
128 OSL_ASSERT( readBytes.getLength() >= nRead );
130 sal_Int32 nPos = bytes.getLength();
131 bytes.realloc( nPos + nRead );
132 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
135 FILE * f = ::fopen( fname, "w" );
136 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
137 ::fflush( f );
138 ::fclose( f );
141 class MyApp : public Application
143 public:
144 void Main();
147 MyApp aMyApp;
149 void MyApp::Main()
151 if (GetCommandLineParamCount() < 2)
153 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
154 return;
157 Reference< XComponentContext > xContext(
158 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
159 Reference< lang::XMultiServiceFactory > xMSF(
160 xContext->getServiceManager(), UNO_QUERY );
164 ::comphelper::setProcessServiceFactory( xMSF );
166 Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
168 // import dialogs
169 OString aParam1( OUStringToOString(
170 OUString( GetCommandLineParam( 1 ) ),
171 RTL_TEXTENCODING_ASCII_US ) );
172 Reference< container::XNameContainer > xModel(
173 importFile( aParam1.getStr(), xContext ) );
174 OSL_ASSERT( xModel.is() );
176 Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );;
177 xDlg->setModel( xModel );
178 xDlg->createPeer( xToolkit, 0 );
179 xDlg->execute();
181 if (GetCommandLineParamCount() == 3)
183 // write modified dialogs
184 OString aParam2( OUStringToOString(
185 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
186 exportToFile( aParam2.getStr(), xModel, xContext );
189 catch (const xml::sax::SAXException & rExc)
191 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
192 uno::Exception exc;
193 if (rExc.WrappedException >>= exc)
195 aStr += OString( " >>> " );
196 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
198 OSL_FAIL( aStr.getStr() );
200 catch (const uno::Exception & rExc)
202 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
203 OSL_FAIL( aStr.getStr() );
206 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
207 if (xComp.is())
209 xComp->dispose();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */