LCOV - code coverage report
Current view: top level - oox/source/ole - vbamodule.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 101 128 78.9 %
Date: 2015-06-13 12:38:46 Functions: 8 9 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "oox/ole/vbamodule.hxx"
      21             : #include <com/sun/star/container/XNameContainer.hpp>
      22             : #include <com/sun/star/container/XIndexContainer.hpp>
      23             : #include <com/sun/star/script/ModuleInfo.hpp>
      24             : #include <com/sun/star/script/ModuleType.hpp>
      25             : #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
      26             : #include <com/sun/star/awt/KeyEvent.hpp>
      27             : #include <osl/diagnose.h>
      28             : #include <cppuhelper/implbase1.hxx>
      29             : #include <filter/msfilter/msvbahelper.hxx>
      30             : #include "oox/helper/binaryinputstream.hxx"
      31             : #include "oox/helper/storagebase.hxx"
      32             : #include "oox/helper/textinputstream.hxx"
      33             : #include "oox/ole/vbahelper.hxx"
      34             : #include "oox/ole/vbainputstream.hxx"
      35             : 
      36             : namespace oox {
      37             : namespace ole {
      38             : 
      39             : using namespace ::com::sun::star::lang;
      40             : using namespace ::com::sun::star::script::vba;
      41             : using namespace ::com::sun::star::uno;
      42             : using namespace ::com::sun::star;
      43             : 
      44             : using ::com::sun::star::awt::KeyEvent;
      45             : 
      46         160 : VbaModule::VbaModule( const Reference< XComponentContext >& rxContext,
      47             :                       const Reference< frame::XModel >& rxDocModel,
      48             :                       const OUString& rName, rtl_TextEncoding eTextEnc, bool bExecutable ) :
      49             :     mxContext( rxContext ),
      50             :     mxDocModel( rxDocModel ),
      51             :     maName( rName ),
      52             :     meTextEnc( eTextEnc ),
      53             :     mnType( script::ModuleType::UNKNOWN ),
      54             :     mnOffset( SAL_MAX_UINT32 ),
      55             :     mbReadOnly( false ),
      56             :     mbPrivate( false ),
      57         160 :     mbExecutable( bExecutable )
      58             : {
      59         160 : }
      60             : 
      61         160 : void VbaModule::importDirRecords( BinaryInputStream& rDirStrm )
      62             : {
      63         160 :     sal_uInt16 nRecId = 0;
      64         160 :     StreamDataSequence aRecData;
      65        1752 :     while( VbaHelper::readDirRecord( nRecId, aRecData, rDirStrm ) && (nRecId != VBA_ID_MODULEEND) )
      66             :     {
      67        1432 :         SequenceInputStream aRecStrm( aRecData );
      68        1432 :         sal_Int32 nRecSize = aRecData.getLength();
      69        1432 :         switch( nRecId )
      70             :         {
      71             : #define OOX_ENSURE_RECORDSIZE( cond ) OSL_ENSURE( cond, "VbaModule::importDirRecords - invalid record size" )
      72             :             case VBA_ID_MODULENAME:
      73             :                 OSL_FAIL( "VbaModule::importDirRecords - unexpected MODULENAME record" );
      74           0 :                 maName = aRecStrm.readCharArrayUC( nRecSize, meTextEnc );
      75           0 :             break;
      76             :             case VBA_ID_MODULENAMEUNICODE:
      77         150 :             break;
      78             :             case VBA_ID_MODULESTREAMNAME:
      79         160 :                 maStreamName = aRecStrm.readCharArrayUC( nRecSize, meTextEnc );
      80             :                 // Actually the stream name seems the best name to use
      81             :                 // the VBA_ID_MODULENAME name can sometimes be the wrong case
      82         160 :                 maName = maStreamName;
      83         160 :             break;
      84             :             case VBA_ID_MODULESTREAMNAMEUNICODE:
      85         160 :             break;
      86             :             case VBA_ID_MODULEDOCSTRING:
      87         160 :                 maDocString = aRecStrm.readCharArrayUC( nRecSize, meTextEnc );
      88         160 :             break;
      89             :             case VBA_ID_MODULEDOCSTRINGUNICODE:
      90         160 :             break;
      91             :             case VBA_ID_MODULEOFFSET:
      92             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 4 );
      93         160 :                 mnOffset = aRecStrm.readuInt32();
      94         160 :             break;
      95             :             case VBA_ID_MODULEHELPCONTEXT:
      96             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 4 );
      97         160 :             break;
      98             :             case VBA_ID_MODULECOOKIE:
      99             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 2 );
     100         160 :             break;
     101             :             case VBA_ID_MODULETYPEPROCEDURAL:
     102             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 0 );
     103             :                 OSL_ENSURE( mnType == script::ModuleType::UNKNOWN, "VbaModule::importDirRecords - multiple module type records" );
     104          30 :                 mnType = script::ModuleType::NORMAL;
     105          30 :             break;
     106             :             case VBA_ID_MODULETYPEDOCUMENT:
     107             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 0 );
     108             :                 OSL_ENSURE( mnType == script::ModuleType::UNKNOWN, "VbaModule::importDirRecords - multiple module type records" );
     109         130 :                 mnType = script::ModuleType::DOCUMENT;
     110         130 :             break;
     111             :             case VBA_ID_MODULEREADONLY:
     112             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 0 );
     113           0 :                 mbReadOnly = true;
     114           0 :             break;
     115             :             case VBA_ID_MODULEPRIVATE:
     116             :                 OOX_ENSURE_RECORDSIZE( nRecSize == 0 );
     117           2 :                 mbPrivate = true;
     118           2 :             break;
     119             :             default:
     120             :                 OSL_FAIL( "VbaModule::importDirRecords - unknown module record" );
     121             : #undef OOX_ENSURE_RECORDSIZE
     122             :         }
     123        1432 :     }
     124             :     OSL_ENSURE( !maName.isEmpty(), "VbaModule::importDirRecords - missing module name" );
     125             :     OSL_ENSURE( !maStreamName.isEmpty(), "VbaModule::importDirRecords - missing module stream name" );
     126             :     OSL_ENSURE( mnType != script::ModuleType::UNKNOWN, "VbaModule::importDirRecords - missing module type" );
     127         160 :     OSL_ENSURE( mnOffset < SAL_MAX_UINT32, "VbaModule::importDirRecords - missing module stream offset" );
     128         160 : }
     129             : 
     130         160 : void VbaModule::createAndImportModule( StorageBase& rVbaStrg,
     131             :                                        const Reference< container::XNameContainer >& rxBasicLib,
     132             :                                        const Reference< container::XNameAccess >& rxDocObjectNA ) const
     133             : {
     134         160 :     OUString aVBASourceCode = readSourceCode( rVbaStrg );
     135         160 :     createModule( aVBASourceCode, rxBasicLib, rxDocObjectNA );
     136         160 : }
     137             : 
     138           0 : void VbaModule::createEmptyModule( const Reference< container::XNameContainer >& rxBasicLib,
     139             :                                    const Reference< container::XNameAccess >& rxDocObjectNA ) const
     140             : {
     141           0 :     createModule( OUString(), rxBasicLib, rxDocObjectNA );
     142           0 : }
     143             : 
     144         160 : OUString VbaModule::readSourceCode( StorageBase& rVbaStrg ) const
     145             : {
     146         160 :     OUStringBuffer aSourceCode;
     147             :     static const char sUnmatchedRemovedTag[] = "Rem removed unmatched Sub/End: ";
     148         160 :     if( !maStreamName.isEmpty() && (mnOffset != SAL_MAX_UINT32) )
     149             :     {
     150         160 :         BinaryXInputStream aInStrm( rVbaStrg.openInputStream( maStreamName ), true );
     151             :         OSL_ENSURE( !aInStrm.isEof(), "VbaModule::readSourceCode - cannot open module stream" );
     152             :         // skip the 'performance cache' stored before the actual source code
     153         160 :         aInStrm.seek( mnOffset );
     154             :         // if stream is still valid, load the source code
     155         160 :         if( !aInStrm.isEof() )
     156             :         {
     157             :             // decompression starts at current stream position of aInStrm
     158         160 :             VbaInputStream aVbaStrm( aInStrm );
     159             :             // load the source code line-by-line, with some more processing
     160         320 :             TextInputStream aVbaTextStrm( mxContext, aVbaStrm, meTextEnc );
     161             : 
     162             :             struct ProcedurePair
     163             :             {
     164             :                 bool bInProcedure;
     165             :                 sal_uInt32 nPos;
     166         160 :                 ProcedurePair() : bInProcedure( false ), nPos( 0 ) {};
     167         160 :             } procInfo;
     168             : 
     169        7381 :             while( !aVbaTextStrm.isEof() )
     170             :             {
     171        7061 :                 OUString aCodeLine = aVbaTextStrm.readLine();
     172        7061 :                 if( aCodeLine.match( "Attribute " ) )
     173             :                 {
     174             :                     // attribute
     175        1091 :                     int index = aCodeLine.indexOf( ".VB_ProcData.VB_Invoke_Func = " );
     176        1091 :                     if ( index != -1 )
     177             :                     {
     178             :                         // format is
     179             :                         //    'Attribute Procedure.VB_ProcData.VB_Invoke_Func = "*\n14"'
     180             :                         //    where 'Procedure' is the procedure name and '*' is the shortcut key
     181             :                         // note: his is only relevant for Excel, seems that
     182             :                         // word doesn't store the shortcut in the module
     183             :                         // attributes
     184           1 :                         int nSpaceIndex = aCodeLine.indexOf(' ');
     185           1 :                         OUString sProc = aCodeLine.copy( nSpaceIndex + 1, index - nSpaceIndex - 1);
     186             :                         // for Excel short cut key seems limited to cntrl+'a-z, A-Z'
     187           2 :                         OUString sKey = aCodeLine.copy( aCodeLine.lastIndexOf("= ") + 3, 1 );
     188             :                         // only alpha key valid for key shortcut, however the api will accept other keys
     189           1 :                         if ( !isalpha( (char)sKey[ 0 ] ) )
     190             :                         {
     191             :                             // cntrl modifier is explicit ( but could be cntrl+shift ), parseKeyEvent
     192             :                             // will handle and uppercase letter appropriately
     193           0 :                             OUString sApiKey = "^";
     194           0 :                             sApiKey += sKey;
     195             :                             try
     196             :                             {
     197           0 :                                 KeyEvent aKeyEvent = ooo::vba::parseKeyEvent( sApiKey );
     198           0 :                                 ooo::vba::applyShortCutKeyBinding( mxDocModel, aKeyEvent, sProc );
     199             :                             }
     200           0 :                             catch (const Exception&)
     201             :                             {
     202           0 :                             }
     203           1 :                         }
     204             :                     }
     205             :                 }
     206             :                 else
     207             :                 {
     208             :                     // Hack here to weed out any unmatched End Sub / Sub Foo statements.
     209             :                     // The behaviour of the vba ide practically guarantees the case and
     210             :                     // spacing of Sub statement(s). However, indentation can be arbitrary hence
     211             :                     // the trim.
     212        5970 :                     OUString trimLine( aCodeLine.trim() );
     213       12141 :                     if ( mbExecutable && (
     214       11776 :                       trimLine.match("Sub ")         ||
     215       11611 :                       trimLine.match("Public Sub ")  ||
     216       11574 :                       trimLine.match("Private Sub ") ||
     217        5769 :                       trimLine.match("Static Sub ") ) )
     218             :                     {
     219             :                         // this should never happen, basic doesn't support nested procedures
     220             :                         // first Sub Foo must be bogus
     221         201 :                         if ( procInfo.bInProcedure )
     222             :                         {
     223             :                             // comment out the line
     224           0 :                             aSourceCode.insert( procInfo.nPos, sUnmatchedRemovedTag );
     225             :                             // mark location of this Sub
     226           0 :                             procInfo.nPos = aSourceCode.getLength();
     227             :                         }
     228             :                         else
     229             :                         {
     230         201 :                             procInfo.bInProcedure = true;
     231         201 :                             procInfo.nPos = aSourceCode.getLength();
     232             :                         }
     233             :                     }
     234        5769 :                     else if ( mbExecutable && aCodeLine.trim().match("End Sub") )
     235             :                     {
     236             :                         // un-matched End Sub
     237         201 :                         if ( !procInfo.bInProcedure )
     238             :                         {
     239           0 :                             aSourceCode.append( sUnmatchedRemovedTag );
     240             :                         }
     241             :                         else
     242             :                         {
     243         201 :                             procInfo.bInProcedure = false;
     244         201 :                             procInfo.nPos = 0;
     245             :                         }
     246             :                     }
     247             :                     // normal source code line
     248        5970 :                     if( !mbExecutable )
     249           0 :                         aSourceCode.appendAscii( "Rem " );
     250        5970 :                     aSourceCode.append( aCodeLine ).append( '\n' );
     251             :                 }
     252        7221 :             }
     253         160 :         }
     254             :     }
     255         160 :     return aSourceCode.makeStringAndClear();
     256             : }
     257             : 
     258         160 : void VbaModule::createModule( const OUString& rVBASourceCode,
     259             :                               const Reference< container::XNameContainer >& rxBasicLib,
     260             :                               const Reference< container::XNameAccess >& rxDocObjectNA ) const
     261             : {
     262         160 :     if( maName.isEmpty() )
     263         160 :         return;
     264             : 
     265             :     // prepare the Basic module
     266         160 :     script::ModuleInfo aModuleInfo;
     267         160 :     aModuleInfo.ModuleType = mnType;
     268         320 :     OUStringBuffer aSourceCode;
     269         160 :     aSourceCode.appendAscii( "Rem Attribute VBA_ModuleType=" );
     270         160 :     switch( mnType )
     271             :     {
     272             :         case script::ModuleType::NORMAL:
     273          30 :             aSourceCode.appendAscii( "VBAModule" );
     274          30 :         break;
     275             :         case script::ModuleType::CLASS:
     276           0 :             aSourceCode.appendAscii( "VBAClassModule" );
     277           0 :         break;
     278             :         case script::ModuleType::FORM:
     279           2 :             aSourceCode.appendAscii( "VBAFormModule" );
     280             :             // hack from old filter, document Basic should know the XModel, but it doesn't
     281           2 :             aModuleInfo.ModuleObject.set( mxDocModel, UNO_QUERY );
     282           2 :         break;
     283             :         case script::ModuleType::DOCUMENT:
     284         128 :             aSourceCode.appendAscii( "VBADocumentModule" );
     285             :             // get the VBA implementation object associated to the document module
     286         128 :             if( rxDocObjectNA.is() ) try
     287             :             {
     288         108 :                 aModuleInfo.ModuleObject.set( rxDocObjectNA->getByName( maName ), UNO_QUERY );
     289             :             }
     290           0 :             catch (const Exception&)
     291             :             {
     292             :             }
     293         128 :         break;
     294             :         default:
     295           0 :             aSourceCode.appendAscii( "VBAUnknown" );
     296             :     }
     297         160 :     aSourceCode.append( '\n' );
     298         160 :     if( mbExecutable )
     299             :     {
     300         160 :         aSourceCode.appendAscii( "Option VBASupport 1\n" );
     301         160 :         if( mnType == script::ModuleType::CLASS )
     302           0 :             aSourceCode.appendAscii( "Option ClassModule\n" );
     303             :     }
     304             :     else
     305             :     {
     306             :         // add a subroutine named after the module itself
     307           0 :         aSourceCode.appendAscii( "Sub " ).
     308           0 :             append( maName.replace( ' ', '_' ) ).append( '\n' );
     309             :     }
     310             : 
     311             :     // append passed VBA source code
     312         160 :     aSourceCode.append( rVBASourceCode );
     313             : 
     314             :     // close the subroutine named after the module
     315         160 :     if( !mbExecutable )
     316           0 :         aSourceCode.appendAscii( "End Sub\n" );
     317             : 
     318             :     // insert extended module info
     319             :     try
     320             :     {
     321         160 :         Reference< XVBAModuleInfo > xVBAModuleInfo( rxBasicLib, UNO_QUERY_THROW );
     322         160 :         xVBAModuleInfo->insertModuleInfo( maName, aModuleInfo );
     323             :     }
     324           0 :     catch (const Exception&)
     325             :     {
     326             :     }
     327             : 
     328             :     // insert the module into the passed Basic library
     329             :     try
     330             :     {
     331         160 :         rxBasicLib->insertByName( maName, Any( aSourceCode.makeStringAndClear() ) );
     332             :     }
     333           0 :     catch (const Exception&)
     334             :     {
     335             :         OSL_FAIL( "VbaModule::createModule - cannot insert module into library" );
     336         160 :     }
     337             : }
     338             : 
     339             : } // namespace ole
     340         246 : } // namespace oox
     341             : 
     342             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11