fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / xmlscript / test / imexp.cxx
blob7621b5dece151434b974633bb6628b9ab0a39b38
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 ::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 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
77 OSL_FAIL( aStr.getStr() );
80 return xContext;
83 Reference< container::XNameContainer > importFile(
84 char const * fname,
85 Reference< XComponentContext > const & xContext )
87 // create the input stream
88 FILE *f = ::fopen( fname, "rb" );
89 if (f)
91 ::fseek( f, 0 ,SEEK_END );
92 int nLength = ::ftell( f );
93 ::fseek( f, 0, SEEK_SET );
95 ByteSequence bytes( nLength );
96 ::fread( bytes.getArray(), nLength, 1, f );
97 ::fclose( f );
99 Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
100 "com.sun.star.awt.UnoControlDialogModel", xContext ), UNO_QUERY );
101 ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
103 return xModel;
105 else
107 throw Exception( "### Cannot read file!" );
111 void exportToFile(
112 char const * fname,
113 Reference< container::XNameContainer > const & xModel,
114 Reference< XComponentContext > const & xContext )
116 Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
117 Reference< io::XInputStream > xStream( xProvider->createInputStream() );
119 Sequence< sal_Int8 > bytes;
120 sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
121 for (;;)
123 Sequence< sal_Int8 > readBytes;
124 nRead = xStream->readBytes( readBytes, 1024 );
125 if (! nRead)
126 break;
127 OSL_ASSERT( readBytes.getLength() >= nRead );
129 sal_Int32 nPos = bytes.getLength();
130 bytes.realloc( nPos + nRead );
131 memcpy( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
134 FILE * f = ::fopen( fname, "w" );
135 ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
136 ::fflush( f );
137 ::fclose( f );
140 class MyApp : public Application
142 public:
143 void Main();
146 MyApp aMyApp;
148 void MyApp::Main()
150 if (GetCommandLineParamCount() < 2)
152 OSL_FAIL( "usage: imexp inst_dir inputfile [outputfile]\n" );
153 return;
156 Reference< XComponentContext > xContext(
157 createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
158 Reference< lang::XMultiServiceFactory > xMSF(
159 xContext->getServiceManager(), UNO_QUERY );
163 ::comphelper::setProcessServiceFactory( xMSF );
165 Reference< awt::XToolkit> xToolkit = awt::Toolkit::create( xContext );
167 // import dialogs
168 OString aParam1( OUStringToOString(
169 OUString( GetCommandLineParam( 1 ) ),
170 RTL_TEXTENCODING_ASCII_US ) );
171 Reference< container::XNameContainer > xModel(
172 importFile( aParam1.getStr(), xContext ) );
173 OSL_ASSERT( xModel.is() );
175 Reference< awt::XUnoControlDialog > xDlg = UnoControlDialog::create( xContext );;
176 xDlg->setModel( xModel );
177 xDlg->createPeer( xToolkit, 0 );
178 xDlg->execute();
180 if (GetCommandLineParamCount() == 3)
182 // write modified dialogs
183 OString aParam2( OUStringToOString(
184 OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
185 exportToFile( aParam2.getStr(), xModel, xContext );
188 catch (const xml::sax::SAXException & rExc)
190 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
191 uno::Exception exc;
192 if (rExc.WrappedException >>= exc)
194 aStr += OString( " >>> " );
195 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
197 OSL_FAIL( aStr.getStr() );
199 catch (const uno::Exception & rExc)
201 OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
202 OSL_FAIL( aStr.getStr() );
205 Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
206 if (xComp.is())
208 xComp->dispose();
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */