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