29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
31#include <com/sun/star/uri/XUriReference.hpp>
32#include <com/sun/star/uri/UriReferenceFactory.hpp>
33#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
35#include <com/sun/star/deployment/XPackage.hpp>
36#include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
37#include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
38#include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
54static bool endsWith( std::u16string_view target, std::u16string_view item )
56 size_t index = target.find( item );
57 return index != std::u16string_view::npos &&
58 index == ( target.size() - item.size() );
67 m_xContext( xContext ), m_bIsValid( false ), m_bInitialised( false ),
72 ENSURE_OR_THROW(
m_xMgr.is(),
"MasterScriptProvider::MasterScriptProvider: No service manager available\n" );
89 sal_Int32 len =
args.getLength();
93 "MasterScriptProvider::initialize: invalid number of arguments" );
96 Sequence< Any > invokeArgs( len );
100 auto pinvokeArgs = invokeArgs.getArray();
107 pinvokeArgs[ 0 ] =
args[ 0 ];
127 Reference< XEmbeddedScripts > xScripts(
m_xModel, UNO_QUERY );
128 if ( !xScripts.is() )
130 throw lang::IllegalArgumentException(
131 "The given document does not support embedding scripts into it, and cannot be associated with such a document.",
143 Any aError( ::cppu::getCaughtException() );
146 aError >>= aException;
148 "MasterScriptProvider::initialize: caught " +
149 aError.getValueTypeName() +
152 throw lang::WrappedTargetException( buf, *
this, aError );
161 OUString pkgSpec =
"uno_packages";
162 sal_Int32 indexOfPkgSpec =
m_sCtxString.lastIndexOf( pkgSpec );
165 if ( indexOfPkgSpec > -1 &&
m_sCtxString.match( pkgSpec, indexOfPkgSpec ) )
177 invokeArgs = Sequence< Any >( 0 );
198 Reference< provider::XScriptProviderFactory > xFac =
199 provider::theMasterScriptProviderFactory::get(
m_xContext );
202 xFac->createScriptProvider( location ), UNO_SET_THROW );
207 TOOLS_WARN_EXCEPTION(
"scripting.provider",
"Exception creating MasterScriptProvider for uno_packages in context "
213Reference< provider::XScript >
218 throw provider::ScriptFrameworkErrorException(
219 "MasterScriptProvider not initialised", Reference< XInterface >(),
221 provider::ScriptFrameworkErrorType::UNKNOWN );
228 Reference< uri::XUriReference > uriRef = xFac->parse( scriptURI );
230 Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY );
232 if ( !uriRef.is() || !sfUri.is() )
234 throw provider::ScriptFrameworkErrorException(
235 "Incorrect format for Script URI: " + scriptURI,
236 Reference< XInterface >(),
238 provider::ScriptFrameworkErrorType::UNKNOWN );
241 OUString langKey(
"language");
242 OUString locKey(
"location");
244 if ( !sfUri->hasParameter( langKey ) ||
245 !sfUri->hasParameter( locKey ) ||
246 ( sfUri->getName().isEmpty() ) )
248 throw provider::ScriptFrameworkErrorException(
249 "Incorrect format for Script URI: " + scriptURI,
250 Reference< XInterface >(),
252 provider::ScriptFrameworkErrorType::UNKNOWN );
255 OUString language = sfUri->getParameter( langKey );
256 OUString location = sfUri->getParameter( locKey );
259 sal_Int32
index = -1;
260 OUString pkgTag(
":uno_packages");
274 if ( (
index = location.indexOf( pkgTag ) ) > -1 )
276 location = location.copy( 0,
index + pkgTag.getLength() );
279 Reference< provider::XScript > xScript;
286 if ( ( location ==
"document"
290 || ( language ==
"Basic" )
293 Reference< provider::XScriptProvider > xScriptProvider;
294 OUString serviceName =
"com.sun.star.script.provider.ScriptProviderFor" + language;
297 throw provider::ScriptFrameworkErrorException(
298 "No LanguageProviders detected",
299 Reference< XInterface >(),
300 sfUri->getName(), language,
301 provider::ScriptFrameworkErrorType::NOTSUPPORTED );
312 throw provider::ScriptFrameworkErrorException(
313 e.Message, Reference< XInterface >(),
314 sfUri->getName(), language,
315 provider::ScriptFrameworkErrorType::NOTSUPPORTED );
318 xScript=xScriptProvider->getScript( scriptURI );
322 Reference< provider::XScriptProviderFactory > xFac_ =
323 provider::theMasterScriptProviderFactory::get(
m_xContext );
325 Reference< provider::XScriptProvider > xSP(
326 xFac_->createScriptProvider(
Any( location ) ), UNO_SET_THROW );
327 xScript = xSP->getScript( scriptURI );
339 std::scoped_lock aGuard(
m_mutex );
342 Sequence<OUString> denylist {
"com.sun.star.script.provider.ScriptProviderForBasic" };
364 if ( sCtx.startsWith(
"vnd.sun.star.tdoc" ) )
369 xModel = MiscUtils::tDocUrlToModel( sCtx );
387Sequence< Reference< browse::XBrowseNode > > SAL_CALL
392 sal_Int32
size = providers.getLength();
398 Sequence< Reference< browse::XBrowseNode > > children(
size );
399 auto childrenRange = asNonConstRange(children);
400 sal_Int32 provIndex = 0;
401 for ( ; provIndex < providers.getLength(); provIndex++ )
403 childrenRange[ provIndex ].set( providers[ provIndex ], UNO_QUERY );
408 childrenRange[ provIndex ].set(
m_xMSPPkg, UNO_QUERY );
426 return browse::BrowseNodeTypes::CONTAINER;
435 OUString temp = location;
444template <
typename Proc>
bool FindProviderAndApply(
ProviderCache& rCache, Proc p)
446 auto pass = [&rCache, &
p]() ->
bool
448 bool bResult =
false;
449 const Sequence<Reference<provider::XScriptProvider>> aAllProviders = rCache.
getAllProviders();
450 for (
const auto& rProv : aAllProviders)
452 Reference<container::XNameContainer> xCont(rProv, UNO_QUERY);
463 catch (
const Exception&)
470 bool bSuccess =
false;
499 xCont->insertByName(
aName, aElement );
503 Reference< deployment::XPackage > xPkg( aElement, UNO_QUERY );
506 throw lang::IllegalArgumentException(
"Couldn't convert to XPackage",
507 Reference < XInterface > (), 2 );
509 if (
aName.isEmpty() )
511 throw lang::IllegalArgumentException(
"Name not set!!",
512 Reference < XInterface > (), 1 );
517 const bool bSuccess = FindProviderAndApply(
519 xCont->insertByName(
aName, aElement);
525 throw lang::IllegalArgumentException(
"Failed to register package for " +
aName,
526 Reference < XInterface > (), 2 );
544 xCont->removeByName(
Name );
548 if (
Name.isEmpty() )
550 throw lang::IllegalArgumentException(
"Name not set!!",
551 Reference < XInterface > (), 1 );
556 const bool bSuccess = FindProviderAndApply(
558 xCont->removeByName(
Name);
564 throw lang::IllegalArgumentException(
"Failed to revoke package for " +
Name,
565 Reference < XInterface > (), 1 );
607 if (
aName.isEmpty() )
609 throw lang::IllegalArgumentException(
"Name not set!!",
610 Reference < XInterface > (), 1 );
616 result = FindProviderAndApply(
618 return xCont->hasByName(
aName);
649 return "com.sun.star.script.provider.MasterScriptProvider";
661 "com.sun.star.script.provider.MasterScriptProvider",
662 "com.sun.star.script.browse.BrowseNode",
663 "com.sun.star.script.provider.ScriptProvider" };
666extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
668 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const&)
Reference< XComponentContext > m_xContext
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Int16 SAL_CALL getType() override
css::uno::Sequence< css::uno::Any > m_sAargs
virtual OUString SAL_CALL getName() override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &args) override
XInitialise implementation.
css::uno::Reference< css::document::XScriptInvocationContext > m_xInvocationContext
ProviderCache * providerCache()
virtual css::uno::Sequence< css::uno::Reference< css::script::browse::XBrowseNode > > SAL_CALL getChildNodes() override
virtual sal_Bool SAL_CALL hasElements() override
virtual void SAL_CALL insertByName(const OUString &aName, const css::uno::Any &aElement) override
css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual void SAL_CALL removeByName(const OUString &Name) override
virtual ~MasterScriptProvider() override
virtual css::uno::Type SAL_CALL getElementType() override
std::unique_ptr< ProviderCache > m_pPCache
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
MasterScriptProvider(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual css::uno::Reference< css::script::provider::XScript > SAL_CALL getScript(const OUString &scriptURI) override
virtual OUString SAL_CALL getImplementationName() override
static OUString parseLocationName(const OUString &location)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL replaceByName(const OUString &aName, const css::uno::Any &aElement) override
css::uno::Reference< css::frame::XModel > m_xModel
virtual sal_Bool SAL_CALL hasChildNodes() override
const OUString & getContextString() const
css::uno::Reference< css::script::provider::XScriptProvider > m_xMSPPkg
css::uno::Sequence< css::uno::Reference< css::script::provider::XScriptProvider > > getAllProviders()
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ENSURE_OR_THROW(c, m)
#define TOOLS_INFO_EXCEPTION(area, stream)
css::uno::Reference< css::uno::XCurrentContext > NoEnableJavaInteractionContext()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
static bool endsWith(std::u16string_view target, std::u16string_view item)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * scripting_MasterScriptProvider_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Reference< XModel > xModel