2001-02-16 13:14:51 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* OpenOffice.org - a multi-platform office productivity suite
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* $RCSfile: imexp.cxx,v $
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* $Revision: 1.18 $
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* last change: $Author: rt $ $Date: 2005-09-09 16:18:22 $
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* The Contents of this file are made available subject to
|
|
|
|
* the terms of GNU Lesser General Public License Version 2.1.
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2005 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* This library 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 for more details.
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
2005-09-09 15:18:22 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
2001-02-16 13:14:51 +00:00
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2001-03-19 09:16:58 +00:00
|
|
|
#include <rtl/memory.h>
|
2003-09-04 08:20:37 +00:00
|
|
|
#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>
|
2001-05-10 08:20:42 +00:00
|
|
|
#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>
|
2001-05-10 08:20:42 +00:00
|
|
|
#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>
|
2003-04-23 15:54:52 +00:00
|
|
|
#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
|
|
|
|
2003-09-04 08:20:37 +00:00
|
|
|
Reference< XComponentContext > createInitialComponentContext(
|
|
|
|
OUString const & inst_dir )
|
2001-02-16 13:14:51 +00:00
|
|
|
{
|
2001-05-10 08:20:42 +00:00
|
|
|
Reference< XComponentContext > xContext;
|
2001-02-16 13:14:51 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2003-09-04 08:20:37 +00:00
|
|
|
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
|
|
|
|
2003-09-04 08:20:37 +00:00
|
|
|
::rtl::OUString unorc = file_url + OUString(
|
|
|
|
RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
|
2001-05-10 08:20:42 +00:00
|
|
|
|
2003-09-04 08:20:37 +00:00
|
|
|
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()
|
|
|
|
{
|
2003-09-04 08:20:37 +00:00
|
|
|
if (GetCommandLineParamCount() < 2)
|
2001-02-16 13:14:51 +00:00
|
|
|
{
|
2003-09-04 08:20:37 +00:00
|
|
|
OSL_ENSURE( 0, "usage: imexp inst_dir inputfile [outputfile]\n" );
|
2001-02-16 13:14:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-09-04 08:20:37 +00:00
|
|
|
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
|
2003-09-04 08:20:37 +00:00
|
|
|
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
|
|
|
|
2003-09-04 08:20:37 +00:00
|
|
|
if (GetCommandLineParamCount() == 3)
|
2001-02-28 17:22:08 +00:00
|
|
|
{
|
|
|
|
// write modified dialogs
|
2003-09-04 08:20:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|