Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / xmlscript / test / imexp.cxx
blob0e4a80b516d5668d4fed42e38b8edc106be10b3a
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>
33 #include <comphelper/processfactory.hxx>
35 #include <vcl/svapp.hxx>
37 #include <com/sun/star/awt/UnoControlDialog.hpp>
38 #include <com/sun/star/awt/Toolkit.hpp>
39 #include <com/sun/star/awt/XToolkit.hpp>
40 #include <com/sun/star/awt/XControlModel.hpp>
41 #include <com/sun/star/awt/XControl.hpp>
42 #include <com/sun/star/awt/XDialog.hpp>
43 #include <com/sun/star/container/XNameContainer.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <com/sun/star/io/XActiveDataSource.hpp>
48 #include <com/sun/star/registry/XSimpleRegistry.hpp>
49 #include <com/sun/star/registry/XImplementationRegistration.hpp>
50 #include <com/sun/star/uno/XComponentContext.hpp>
52 using namespace ::cppu;
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::uno;
56 Reference< XComponentContext > createInitialComponentContext(
57 OUString const & inst_dir )
59 Reference< XComponentContext > xContext;
61 try
63 OUString file_url;
64 oslFileError rc = osl_getFileURLFromSystemPath(
65 inst_dir.pData, &file_url.pData );
66 OSL_ASSERT( osl_File_E_None == rc );
68 OUString unorc = file_url + ("/" LIBO_ETC_FOLDER "/" SAL_CONFIGFILE("louno") );
70 return defaultBootstrap_InitialComponentContext( unorc );
73 catch( const Exception& rExc )
75 SAL_WARN( "xmlscript", rExc );
78 return xContext;
81 Reference< container::XNameContainer > importFile(
82 char const * fname,
83 Reference< XComponentContext > const & xContext )
85 // create the input stream
86 FILE *f = ::fopen( fname, "rb" );
87 if (f)
89 ::fseek( f, 0 ,SEEK_END );
90 int nLength = ::ftell( f );
91 ::fseek( f, 0, SEEK_SET );
93 ByteSequence bytes( nLength );
94 ::fread( bytes.getArray(), nLength, 1, f );
95 ::fclose( f );
97 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
98 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
99 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
101 return xModel;
103 else
105 throw Exception( "### Cannot read file!" );
109 void exportToFile(
110 char const * fname,
111 Reference< container::XNameContainer > const & xModel,
112 Reference< XComponentContext > const & xContext )
114 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
115 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
117 Sequence< sal_Int8 > bytes;
118 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
119 for (;;)
121 Sequence< sal_Int8 > readBytes;
122 nRead = xStream->readBytes( readBytes, 1024 );
123 if (! nRead)
124 break;
125 OSL_ASSERT( readBytes.getLength() >= nRead );
127 sal_Int32 nPos = bytes.getLength();
128 bytes.realloc( nPos + nRead );
129 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
132 FILE * f = ::fopen( fname, "w" );
133 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
134 ::fclose( f );
137 class MyApp : public Application
139 public:
140 void Main();
143 MyApp aMyApp;
145 void MyApp::Main()
147 if (GetCommandLineParamCount() < 2)
149 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]" );
150 return;
153 Reference< XComponentContext > xContext(
154 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
155 Reference< lang::XMultiServiceFactory > xMSF(
156 xContext->getServiceManager(), UNO_QUERY );
160 ::comphelper::setProcessServiceFactory( xMSF );
162 Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
164 // import dialogs
165 OString aParam1( OUStringToOString(
166 OUString( GetCommandLineParam( 1 ) ),
167 RTL_TEXTENCODING_ASCII_US ) );
168 Reference< container::XNameContainer > xModel(
169 importFile( aParam1.getStr(), xContext ) );
170 OSL_ASSERT( xModel.is() );
172 Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );
173 xDlg->setModel( xModel );
174 xDlg->createPeer( xToolkit, 0 );
175 xDlg->execute();
177 if (GetCommandLineParamCount() == 3)
179 // write modified dialogs
180 OString aParam2( OUStringToOString(
181 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
182 exportToFile( aParam2.getStr(), xModel, xContext );
185 catch (const xml::sax::SAXException & rExc)
187 OUString aStr( rExc );
188 uno::Exception exc;
189 if (rExc.WrappedException >>= exc)
191 aStr += " >>> ";
192 aStr += exc;
194 SAL_WARN( "xmlscript", aStr );
196 catch (const uno::Exception & rExc)
198 SAL_WARN( "xmlscript", rExc );
201 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
202 if (xComp.is())
204 xComp->dispose();
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */