xref: /AOO42X/main/xmlscript/test/imexp.cxx (revision e1d5bd03a6ea7ac2b26b792c9e2a94e9f347a43b)
1*e1d5bd03SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e1d5bd03SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1d5bd03SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1d5bd03SAndrew Rist  * distributed with this work for additional information
6*e1d5bd03SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1d5bd03SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1d5bd03SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1d5bd03SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*e1d5bd03SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*e1d5bd03SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1d5bd03SAndrew Rist  * software distributed under the License is distributed on an
15*e1d5bd03SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1d5bd03SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1d5bd03SAndrew Rist  * specific language governing permissions and limitations
18*e1d5bd03SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*e1d5bd03SAndrew Rist  *************************************************************/
21*e1d5bd03SAndrew Rist 
22*e1d5bd03SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmlscript.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <rtl/memory.h>
29cdf0e10cSrcweir #include "osl/file.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
34cdf0e10cSrcweir #include <xmlscript/xml_helper.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
37cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
38cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
41cdf0e10cSrcweir #include <comphelper/regpathhelper.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <tools/debug.hxx>
44cdf0e10cSrcweir #include <vcl/svapp.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
49cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
50cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
53cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
54cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #include <com/sun/star/awt/XToolkit.hpp>
57cdf0e10cSrcweir #include <com/sun/star/awt/XControlModel.hpp>
58cdf0e10cSrcweir #include <com/sun/star/awt/XControl.hpp>
59cdf0e10cSrcweir #include <com/sun/star/awt/XDialog.hpp>
60cdf0e10cSrcweir 
61cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir using namespace ::rtl;
65cdf0e10cSrcweir using namespace ::cppu;
66cdf0e10cSrcweir using namespace ::com::sun::star;
67cdf0e10cSrcweir using namespace ::com::sun::star::uno;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 
72cdf0e10cSrcweir Reference< XComponentContext > createInitialComponentContext(
73cdf0e10cSrcweir     OUString const & inst_dir )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     Reference< XComponentContext > xContext;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     try
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         OUString file_url;
80cdf0e10cSrcweir         oslFileError rc = osl_getFileURLFromSystemPath(
81cdf0e10cSrcweir             inst_dir.pData, &file_url.pData );
82cdf0e10cSrcweir         OSL_ASSERT( osl_File_E_None == rc );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         ::rtl::OUString unorc = file_url + OUString(
85cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("/program/" SAL_CONFIGFILE("uno")) );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         return defaultBootstrap_InitialComponentContext( unorc );
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     catch( Exception& rExc )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
93cdf0e10cSrcweir         OSL_ENSURE( 0, aStr.getStr() );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     return xContext;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir // -----------------------------------------------------------------------
101cdf0e10cSrcweir 
102cdf0e10cSrcweir Reference< container::XNameContainer > importFile(
103cdf0e10cSrcweir     char const * fname,
104cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     // create the input stream
107cdf0e10cSrcweir     FILE *f = ::fopen( fname, "rb" );
108cdf0e10cSrcweir     if (f)
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         ::fseek( f, 0 ,SEEK_END );
111cdf0e10cSrcweir         int nLength = ::ftell( f );
112cdf0e10cSrcweir         ::fseek( f, 0, SEEK_SET );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         ByteSequence bytes( nLength );
115cdf0e10cSrcweir         ::fread( bytes.getArray(), nLength, 1, f );
116cdf0e10cSrcweir         ::fclose( f );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
119cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
120cdf0e10cSrcweir         ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         return xModel;
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir     else
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
127cdf0e10cSrcweir                          Reference< XInterface >() );
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir void exportToFile(
132cdf0e10cSrcweir     char const * fname,
133cdf0e10cSrcweir     Reference< container::XNameContainer > const & xModel,
134cdf0e10cSrcweir     Reference< XComponentContext > const & xContext )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
137cdf0e10cSrcweir     Reference< io::XInputStream > xStream( xProvider->createInputStream() );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     Sequence< sal_Int8 > bytes;
140cdf0e10cSrcweir     sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
141cdf0e10cSrcweir     for (;;)
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         Sequence< sal_Int8 > readBytes;
144cdf0e10cSrcweir         nRead = xStream->readBytes( readBytes, 1024 );
145cdf0e10cSrcweir         if (! nRead)
146cdf0e10cSrcweir             break;
147cdf0e10cSrcweir         OSL_ASSERT( readBytes.getLength() >= nRead );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         sal_Int32 nPos = bytes.getLength();
150cdf0e10cSrcweir         bytes.realloc( nPos + nRead );
151cdf0e10cSrcweir         ::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     FILE * f = ::fopen( fname, "w" );
155cdf0e10cSrcweir     ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
156cdf0e10cSrcweir     ::fflush( f );
157cdf0e10cSrcweir     ::fclose( f );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 
162cdf0e10cSrcweir class MyApp : public Application
163cdf0e10cSrcweir {
164cdf0e10cSrcweir public:
165cdf0e10cSrcweir     void Main();
166cdf0e10cSrcweir };
167cdf0e10cSrcweir 
168cdf0e10cSrcweir MyApp aMyApp;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir // -----------------------------------------------------------------------
171cdf0e10cSrcweir 
172cdf0e10cSrcweir void MyApp::Main()
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     if (GetCommandLineParamCount() < 2)
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         OSL_ENSURE( 0, "usage: imexp inst_dir inputfile [outputfile]\n" );
177cdf0e10cSrcweir         return;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     Reference< XComponentContext > xContext(
181cdf0e10cSrcweir         createInitialComponentContext( OUString( GetCommandLineParam( 0 ) ) ) );
182cdf0e10cSrcweir     Reference< lang::XMultiServiceFactory > xMSF(
183cdf0e10cSrcweir         xContext->getServiceManager(), UNO_QUERY );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir     try
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         ::comphelper::setProcessServiceFactory( xMSF );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
190cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );
191cdf0e10cSrcweir 
192cdf0e10cSrcweir         // import dialogs
193cdf0e10cSrcweir         OString aParam1( OUStringToOString(
194cdf0e10cSrcweir                              OUString( GetCommandLineParam( 1 ) ),
195cdf0e10cSrcweir                              RTL_TEXTENCODING_ASCII_US ) );
196cdf0e10cSrcweir         Reference< container::XNameContainer > xModel(
197cdf0e10cSrcweir             importFile( aParam1.getStr(), xContext ) );
198cdf0e10cSrcweir         OSL_ASSERT( xModel.is() );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         Reference< awt::XControl > xDlg( xMSF->createInstance(
201cdf0e10cSrcweir             OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
202cdf0e10cSrcweir         xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
203cdf0e10cSrcweir         xDlg->createPeer( xToolkit, 0 );
204cdf0e10cSrcweir         Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
205cdf0e10cSrcweir         xD->execute();
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         if (GetCommandLineParamCount() == 3)
208cdf0e10cSrcweir         {
209cdf0e10cSrcweir             // write modified dialogs
210cdf0e10cSrcweir             OString aParam2( OUStringToOString(
211cdf0e10cSrcweir                                  OUString( GetCommandLineParam( 2 ) ), RTL_TEXTENCODING_ASCII_US ) );
212cdf0e10cSrcweir             exportToFile( aParam2.getStr(), xModel, xContext );
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir     catch (xml::sax::SAXException & rExc)
216cdf0e10cSrcweir     {
217cdf0e10cSrcweir         OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
218cdf0e10cSrcweir         uno::Exception exc;
219cdf0e10cSrcweir         if (rExc.WrappedException >>= exc)
220cdf0e10cSrcweir         {
221cdf0e10cSrcweir             aStr += OString( " >>> " );
222cdf0e10cSrcweir             aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir         OSL_ENSURE( 0, aStr.getStr() );
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir     catch (uno::Exception & rExc)
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
229cdf0e10cSrcweir         OSL_ENSURE( 0, aStr.getStr() );
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
233cdf0e10cSrcweir     if (xComp.is())
234cdf0e10cSrcweir     {
235cdf0e10cSrcweir         xComp->dispose();
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239