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