Files
libreoffice/xmlscript/test/imexp.cxx

245 lines
7.6 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2001-02-16 13:14:51 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2001-02-16 13:14:51 +00:00
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
2001-02-16 13:14:51 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2001-02-16 13:14:51 +00:00
*
* This file is part of OpenOffice.org.
2001-02-16 13:14:51 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2001-02-16 13:14:51 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2001-02-16 13:14:51 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2001-02-16 13:14:51 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_xmlscript.hxx"
2001-02-16 13:14:51 +00:00
#include <stdio.h>
2001-03-19 09:16:58 +00:00
#include <rtl/memory.h>
#include "osl/file.h"
2001-02-16 13:14:51 +00:00
2001-09-19 07:46:35 +00:00
#include <rtl/ustrbuf.hxx>
2001-02-16 13:14:51 +00:00
#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>
#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/bootstrap.hxx>
2001-02-16 13:14:51 +00:00
#include <cppuhelper/implbase2.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/regpathhelper.hxx>
#include <tools/debug.hxx>
#include <vcl/svapp.hxx>
#include <com/sun/star/io/XActiveDataSource.hpp>
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
2001-02-16 13:14:51 +00:00
#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/awt/XDialog.hpp>
2001-02-16 13:14:51 +00:00
#include <com/sun/star/container/XNameContainer.hpp>
using namespace ::rtl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
2001-09-19 07:46:35 +00:00
Reference< XComponentContext > createInitialComponentContext(
OUString const & inst_dir )
2001-02-16 13:14:51 +00:00
{
Reference< XComponentContext > xContext;
2001-02-16 13:14:51 +00:00
try
{
OUString file_url;
oslFileError rc = osl_getFileURLFromSystemPath(
inst_dir.pData, &file_url.pData );
OSL_ASSERT( osl_File_E_None == rc );
2001-02-16 13:14:51 +00:00
::rtl::OUString unorc = file_url + OUString(
RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
return defaultBootstrap_InitialComponentContext( unorc );
2001-02-16 13:14:51 +00:00
}
catch( Exception& rExc )
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
OSL_ENSURE( 0, aStr.getStr() );
}
2001-09-19 07:46:35 +00:00
return xContext;
2001-02-16 13:14:51 +00:00
}
// -----------------------------------------------------------------------
2001-03-14 15:41:59 +00:00
Reference< container::XNameContainer > importFile(
2001-09-19 07:46:35 +00:00
char const * fname,
Reference< XComponentContext > const & xContext )
2001-02-16 13:14:51 +00:00
{
2001-02-27 11:45:17 +00:00
// create the input stream
FILE *f = ::fopen( fname, "rb" );
if (f)
2001-02-16 13:14:51 +00:00
{
2001-02-27 11:45:17 +00:00
::fseek( f, 0 ,SEEK_END );
int nLength = ::ftell( f );
::fseek( f, 0, SEEK_SET );
2001-02-16 13:14:51 +00:00
2001-03-14 15:41:59 +00:00
ByteSequence bytes( nLength );
2001-02-27 11:45:17 +00:00
::fread( bytes.getArray(), nLength, 1, f );
::fclose( f );
2001-02-16 13:14:51 +00:00
2001-09-19 07:46:35 +00:00
Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
2001-03-14 15:41:59 +00:00
return xModel;
2001-02-16 13:14:51 +00:00
}
else
{
2001-02-27 11:45:17 +00:00
throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
Reference< XInterface >() );
2001-02-16 13:14:51 +00:00
}
}
2001-02-27 11:45:17 +00:00
void exportToFile(
2001-02-16 13:14:51 +00:00
char const * fname,
2001-09-19 07:46:35 +00:00
Reference< container::XNameContainer > const & xModel,
Reference< XComponentContext > const & xContext )
2001-02-16 13:14:51 +00:00
{
2001-09-19 07:46:35 +00:00
Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
2001-03-14 15:41:59 +00:00
Reference< io::XInputStream > xStream( xProvider->createInputStream() );
2001-02-27 11:45:17 +00:00
Sequence< sal_Int8 > bytes;
2001-03-19 09:08:36 +00:00
sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
for (;;)
2001-03-14 15:41:59 +00:00
{
2001-03-19 09:08:36 +00:00
Sequence< sal_Int8 > readBytes;
nRead = xStream->readBytes( readBytes, 1024 );
if (! nRead)
break;
2001-03-19 09:16:58 +00:00
OSL_ASSERT( readBytes.getLength() >= nRead );
2001-03-19 09:08:36 +00:00
sal_Int32 nPos = bytes.getLength();
bytes.realloc( nPos + nRead );
2001-03-19 09:16:58 +00:00
::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
2001-03-14 15:41:59 +00:00
}
2001-02-16 13:14:51 +00:00
FILE * f = ::fopen( fname, "w" );
2001-02-27 11:45:17 +00:00
::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
2001-02-16 13:14:51 +00:00
::fflush( f );
::fclose( f );
}
class MyApp : public Application
{
public:
2001-03-14 15:41:59 +00:00
void Main();
2001-02-16 13:14:51 +00:00
};
MyApp aMyApp;
// -----------------------------------------------------------------------
void MyApp::Main()
{
if (GetCommandLineParamCount() < 2)
2001-02-16 13:14:51 +00:00
{
OSL_ENSURE( 0, "usage: imexp inst_dir inputfile [outputfile]\n" );
2001-02-16 13:14:51 +00:00
return;
}
Reference< XComponentContext > xContext(
createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
Reference< lang::XMultiServiceFactory > xMSF(
xContext->getServiceManager(), UNO_QUERY );
2001-02-16 13:14:51 +00:00
try
{
::comphelper::setProcessServiceFactory( xMSF );
Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );
2001-02-27 11:45:17 +00:00
// import dialogs
OString aParam1( OUStringToOString(
OUString( GetCommandLineParam( 1 ) ),
RTL_TEXTENCODING_ASCII_US ) );
Reference< container::XNameContainer > xModel(
importFile( aParam1.getStr(), xContext ) );
2001-03-14 15:41:59 +00:00
OSL_ASSERT( xModel.is() );
2001-02-16 13:14:51 +00:00
2001-03-14 15:41:59 +00:00
Reference< awt::XControl > xDlg( xMSF->createInstance(
OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
xDlg->createPeer( xToolkit, 0 );
Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
xD->execute();
2001-02-28 17:22:08 +00:00
if (GetCommandLineParamCount() == 3)
2001-02-28 17:22:08 +00:00
{
// write modified dialogs
OString aParam2( OUStringToOString(
OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
2001-09-19 07:46:35 +00:00
exportToFile( aParam2.getStr(), xModel, xContext );
2001-02-28 17:22:08 +00:00
}
2001-02-16 13:14:51 +00:00
}
2001-03-14 15:41:59 +00:00
catch (xml::sax::SAXException & rExc)
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
uno::Exception exc;
if (rExc.WrappedException >>= exc)
{
aStr += OString( " >>> " );
aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
}
OSL_ENSURE( 0, aStr.getStr() );
}
2001-02-16 13:14:51 +00:00
catch (uno::Exception & rExc)
{
OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
OSL_ENSURE( 0, aStr.getStr() );
}
2001-09-19 07:46:35 +00:00
Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
if (xComp.is())
2001-02-16 13:14:51 +00:00
{
2001-09-19 07:46:35 +00:00
xComp->dispose();
2001-02-16 13:14:51 +00:00
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */