LCOV - code coverage report
Current view: top level - libreoffice/scripting/source/provider - MasterScriptProvider.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 96 305 31.5 %
Date: 2012-12-27 Functions: 12 33 36.4 %
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             : 
      21             : #include <comphelper/documentinfo.hxx>
      22             : 
      23             : #include <cppuhelper/implementationentry.hxx>
      24             : #include <cppuhelper/exc_hlp.hxx>
      25             : #include <cppuhelper/factory.hxx>
      26             : #include <tools/diagnose_ex.h>
      27             : 
      28             : #include <com/sun/star/frame/XModel.hpp>
      29             : #include <com/sun/star/lang/EventObject.hpp>
      30             : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      31             : #include <com/sun/star/document/XScriptInvocationContext.hpp>
      32             : 
      33             : #include <com/sun/star/uri/XUriReference.hpp>
      34             : #include <com/sun/star/uri/UriReferenceFactory.hpp>
      35             : #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
      36             : 
      37             : #include <com/sun/star/deployment/XPackage.hpp>
      38             : #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
      39             : #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
      40             : #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
      41             : 
      42             : #include <util/scriptingconstants.hxx>
      43             : #include <util/util.hxx>
      44             : #include <util/MiscUtils.hxx>
      45             : 
      46             : #include "ActiveMSPList.hxx"
      47             : #include "MasterScriptProvider.hxx"
      48             : #include "URIHelper.hxx"
      49             : 
      50             : using namespace ::com::sun::star;
      51             : using namespace ::com::sun::star::uno;
      52             : using namespace ::com::sun::star::script;
      53             : using namespace ::com::sun::star::document;
      54             : using namespace ::sf_misc;
      55             : 
      56             : namespace func_provider
      57             : {
      58             : //*************************************************************************
      59             : //  Definitions for MasterScriptProviderFactory global methods.
      60             : //*************************************************************************
      61             : 
      62             : ::rtl::OUString SAL_CALL mspf_getImplementationName() ;
      63             : Reference< XInterface > SAL_CALL mspf_create( Reference< XComponentContext > const & xComponentContext );
      64             : Sequence< ::rtl::OUString > SAL_CALL mspf_getSupportedServiceNames();
      65             : 
      66             : 
      67           0 : bool endsWith( const ::rtl::OUString& target,
      68             :     const ::rtl::OUString& item )
      69             : {
      70           0 :     sal_Int32 index = 0;
      71           0 :     if (  ( index = target.indexOf( item ) ) != -1  &&
      72           0 :        ( index == ( target.getLength() - item.getLength() ) ) )
      73             :     {
      74           0 :         return true;
      75             :     }
      76           0 :     return false;
      77             : }
      78             : //::rtl_StandardModuleCount s_moduleCount = MODULE_COUNT_INIT;
      79             : 
      80             : /* should be available in some central location. */
      81             : //*************************************************************************
      82             : // XScriptProvider implementation
      83             : //
      84             : //*************************************************************************
      85           1 : MasterScriptProvider::MasterScriptProvider( const Reference< XComponentContext > & xContext ) throw ( RuntimeException ):
      86             :         m_xContext( xContext ), m_bIsValid( false ), m_bInitialised( false ),
      87           1 :         m_bIsPkgMSP( false ), m_pPCache( 0 )
      88             : {
      89           1 :     ENSURE_OR_THROW( m_xContext.is(), "MasterScriptProvider::MasterScriptProvider: No context available\n" );
      90           1 :     m_xMgr = m_xContext->getServiceManager();
      91           1 :     ENSURE_OR_THROW( m_xMgr.is(), "MasterScriptProvider::MasterScriptProvider: No service manager available\n" );
      92           1 :     m_bIsValid = true;
      93           1 : }
      94             : 
      95             : //*************************************************************************
      96           3 : MasterScriptProvider::~MasterScriptProvider()
      97             : {
      98             :     //s_moduleCount.modCnt.release( &s_moduleCount.modCnt );
      99           1 :     if ( m_pPCache )
     100             :     {
     101           1 :         delete m_pPCache;
     102             :     }
     103           1 :     m_pPCache = 0;
     104           2 : }
     105             : 
     106             : //*************************************************************************
     107           1 : void SAL_CALL MasterScriptProvider::initialize( const Sequence < Any >& args )
     108             : throw ( Exception, RuntimeException )
     109             : {
     110           1 :     if ( m_bInitialised )
     111           1 :         return;
     112             : 
     113           1 :     m_bIsValid = false;
     114             : 
     115           1 :     sal_Int32 len = args.getLength();
     116           1 :     if ( len > 1  )
     117             :     {
     118             :         throw RuntimeException(
     119             :             OUSTR( "MasterScriptProvider::initialize: invalid number of arguments" ),
     120           0 :             Reference< XInterface >() );
     121             :     }
     122             : 
     123           1 :     Sequence< Any > invokeArgs( len );
     124             : 
     125           1 :     if ( len != 0 )
     126             :     {
     127             :         // check if first parameter is a string
     128             :         // if it is, this implies that this is a MSP created
     129             :         // with a user or share ctx ( used for browse functionality )
     130             :         //
     131           1 :         if ( args[ 0 ] >>= m_sCtxString )
     132             :         {
     133           1 :             invokeArgs[ 0  ] = args[ 0 ];
     134           1 :             if ( m_sCtxString.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.tdoc" ) ) == 0 )
     135             :             {
     136           1 :                 m_xModel =  MiscUtils::tDocUrlToModel( m_sCtxString );
     137             :             }
     138             :         }
     139           0 :         else if ( args[ 0 ] >>= m_xInvocationContext )
     140             :         {
     141           0 :             m_xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY_THROW );
     142             :         }
     143             :         else
     144             :         {
     145           0 :             args[ 0 ] >>= m_xModel;
     146             :         }
     147             : 
     148           1 :         if ( m_xModel.is() )
     149             :         {
     150             :             // from the arguments, we were able to deduce a model. That alone doesn't
     151             :             // suffice, we also need an XEmbeddedScripts which actually indicates support
     152             :             // for embeddeding scripts
     153           1 :             Reference< XEmbeddedScripts > xScripts( m_xModel, UNO_QUERY );
     154           1 :             if ( !xScripts.is() )
     155             :             {
     156             :                 throw lang::IllegalArgumentException(
     157             :                     ::rtl::OUString( "The given document does not support embedding scripts into it, and cannot be associated with such a document."
     158             :                      ),
     159             :                     *this,
     160             :                     1
     161           0 :                 );
     162             :             }
     163             : 
     164             :             try
     165             :             {
     166           1 :                 m_sCtxString =  MiscUtils::xModelToTdocUrl( m_xModel, m_xContext );
     167             :             }
     168           0 :             catch ( const Exception& )
     169             :             {
     170           0 :                 Any aError( ::cppu::getCaughtException() );
     171             : 
     172           0 :                 ::rtl::OUStringBuffer buf;
     173           0 :                 buf.appendAscii( "MasterScriptProvider::initialize: caught " );
     174           0 :                 buf.append     ( aError.getValueTypeName() );
     175           0 :                 buf.appendAscii( ":" );
     176             : 
     177           0 :                 Exception aException; aError >>= aException;
     178           0 :                 buf.append     ( aException.Message );
     179           0 :                 throw lang::WrappedTargetException( buf.makeStringAndClear(), *this, aError );
     180             :             }
     181             : 
     182           1 :             if ( m_xInvocationContext.is() && m_xInvocationContext != m_xModel )
     183           0 :                 invokeArgs[ 0 ] <<= m_xInvocationContext;
     184             :             else
     185           1 :                 invokeArgs[ 0 ] <<= m_sCtxString;
     186             :         }
     187             : 
     188           1 :         ::rtl::OUString pkgSpec = OUSTR("uno_packages");
     189           1 :         sal_Int32 indexOfPkgSpec = m_sCtxString.lastIndexOf( pkgSpec );
     190             : 
     191             :         // if contex string ends with "uno_packages"
     192           1 :         if ( indexOfPkgSpec > -1 && ( m_sCtxString.match( pkgSpec, indexOfPkgSpec ) == sal_True ) )
     193             :         {
     194           0 :             m_bIsPkgMSP = sal_True;
     195             :         }
     196             :         else
     197             :         {
     198           1 :             m_bIsPkgMSP = sal_False;
     199           1 :         }
     200             :     }
     201             :     else // no args
     202             :     {
     203             :         // use either scriping context or maybe zero args?
     204           0 :         invokeArgs = Sequence< Any >( 0 ); // no arguments
     205             :     }
     206           1 :     m_sAargs = invokeArgs;
     207             :     // don't create pkg mgr MSP for documents, not supported
     208           1 :     if ( m_bIsPkgMSP == sal_False && !m_xModel.is() )
     209             :     {
     210           0 :         createPkgProvider();
     211             :     }
     212             : 
     213           1 :     m_bInitialised = true;
     214           1 :     m_bIsValid = true;
     215             : }
     216             : 
     217             : 
     218             : //*************************************************************************
     219           0 : void MasterScriptProvider::createPkgProvider()
     220             : {
     221             :     try
     222             :     {
     223           0 :         Any location;
     224           0 :         ::rtl::OUString sPkgCtx =  m_sCtxString.concat( OUSTR(":uno_packages") );
     225           0 :         location <<= sPkgCtx;
     226             : 
     227             :         Reference< provider::XScriptProviderFactory > xFac(
     228           0 :             m_xContext->getValueByName(
     229           0 :                 OUSTR( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW );
     230             : 
     231             :         m_xMSPPkg.set(
     232           0 :             xFac->createScriptProvider( location ), UNO_QUERY_THROW );
     233             : 
     234             :     }
     235           0 :     catch ( const Exception& e )
     236             :     {
     237             :         (void)e;
     238             :         OSL_TRACE("Exception creating MasterScriptProvider for uno_packages in context %s: %s",
     239             :                 ::rtl::OUStringToOString( m_sCtxString,
     240             :                     RTL_TEXTENCODING_ASCII_US ).pData->buffer,
     241             :                 ::rtl::OUStringToOString( e.Message,
     242             :                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
     243             :     }
     244           0 : }
     245             : 
     246             : //*************************************************************************
     247             : Reference< provider::XScript >
     248           1 : MasterScriptProvider::getScript( const ::rtl::OUString& scriptURI )
     249             : throw ( provider::ScriptFrameworkErrorException,
     250             :         RuntimeException )
     251             : {
     252           1 :     if ( !isValid() )
     253             :     {
     254             :         throw provider::ScriptFrameworkErrorException(
     255             :             OUSTR( "MasterScriptProvider not initialised" ), Reference< XInterface >(),
     256             :             scriptURI, OUSTR(""),
     257           0 :             provider::ScriptFrameworkErrorType::UNKNOWN );
     258             :     }
     259             : 
     260             :     // need to get the language from the string
     261             : 
     262           1 :     Reference< uri::XUriReferenceFactory > xFac ( uri::UriReferenceFactory::create( m_xContext )  );
     263             : 
     264             :     Reference<  uri::XUriReference > uriRef(
     265           1 :         xFac->parse( scriptURI ), UNO_QUERY );
     266             : 
     267           1 :     Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
     268             : 
     269           1 :     if ( !uriRef.is() || !sfUri.is() )
     270             :     {
     271           0 :         ::rtl::OUString errorMsg = OUSTR( "Incorrect format for Script URI: " );
     272           0 :         errorMsg = errorMsg.concat( scriptURI );
     273             :         throw provider::ScriptFrameworkErrorException(
     274             :             errorMsg, Reference< XInterface >(),
     275             :             scriptURI, OUSTR(""),
     276           0 :             provider::ScriptFrameworkErrorType::UNKNOWN );
     277             :     }
     278             : 
     279           1 :     ::rtl::OUString langKey("language");
     280           1 :     ::rtl::OUString locKey("location");
     281             : 
     282           5 :     if ( sfUri->hasParameter( langKey ) == sal_False ||
     283           1 :          sfUri->hasParameter( locKey ) == sal_False ||
     284           3 :          ( sfUri->getName().isEmpty()  ) )
     285             :     {
     286           0 :         ::rtl::OUString errorMsg = OUSTR( "Incorrect format for Script URI: " );
     287           0 :         errorMsg = errorMsg.concat( scriptURI );
     288             :         throw provider::ScriptFrameworkErrorException(
     289             :             errorMsg, Reference< XInterface >(),
     290             :             scriptURI, OUSTR(""),
     291           0 :             provider::ScriptFrameworkErrorType::UNKNOWN );
     292             :     }
     293             : 
     294           1 :     ::rtl::OUString language = sfUri->getParameter( langKey );
     295           1 :     ::rtl::OUString location = sfUri->getParameter( locKey );
     296             : 
     297             :     // if script us located in uno pkg
     298           1 :     sal_Int32 index = -1;
     299           1 :     ::rtl::OUString pkgTag(":uno_packages");
     300             :     // for languages other than basic,  scripts located in uno packages
     301             :     // are merged into the user/share location context.
     302             :     // For other languages the location attribute in script url has the form
     303             :     // location = [user|share]:uno_packages or location :uno_pacakges/xxxx.uno.pkg
     304             :     // we need to extract the value of location part from the
     305             :     // location attribute of the script, if the script is located in an
     306             :     // uno package then that is the location part up to and including
     307             :     // ":uno_packages", if the script is not in an uno package then the
     308             :     // normal value is used e.g. user or share.
     309             :     // The value extracted will be used to determine if the script is
     310             :     // located in the same location context as this MSP.
     311             :     // For Basic, the language script provider can handle the execution of a
     312             :     // script in any location context
     313           1 :     if ( ( index = location.indexOf( pkgTag ) ) > -1 )
     314             :     {
     315           0 :         location = location.copy( 0, index + pkgTag.getLength() );
     316             :     }
     317             : 
     318           1 :     Reference< provider::XScript > xScript;
     319             : 
     320             :     // If the script location is in the same location context as this
     321             :     // MSP then delate to the lanaguage provider controlled by this MSP
     322             :     // ** Special case is BASIC, all calls to getScript will be handled
     323             :     // by the language script provider in the current location context
     324             :     // even if its different
     325           3 :     if  (   (   location.equals( OUSTR( "document" ) )
     326           1 :             &&  m_xModel.is()
     327             :             )
     328           0 :             ||  ( endsWith( m_sCtxString, location ) )
     329           1 :             ||  ( language.equals( OUSTR( "Basic" ) ) )
     330             :          )
     331             :     {
     332           1 :         Reference< provider::XScriptProvider > xScriptProvider;
     333           1 :         ::rtl::OUStringBuffer buf( 80 );
     334           1 :         buf.appendAscii( "com.sun.star.script.provider.ScriptProviderFor");
     335           1 :         buf.append( language );
     336           1 :         ::rtl::OUString serviceName = buf.makeStringAndClear();
     337           1 :         if ( providerCache() )
     338             :         {
     339             :             try
     340             :             {
     341             :                 xScriptProvider.set(
     342             :                     providerCache()->getProvider( serviceName ),
     343           1 :                     UNO_QUERY_THROW );
     344             :             }
     345           0 :             catch( const Exception& e )
     346             :             {
     347             :                 throw provider::ScriptFrameworkErrorException(
     348             :                     e.Message, Reference< XInterface >(),
     349           0 :                     sfUri->getName(), language,
     350           0 :                     provider::ScriptFrameworkErrorType::NOTSUPPORTED );
     351             :             }
     352             :         }
     353             :         else
     354             :         {
     355             :             throw provider::ScriptFrameworkErrorException(
     356             :                 OUSTR( "No LanguageProviders detected" ),
     357             :                 Reference< XInterface >(),
     358           0 :                 sfUri->getName(), language,
     359           0 :                 provider::ScriptFrameworkErrorType::NOTSUPPORTED );
     360             :         }
     361           2 :         xScript=xScriptProvider->getScript( scriptURI );
     362             :     }
     363             :     else
     364             :     {
     365             :         Reference< provider::XScriptProviderFactory > xFac_(
     366           0 :             m_xContext->getValueByName(
     367           0 :                 OUSTR( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW );
     368             : 
     369             :         Reference< provider::XScriptProvider > xSP(
     370           0 :             xFac_->createScriptProvider( makeAny( location ) ), UNO_QUERY_THROW );
     371           0 :         xScript = xSP->getScript( scriptURI );
     372             :     }
     373             : 
     374           1 :     return xScript;
     375             : }
     376             : //*************************************************************************
     377             : bool
     378           1 : MasterScriptProvider::isValid()
     379             : {
     380           1 :     return m_bIsValid;
     381             : }
     382             : 
     383             : //*************************************************************************
     384             : ProviderCache*
     385           2 : MasterScriptProvider::providerCache()
     386             : {
     387           2 :     if ( !m_pPCache )
     388             :     {
     389           1 :         ::osl::MutexGuard aGuard( m_mutex );
     390           1 :         if ( !m_pPCache )
     391             :         {
     392           1 :             ::rtl::OUString serviceName1 = OUSTR("com.sun.star.script.provider.ScriptProviderForBasic");
     393           1 :             Sequence< ::rtl::OUString > blacklist(1);
     394           1 :             blacklist[ 0 ] = serviceName1;
     395             : 
     396           1 :             if ( !m_bIsPkgMSP )
     397             :             {
     398           1 :                 m_pPCache = new ProviderCache( m_xContext, m_sAargs );
     399             :             }
     400             :             else
     401             :             {
     402           0 :                 m_pPCache = new ProviderCache( m_xContext, m_sAargs, blacklist );
     403           1 :             }
     404           1 :         }
     405             :     }
     406           2 :     return m_pPCache;
     407             : }
     408             : 
     409             : 
     410             : //*************************************************************************
     411             : ::rtl::OUString SAL_CALL
     412           0 : MasterScriptProvider::getName()
     413             :         throw ( css::uno::RuntimeException )
     414             : {
     415           0 :     if ( !isPkgProvider() )
     416             :     {
     417           0 :         ::rtl::OUString sCtx = getContextString();
     418           0 :         if ( sCtx.indexOf( OUSTR( "vnd.sun.star.tdoc" ) ) == 0 )
     419             :         {
     420           0 :             Reference< frame::XModel > xModel = m_xModel;
     421           0 :             if ( !xModel.is() )
     422             :             {
     423           0 :                 xModel = MiscUtils::tDocUrlToModel( sCtx );
     424             :             }
     425             : 
     426           0 :             m_sNodeName = ::comphelper::DocumentInfo::getDocumentTitle( xModel );
     427             :         }
     428             :         else
     429             :         {
     430           0 :             m_sNodeName = parseLocationName( getContextString() );
     431           0 :         }
     432             :     }
     433             :     else
     434             :     {
     435           0 :         m_sNodeName = OUSTR("uno_packages");
     436             :     }
     437           0 :     return m_sNodeName;
     438             : }
     439             : 
     440             : //*************************************************************************
     441             : Sequence< Reference< browse::XBrowseNode > > SAL_CALL
     442           0 : MasterScriptProvider::getChildNodes()
     443             :         throw ( css::uno::RuntimeException )
     444             : {
     445           0 :     Sequence< Reference< provider::XScriptProvider > > providers = getAllProviders();
     446             : 
     447           0 :     Reference< provider::XScriptProvider > pkgProv = getPkgProvider();
     448           0 :     sal_Int32 size = providers.getLength();
     449           0 :     bool hasPkgs = pkgProv.is();
     450           0 :     if ( hasPkgs  )
     451             :     {
     452           0 :         size++;
     453             :     }
     454           0 :     Sequence<  Reference< browse::XBrowseNode > > children( size );
     455           0 :     sal_Int32 provIndex = 0;
     456           0 :     for ( ; provIndex < providers.getLength(); provIndex++ )
     457             :     {
     458           0 :         children[ provIndex ] = Reference< browse::XBrowseNode >( providers[ provIndex ], UNO_QUERY );
     459             :     }
     460             : 
     461           0 :     if ( hasPkgs  )
     462             :     {
     463           0 :         children[ provIndex ] = Reference< browse::XBrowseNode >( pkgProv, UNO_QUERY );
     464             : 
     465             :     }
     466             : 
     467           0 :     return children;
     468             : }
     469             : 
     470             : //*************************************************************************
     471             : sal_Bool SAL_CALL
     472           0 : MasterScriptProvider::hasChildNodes()
     473             :         throw ( css::uno::RuntimeException )
     474             : {
     475           0 :     return sal_True;
     476             : }
     477             : 
     478             : //*************************************************************************
     479             : sal_Int16 SAL_CALL
     480           0 : MasterScriptProvider::getType()
     481             :         throw ( css::uno::RuntimeException )
     482             : {
     483           0 :     return browse::BrowseNodeTypes::CONTAINER;
     484             : }
     485             : 
     486             : //*************************************************************************
     487             : 
     488             : ::rtl::OUString
     489           0 : MasterScriptProvider::parseLocationName( const ::rtl::OUString& location )
     490             : {
     491             :     // strip out the last leaf of location name
     492             :     // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw
     493           0 :     ::rtl::OUString temp = location;
     494           0 :     INetURLObject aURLObj( temp );
     495           0 :     if ( !aURLObj.HasError() )
     496           0 :         temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET );
     497           0 :     return temp;
     498             : }
     499             : 
     500             : //*************************************************************************
     501             : // Register Package
     502             : void SAL_CALL
     503           0 : MasterScriptProvider::insertByName( const ::rtl::OUString& aName, const Any& aElement ) throw ( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, css::uno::RuntimeException)
     504             : {
     505           0 :     if ( !m_bIsPkgMSP )
     506             :     {
     507           0 :         if ( m_xMSPPkg.is() )
     508             :         {
     509           0 :             Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY );
     510           0 :             if ( !xCont.is() )
     511             :             {
     512             :                 throw RuntimeException(
     513             :                     OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"),
     514           0 :                     Reference< XInterface >() );
     515             :             }
     516           0 :             xCont->insertByName( aName, aElement );
     517             :         }
     518             :         else
     519             :         {
     520             :             throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"),
     521           0 :                                         Reference< XInterface >() );
     522             :         }
     523             : 
     524             :     }
     525             :     else
     526             :     {
     527           0 :         Reference< deployment::XPackage > xPkg( aElement, UNO_QUERY );
     528           0 :         if ( !xPkg.is() )
     529             :         {
     530             :             throw lang::IllegalArgumentException( OUSTR("Couldn't convert to XPackage"),
     531           0 :                                                       Reference < XInterface > (), 2 );
     532             :         }
     533           0 :         if ( aName.isEmpty() )
     534             :         {
     535             :             throw lang::IllegalArgumentException( OUSTR("Name not set!!"),
     536           0 :                                                       Reference < XInterface > (), 1 );
     537             :         }
     538             :         // TODO for library pacakge parse the language, for the moment will try
     539             :         // to get each provider to process the new Package, the first one the succeeds
     540             :         // will terminate processing
     541           0 :         if ( !providerCache() )
     542             :         {
     543             :             throw RuntimeException(
     544             :                 OUSTR("insertByName cannot instantiate "
     545             :                     "child script providers."),
     546           0 :                 Reference< XInterface >() );
     547             :         }
     548             :         Sequence < Reference< provider::XScriptProvider > > xSProviders =
     549           0 :             providerCache()->getAllProviders();
     550           0 :         sal_Int32 index = 0;
     551             : 
     552           0 :         for ( ; index < xSProviders.getLength(); index++ )
     553             :         {
     554           0 :             Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY );
     555           0 :             if ( !xCont.is() )
     556             :             {
     557           0 :                 continue;
     558             :             }
     559             :             try
     560             :             {
     561           0 :                 xCont->insertByName( aName, aElement );
     562             :                 break;
     563             :             }
     564           0 :             catch ( Exception& )
     565             :             {
     566             :             }
     567             : 
     568           0 :         }
     569           0 :         if ( index == xSProviders.getLength() )
     570             :         {
     571             :             // No script providers could process the package
     572           0 :             ::rtl::OUString message = OUSTR("Failed to register package for ");
     573           0 :             message = message.concat( aName );
     574             :             throw lang::IllegalArgumentException( message,
     575           0 :                 Reference < XInterface > (), 2 );
     576           0 :         }
     577             :    }
     578           0 : }
     579             : 
     580             : //*************************************************************************
     581             : // Revoke Package
     582             : void SAL_CALL
     583           0 : MasterScriptProvider::removeByName( const ::rtl::OUString& Name ) throw ( container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     584             : {
     585           0 :     if ( !m_bIsPkgMSP )
     586             :     {
     587           0 :         if ( m_xMSPPkg.is() )
     588             :         {
     589           0 :             Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY );
     590           0 :             if ( !xCont.is() )
     591             :             {
     592             :                 throw RuntimeException(
     593             :                     OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"),
     594           0 :                     Reference< XInterface >() );
     595             :             }
     596           0 :             xCont->removeByName( Name );
     597             :         }
     598             :         else
     599             :         {
     600             :             throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"),
     601           0 :                                         Reference< XInterface >() );
     602             :         }
     603             : 
     604             :    }
     605             :    else
     606             :    {
     607           0 :         if ( Name.isEmpty() )
     608             :         {
     609             :             throw lang::IllegalArgumentException( OUSTR("Name not set!!"),
     610           0 :                                                       Reference < XInterface > (), 1 );
     611             :         }
     612             :         // TODO for Script library pacakge url parse the language,
     613             :         // for the moment will just try to get each provider to process remove/revoke
     614             :         // request, the first one the succeeds will terminate processing
     615             : 
     616           0 :         if ( !providerCache() )
     617             :         {
     618             :             throw RuntimeException(
     619             :                 OUSTR("removeByName() cannot instantiate "
     620             :                     "child script providers."),
     621           0 :                 Reference< XInterface >() );
     622             :         }
     623             :         Sequence < Reference< provider::XScriptProvider > > xSProviders =
     624           0 :             providerCache()->getAllProviders();
     625           0 :         sal_Int32 index = 0;
     626           0 :         for ( ; index < xSProviders.getLength(); index++ )
     627             :         {
     628           0 :             Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY );
     629           0 :             if ( !xCont.is() )
     630             :             {
     631           0 :                 continue;
     632             :             }
     633             :             try
     634             :             {
     635           0 :                 xCont->removeByName( Name );
     636             :                 break;
     637             :             }
     638           0 :             catch ( Exception& )
     639             :             {
     640             :             }
     641             : 
     642           0 :         }
     643           0 :         if ( index == xSProviders.getLength() )
     644             :         {
     645             :             // No script providers could process the package
     646           0 :             ::rtl::OUString message = OUSTR("Failed to revoke package for ");
     647           0 :             message = message.concat( Name );
     648             :             throw lang::IllegalArgumentException( message,
     649           0 :                                                       Reference < XInterface > (), 1 );
     650           0 :         }
     651             : 
     652             :     }
     653           0 : }
     654             : 
     655             : //*************************************************************************
     656             : void SAL_CALL
     657           0 : MasterScriptProvider::replaceByName( const ::rtl::OUString& aName, const Any& aElement ) throw ( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     658             : {
     659             :     (void)aName;
     660             :     (void)aElement;
     661             : 
     662             :     // TODO needs implementing
     663             :     if ( true )
     664             :     {
     665             :         throw RuntimeException(  OUSTR("replaceByName not implemented!!!!") ,
     666           0 :                 Reference< XInterface >() );
     667             :     }
     668             : }
     669             : //*************************************************************************
     670             : Any SAL_CALL
     671           0 : MasterScriptProvider::getByName( const ::rtl::OUString& aName ) throw ( container::NoSuchElementException, lang::WrappedTargetException, RuntimeException)
     672             : {
     673             :     (void)aName;
     674             : 
     675             :     // TODO needs to be implemented
     676           0 :     Any result;
     677             :     if ( true )
     678             :     {
     679             :         throw RuntimeException(  OUSTR("getByName not implemented!!!!") ,
     680           0 :                 Reference< XInterface >() );
     681             :     }
     682           0 :     return result;
     683             : }
     684             : //*************************************************************************
     685             : sal_Bool SAL_CALL
     686           0 : MasterScriptProvider::hasByName( const ::rtl::OUString& aName ) throw (RuntimeException)
     687             : {
     688           0 :     sal_Bool result = sal_False;
     689           0 :     if ( !m_bIsPkgMSP )
     690             :     {
     691           0 :         if ( m_xMSPPkg.is() )
     692             :         {
     693           0 :             Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY );
     694           0 :             if ( !xCont.is() )
     695             :             {
     696             :                 throw RuntimeException(
     697             :                     OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"),
     698           0 :                     Reference< XInterface >() );
     699             :             }
     700             : 
     701           0 :             result = xCont->hasByName( aName );
     702             :         }
     703             :         else
     704             :         {
     705             :             throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"),
     706           0 :                                         Reference< XInterface >() );
     707             :         }
     708             : 
     709             :    }
     710             :    else
     711             :    {
     712           0 :         if ( aName.isEmpty() )
     713             :         {
     714             :             throw lang::IllegalArgumentException( OUSTR("Name not set!!"),
     715           0 :                                                       Reference < XInterface > (), 1 );
     716             :         }
     717             :         // TODO for Script library pacakge url parse the language,
     718             :         // for the moment will just try to get each provider to see if the
     719             :         // package exists in any provider, first one that succeed will
     720             :         // terminate the loop
     721             : 
     722           0 :         if ( !providerCache() )
     723             :         {
     724             :             throw RuntimeException(
     725             :                 OUSTR("removeByName() cannot instantiate "
     726             :                     "child script providers."),
     727           0 :                 Reference< XInterface >() );
     728             :         }
     729             :         Sequence < Reference< provider::XScriptProvider > > xSProviders =
     730           0 :             providerCache()->getAllProviders();
     731           0 :         for ( sal_Int32 index = 0; index < xSProviders.getLength(); index++ )
     732             :         {
     733           0 :             Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY );
     734           0 :             if ( !xCont.is() )
     735             :             {
     736           0 :                 continue;
     737             :             }
     738             :             try
     739             :             {
     740           0 :                 result = xCont->hasByName( aName );
     741           0 :                 if ( result == sal_True )
     742             :                 {
     743             :                     break;
     744             :                 }
     745             :             }
     746           0 :             catch ( Exception& )
     747             :             {
     748             :             }
     749             : 
     750           0 :         }
     751             :     }
     752           0 :     return result;
     753             : }
     754             : 
     755             : //*************************************************************************
     756             : Sequence< ::rtl::OUString > SAL_CALL
     757           0 : MasterScriptProvider::getElementNames(  ) throw ( RuntimeException)
     758             : {
     759             :     // TODO needs implementing
     760           0 :     Sequence< ::rtl::OUString >  names;
     761             :     if ( true )
     762             :     {
     763             :         throw RuntimeException(  OUSTR("getElementNames not implemented!!!!") ,
     764           0 :                 Reference< XInterface >() );
     765             :     }
     766           0 :     return names;
     767             : }
     768             : //*************************************************************************
     769             : Type SAL_CALL
     770           0 : MasterScriptProvider::getElementType(  ) throw ( RuntimeException)
     771             : {
     772             :     // TODO needs implementing
     773           0 :     Type t;
     774           0 :     return t;
     775             : }
     776             : //*************************************************************************
     777           0 : sal_Bool SAL_CALL MasterScriptProvider::hasElements(  ) throw ( RuntimeException)
     778             : {
     779             :     // TODO needs implementing
     780             :     if ( true )
     781             :     {
     782             :         throw RuntimeException(  OUSTR("hasElements not implemented!!!!") ,
     783           0 :                 Reference< XInterface >() );
     784             :     }
     785             :     return false;
     786             : }
     787             : 
     788             : //*************************************************************************
     789             : Sequence< Reference< provider::XScriptProvider > > SAL_CALL
     790           0 : MasterScriptProvider::getAllProviders() throw ( css::uno::RuntimeException )
     791             : {
     792           0 :     if ( providerCache() )
     793             :     {
     794           0 :         return providerCache()->getAllProviders();
     795             :     }
     796             :     else
     797             :     {
     798             :         ::rtl::OUString errorMsg(
     799           0 :             "MasterScriptProvider::getAllProviders, cache not initialised");
     800             :         throw RuntimeException( errorMsg.concat( errorMsg ),
     801           0 :             Reference< XInterface >() );
     802             :     }
     803             : }
     804             : 
     805             : 
     806             : //*************************************************************************
     807           0 : ::rtl::OUString SAL_CALL MasterScriptProvider::getImplementationName( )
     808             : throw( RuntimeException )
     809             : {
     810           0 :     return ::rtl::OUString( "com.sun.star.script.provider.MasterScriptProvider"  );
     811             : }
     812             : 
     813             : //*************************************************************************
     814           0 : sal_Bool SAL_CALL MasterScriptProvider::supportsService( const ::rtl::OUString& serviceName )
     815             : throw( RuntimeException )
     816             : {
     817           0 :     Sequence< ::rtl::OUString > serviceNames( getSupportedServiceNames() );
     818           0 :     ::rtl::OUString const * pNames = serviceNames.getConstArray();
     819           0 :     for ( sal_Int32 nPos = serviceNames.getLength(); nPos--; )
     820             :     {
     821           0 :         if ( serviceName.equals( pNames[ nPos ] ) )
     822             :         {
     823           0 :             return sal_True;
     824             :         }
     825             :     }
     826           0 :     return sal_False;
     827             : }
     828             : 
     829             : //*************************************************************************
     830           0 : Sequence< ::rtl::OUString > SAL_CALL MasterScriptProvider::getSupportedServiceNames( )
     831             : throw( RuntimeException )
     832             : {
     833           0 :     ::rtl::OUString names[3];
     834             : 
     835           0 :     names[0] = ::rtl::OUString( "com.sun.star.script.provider.MasterScriptProvider"  );
     836           0 :     names[1] = ::rtl::OUString( "com.sun.star.script.browse.BrowseNode"  );
     837           0 :     names[2] = ::rtl::OUString( "com.sun.star.script.provider.ScriptProvider"  );
     838             : 
     839           0 :     return Sequence< ::rtl::OUString >( names, 3 );
     840             : }
     841             : 
     842             : } // namespace func_provider
     843             : 
     844             : 
     845             : namespace browsenodefactory
     846             : {
     847             : ::rtl::OUString SAL_CALL bnf_getImplementationName() ;
     848             : Reference< XInterface > SAL_CALL bnf_create( Reference< XComponentContext > const & xComponentContext );
     849             : Sequence< ::rtl::OUString > SAL_CALL bnf_getSupportedServiceNames();
     850             : }
     851             : 
     852             : namespace scripting_runtimemgr
     853             : {
     854             : //*************************************************************************
     855           1 : Reference< XInterface > SAL_CALL sp_create(
     856             :     const Reference< XComponentContext > & xCompC )
     857             : {
     858           1 :     return ( cppu::OWeakObject * ) new ::func_provider::MasterScriptProvider( xCompC );
     859             : }
     860             : 
     861             : //*************************************************************************
     862           1 : Sequence< ::rtl::OUString > sp_getSupportedServiceNames( )
     863             :     SAL_THROW(())
     864             : {
     865           4 :     ::rtl::OUString names[3];
     866             : 
     867           1 :     names[0] = ::rtl::OUString( "com.sun.star.script.provider.MasterScriptProvider"  );
     868           1 :     names[1] = ::rtl::OUString( "com.sun.star.script.browse.BrowseNode"  );
     869           1 :     names[2] = ::rtl::OUString( "com.sun.star.script.provider.ScriptProvider"  );
     870             : 
     871           4 :     return Sequence< ::rtl::OUString >( names, 3 );
     872             : }
     873             : 
     874             : //*************************************************************************
     875           2 : ::rtl::OUString sp_getImplementationName( )
     876             : SAL_THROW(())
     877             : {
     878           2 :     return ::rtl::OUString( "com.sun.star.script.provider.MasterScriptProvider"  );
     879             : }
     880             : 
     881             : // ***** registration or ScriptingFrameworkURIHelper
     882           0 : Reference< XInterface > SAL_CALL urihelper_create(
     883             :     const Reference< XComponentContext > & xCompC )
     884             : {
     885             :     return ( cppu::OWeakObject * )
     886           0 :         new ::func_provider::ScriptingFrameworkURIHelper( xCompC );
     887             : }
     888             : 
     889           0 : Sequence< ::rtl::OUString > urihelper_getSupportedServiceNames( )
     890             :     SAL_THROW(())
     891             : {
     892             :     ::rtl::OUString serviceNameList[] = {
     893             :         ::rtl::OUString(
     894           0 :             "com.sun.star.script.provider.ScriptURIHelper" ) };
     895             : 
     896             :     Sequence< ::rtl::OUString > serviceNames = Sequence <
     897           0 :         ::rtl::OUString > ( serviceNameList, 1 );
     898             : 
     899           0 :     return serviceNames;
     900             : }
     901             : 
     902           2 : ::rtl::OUString urihelper_getImplementationName( )
     903             :     SAL_THROW(())
     904             : {
     905             :     return ::rtl::OUString(
     906           2 :         "com.sun.star.script.provider.ScriptURIHelper");
     907             : }
     908             : 
     909             : static struct cppu::ImplementationEntry s_entries [] =
     910             :     {
     911             :         {
     912             :             sp_create, sp_getImplementationName,
     913             :             sp_getSupportedServiceNames, cppu::createSingleComponentFactory,
     914             :             0, 0
     915             :         },
     916             :         {
     917             :             urihelper_create,
     918             :             urihelper_getImplementationName,
     919             :             urihelper_getSupportedServiceNames,
     920             :             cppu::createSingleComponentFactory,
     921             :             0, 0
     922             :         },
     923             :         {
     924             :             func_provider::mspf_create, func_provider::mspf_getImplementationName,
     925             :             func_provider::mspf_getSupportedServiceNames, cppu::createSingleComponentFactory,
     926             :             0, 0
     927             :         },
     928             :         {
     929             :             browsenodefactory::bnf_create, browsenodefactory::bnf_getImplementationName,
     930             :             browsenodefactory::bnf_getSupportedServiceNames, cppu::createSingleComponentFactory,
     931             :             0, 0
     932             :         },
     933             :         { 0, 0, 0, 0, 0, 0 }
     934             :     };
     935             : }
     936             : 
     937             : //############################################################################
     938             : //#### EXPORTED ##############################################################
     939             : //############################################################################
     940             : 
     941             : extern "C"
     942             : {
     943             :     /**
     944             :      * This function is called to get service factories for an implementation.
     945             :      *
     946             :      * @param pImplName       name of implementation
     947             :      * @param pServiceManager a service manager, need for component creation
     948             :      * @param pRegistryKey    the registry key for this component, need for persistent
     949             :      *                        data
     950             :      * @return a component factory
     951             :      */
     952           2 :     SAL_DLLPUBLIC_EXPORT void * SAL_CALL scriptframe_component_getFactory(
     953             :         const sal_Char * pImplName,
     954             :         lang::XMultiServiceFactory * pServiceManager,
     955             :         registry::XRegistryKey * pRegistryKey )
     956             :     {
     957             :         return ::cppu::component_getFactoryHelper( pImplName, pServiceManager,
     958           2 :             pRegistryKey, ::scripting_runtimemgr::s_entries );
     959             :     }
     960             : }
     961             : 
     962             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10