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