xref: /trunk/main/xmlscript/test/imexp.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmlscript.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir #include <rtl/memory.h>
33*cdf0e10cSrcweir #include "osl/file.h"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
38*cdf0e10cSrcweir #include <xmlscript/xml_helper.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
41*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
42*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
45*cdf0e10cSrcweir #include <comphelper/regpathhelper.hxx>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <tools/debug.hxx>
48*cdf0e10cSrcweir #include <vcl/svapp.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #include <com/sun/star/awt/XToolkit.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/awt/XControlModel.hpp>
62*cdf0e10cSrcweir #include <com/sun/star/awt/XControl.hpp>
63*cdf0e10cSrcweir #include <com/sun/star/awt/XDialog.hpp>
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir using namespace ::rtl;
69*cdf0e10cSrcweir using namespace ::cppu;
70*cdf0e10cSrcweir using namespace ::com::sun::star;
71*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir Reference< XComponentContext > createInitialComponentContext(
77*cdf0e10cSrcweir     OUString const & inst_dir )
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir     Reference< XComponentContext > xContext;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	try
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir         OUString file_url;
84*cdf0e10cSrcweir         oslFileError rc = osl_getFileURLFromSystemPath(
85*cdf0e10cSrcweir             inst_dir.pData, &file_url.pData );
86*cdf0e10cSrcweir         OSL_ASSERT( osl_File_E_None == rc );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         ::rtl::OUString unorc = file_url + OUString(
89*cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         return defaultBootstrap_InitialComponentContext( unorc );
92*cdf0e10cSrcweir     }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	catch( Exception& rExc )
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
97*cdf0e10cSrcweir 		OSL_ENSURE( 0, aStr.getStr() );
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	return xContext;
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir // -----------------------------------------------------------------------
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir Reference< container::XNameContainer > importFile(
107*cdf0e10cSrcweir 	char const * fname,
108*cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	// create the input stream
111*cdf0e10cSrcweir 	FILE *f = ::fopen( fname, "rb" );
112*cdf0e10cSrcweir 	if (f)
113*cdf0e10cSrcweir 	{
114*cdf0e10cSrcweir 		::fseek( f, 0 ,SEEK_END );
115*cdf0e10cSrcweir 		int nLength = ::ftell( f );
116*cdf0e10cSrcweir 		::fseek( f, 0, SEEK_SET );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		ByteSequence bytes( nLength );
119*cdf0e10cSrcweir 		::fread( bytes.getArray(), nLength, 1, f );
120*cdf0e10cSrcweir 		::fclose( f );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 		Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
123*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
124*cdf0e10cSrcweir 		::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		return xModel;
127*cdf0e10cSrcweir 	}
128*cdf0e10cSrcweir 	else
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
131*cdf0e10cSrcweir 						 Reference< XInterface >() );
132*cdf0e10cSrcweir 	}
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir void exportToFile(
136*cdf0e10cSrcweir 	char const * fname,
137*cdf0e10cSrcweir 	Reference< container::XNameContainer > const & xModel,
138*cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir 	Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
141*cdf0e10cSrcweir 	Reference< io::XInputStream > xStream( xProvider->createInputStream() );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	Sequence< sal_Int8 > bytes;
144*cdf0e10cSrcweir 	sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
145*cdf0e10cSrcweir 	for (;;)
146*cdf0e10cSrcweir 	{
147*cdf0e10cSrcweir 		Sequence< sal_Int8 > readBytes;
148*cdf0e10cSrcweir 		nRead = xStream->readBytes( readBytes, 1024 );
149*cdf0e10cSrcweir 		if (! nRead)
150*cdf0e10cSrcweir 			break;
151*cdf0e10cSrcweir 		OSL_ASSERT( readBytes.getLength() >= nRead );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		sal_Int32 nPos = bytes.getLength();
154*cdf0e10cSrcweir 		bytes.realloc( nPos + nRead );
155*cdf0e10cSrcweir 		::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
156*cdf0e10cSrcweir 	}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	FILE * f = ::fopen( fname, "w" );
159*cdf0e10cSrcweir 	::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
160*cdf0e10cSrcweir 	::fflush( f );
161*cdf0e10cSrcweir 	::fclose( f );
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir class MyApp : public Application
167*cdf0e10cSrcweir {
168*cdf0e10cSrcweir public:
169*cdf0e10cSrcweir     void Main();
170*cdf0e10cSrcweir };
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir MyApp aMyApp;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir // -----------------------------------------------------------------------
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir void MyApp::Main()
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir 	if (GetCommandLineParamCount() < 2)
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 		OSL_ENSURE( 0, "usage: imexp inst_dir inputfile [outputfile]\n" );
181*cdf0e10cSrcweir 		return;
182*cdf0e10cSrcweir 	}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     Reference< XComponentContext > xContext(
185*cdf0e10cSrcweir         createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
186*cdf0e10cSrcweir 	Reference< lang::XMultiServiceFactory > xMSF(
187*cdf0e10cSrcweir         xContext->getServiceManager(), UNO_QUERY );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	try
190*cdf0e10cSrcweir 	{
191*cdf0e10cSrcweir 		::comphelper::setProcessServiceFactory( xMSF );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
194*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		// import dialogs
197*cdf0e10cSrcweir 		OString aParam1( OUStringToOString(
198*cdf0e10cSrcweir                              OUString( GetCommandLineParam( 1 ) ),
199*cdf0e10cSrcweir                              RTL_TEXTENCODING_ASCII_US ) );
200*cdf0e10cSrcweir 		Reference< container::XNameContainer > xModel(
201*cdf0e10cSrcweir             importFile( aParam1.getStr(), xContext ) );
202*cdf0e10cSrcweir 		OSL_ASSERT( xModel.is() );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		Reference< awt::XControl > xDlg( xMSF->createInstance(
205*cdf0e10cSrcweir 			OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
206*cdf0e10cSrcweir 		xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
207*cdf0e10cSrcweir 		xDlg->createPeer( xToolkit, 0 );
208*cdf0e10cSrcweir 		Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
209*cdf0e10cSrcweir 		xD->execute();
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		if (GetCommandLineParamCount() == 3)
212*cdf0e10cSrcweir 		{
213*cdf0e10cSrcweir 			// write modified dialogs
214*cdf0e10cSrcweir 			OString aParam2( OUStringToOString(
215*cdf0e10cSrcweir                                  OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
216*cdf0e10cSrcweir 			exportToFile( aParam2.getStr(), xModel, xContext );
217*cdf0e10cSrcweir 		}
218*cdf0e10cSrcweir 	}
219*cdf0e10cSrcweir 	catch (xml::sax::SAXException & rExc)
220*cdf0e10cSrcweir 	{
221*cdf0e10cSrcweir 		OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
222*cdf0e10cSrcweir 		uno::Exception exc;
223*cdf0e10cSrcweir 		if (rExc.WrappedException >>= exc)
224*cdf0e10cSrcweir 		{
225*cdf0e10cSrcweir 			aStr += OString( " >>> " );
226*cdf0e10cSrcweir 			aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
227*cdf0e10cSrcweir 		}
228*cdf0e10cSrcweir 		OSL_ENSURE( 0, aStr.getStr() );
229*cdf0e10cSrcweir 	}
230*cdf0e10cSrcweir 	catch (uno::Exception & rExc)
231*cdf0e10cSrcweir 	{
232*cdf0e10cSrcweir 		OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
233*cdf0e10cSrcweir 		OSL_ENSURE( 0, aStr.getStr() );
234*cdf0e10cSrcweir 	}
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir     Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
237*cdf0e10cSrcweir     if (xComp.is())
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         xComp->dispose();
240*cdf0e10cSrcweir     }
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243