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