diff options
Diffstat (limited to 'oox/source/ole/vbahelper.cxx')
-rwxr-xr-x | oox/source/ole/vbahelper.cxx | 171 |
1 files changed, 4 insertions, 167 deletions
diff --git a/oox/source/ole/vbahelper.cxx b/oox/source/ole/vbahelper.cxx index 7293e357e746..3bf72d30bfa5 100755 --- a/oox/source/ole/vbahelper.cxx +++ b/oox/source/ole/vbahelper.cxx @@ -27,27 +27,15 @@ #include "oox/ole/vbahelper.hxx" #include <rtl/ustrbuf.hxx> -#include <com/sun/star/beans/PropertyValue.hpp> -#include <com/sun/star/container/XNameContainer.hpp> -#include <com/sun/star/document/XEventsSupplier.hpp> -#include <comphelper/string.hxx> #include "oox/helper/binaryinputstream.hxx" -using ::rtl::OUString; -using ::rtl::OUStringBuffer; -using ::com::sun::star::beans::PropertyValue; -using ::com::sun::star::container::XNameContainer; -using ::com::sun::star::container::XNameReplace; -using ::com::sun::star::document::XEventsSupplier; -using ::com::sun::star::uno::Any; -using ::com::sun::star::uno::Exception; -using ::com::sun::star::uno::Sequence; -using ::com::sun::star::uno::Reference; -using ::com::sun::star::uno::UNO_SET_THROW; - namespace oox { namespace ole { +using ::rtl::OUString; +using ::rtl::OUStringBuffer; +using namespace ::com::sun::star::uno; + // ============================================================================ /*static*/ OUString VbaHelper::getBasicScriptUrl( @@ -91,157 +79,6 @@ namespace ole { return false; } -/*static*/ bool VbaHelper::eatWhitespace( OUString& rCodeLine ) -{ - sal_Int32 nIndex = 0; - while( (nIndex < rCodeLine.getLength()) && ((rCodeLine[ nIndex ] == ' ') || (rCodeLine[ nIndex ] == '\t')) ) - ++nIndex; - if( nIndex > 0 ) - { - rCodeLine = rCodeLine.copy( nIndex ); - return true; - } - return false; -} - -/*static*/ bool VbaHelper::eatKeyword( OUString& rCodeLine, const OUString& rKeyword ) -{ - if( rCodeLine.matchIgnoreAsciiCase( rKeyword ) ) - { - rCodeLine = rCodeLine.copy( rKeyword.getLength() ); - // success, if code line ends after keyword, or if whitespace follows - return (rCodeLine.getLength() == 0) || eatWhitespace( rCodeLine ); - } - return false; -} - -/*static*/ OUString VbaHelper::getSourceCode( const Reference< XNameContainer >& rxBasicLib, const OUString& rModuleName ) -{ - OUString aSourceCode; - if( rxBasicLib.is() ) try - { - rxBasicLib->getByName( rModuleName ) >>= aSourceCode; - } - catch( Exception& ) - { - } - return aSourceCode; -} - -namespace { - -bool lclGetLine( OUString& rCodeLine, sal_Int32& rnIndex, const OUString& rSourceCode ) -{ - if( rnIndex < rSourceCode.getLength() ) - { - sal_Int32 nPosLF = rSourceCode.indexOf( '\n', rnIndex ); - if( nPosLF >= rnIndex ) - { - rCodeLine = rSourceCode.copy( rnIndex, nPosLF - rnIndex ).trim(); - rnIndex = nPosLF + 1; - return true; - } - } - return false; -} - -} // namespace - -/*static*/ bool VbaHelper::hasMacro( const OUString& rSourceCode, const OUString& rMacroName ) -{ - // scan all text lines for '[Public|Private] [Static] Sub <macroname> (...)' - const OUString aPublic = CREATE_OUSTRING( "Public" ); - const OUString aPrivate = CREATE_OUSTRING( "Private" ); - const OUString aStatic = CREATE_OUSTRING( "Static" ); - const OUString aSub = CREATE_OUSTRING( "Sub" ); - - OUString aCodeLine; - sal_Int32 nIndex = 0; - while( lclGetLine( aCodeLine, nIndex, rSourceCode ) ) - { - // eat optional 'Private' or 'Public', but do not accept both keywords in a row (therefore the ||) - eatKeyword( aCodeLine, aPublic ) || eatKeyword( aCodeLine, aPrivate ); - // eat optional 'Static' - eatKeyword( aCodeLine, aStatic ); - // eat 'Sub' keyword, check if macro name follows - if( eatKeyword( aCodeLine, aSub ) && aCodeLine.matchIgnoreAsciiCase( rMacroName ) ) - { - // eat macro name and following whitespace - aCodeLine = aCodeLine.copy( rMacroName.getLength() ); - eatWhitespace( aCodeLine ); - // opening bracket must follow the macro name - if( (aCodeLine.getLength() >= 2) && (aCodeLine[ 0 ] == '(') ) - return true; - } - } - return false; -} - -/*static*/ bool VbaHelper::hasMacro( const Reference< XNameContainer >& rxBasicLib, - const OUString& rModuleName, const OUString& rMacroName ) -{ - return hasMacro( getSourceCode( rxBasicLib, rModuleName ), rMacroName ); -} - -/*static*/ bool VbaHelper::insertMacro( const Reference< XNameContainer >& rxBasicLib, const OUString& rModuleName, - const OUString& rMacroName, const OUString& rMacroArgs, const OUString& rMacroType, const OUString& rMacroCode ) -{ - if( rxBasicLib.is() ) try - { - // receive module source code and check that the specified macro does not exist - OUString aSourceCode = getSourceCode( rxBasicLib, rModuleName ); - if( !hasMacro( aSourceCode, rMacroName ) ) - { - bool bFunction = rMacroType.getLength() > 0; - const sal_Char* pcSubFunc = bFunction ? "Function" : "Sub"; - OUStringBuffer aBuffer( aSourceCode ); - // generate the source code for the new macro - aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "\nPrivate " ) ). - appendAscii( pcSubFunc ).append( sal_Unicode( ' ' ) ). - append( rMacroName ).append( sal_Unicode( '(' ) ); - if( rMacroArgs.getLength() > 0 ) - aBuffer.append( sal_Unicode( ' ' ) ).append( rMacroArgs ).append( sal_Unicode( ' ' ) ); - aBuffer.append( sal_Unicode( ')' ) ); - if( bFunction ) - aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( " As " ) ).append( rMacroType ); - aBuffer.append( sal_Unicode( '\n' ) ); - // replace all $MACRO placeholders with macro name - if( rMacroCode.getLength() > 0 ) - { - OUString aMacroCode = ::comphelper::string::searchAndReplaceAsciiL( rMacroCode, RTL_CONSTASCII_STRINGPARAM( "$MACRO" ), rMacroName ); - aBuffer.append( aMacroCode ).append( sal_Unicode( '\n' ) ); - } - aBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM( "End " ) ).appendAscii( pcSubFunc ).append( sal_Unicode( '\n' ) ); - rxBasicLib->replaceByName( rModuleName, Any( aBuffer.makeStringAndClear() ) ); - return true; - } - } - catch( Exception& ) - { - } - return false; -} - -/*static*/ bool VbaHelper::attachMacroToEvent( const Reference< XEventsSupplier >& rxEventsSupp, - const OUString& rEventName, const OUString& rLibraryName, const OUString& rModuleName, const OUString& rMacroName ) -{ - if( rxEventsSupp.is() ) try - { - Reference< XNameReplace > xEvents( rxEventsSupp->getEvents(), UNO_SET_THROW ); - Sequence< PropertyValue > aEvent( 2 ); - aEvent[ 0 ].Name = CREATE_OUSTRING( "EventType" ); - aEvent[ 0 ].Value <<= CREATE_OUSTRING( "Script" ); - aEvent[ 1 ].Name = CREATE_OUSTRING( "Script" ); - aEvent[ 1 ].Value <<= getBasicScriptUrl( rLibraryName, rModuleName, rMacroName ); - xEvents->replaceByName( rEventName, Any( aEvent ) ); - return true; - } - catch( Exception& ) - { - } - return false; -} - // ============================================================================ } // namespace ole |