diff options
Diffstat (limited to 'scripting/source/provider')
19 files changed, 4010 insertions, 0 deletions
diff --git a/scripting/source/provider/ActiveMSPList.cxx b/scripting/source/provider/ActiveMSPList.cxx new file mode 100644 index 000000000000..3c6206d8d051 --- /dev/null +++ b/scripting/source/provider/ActiveMSPList.cxx @@ -0,0 +1,324 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/factory.hxx> +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <util/scriptingconstants.hxx> +#include <util/util.hxx> +#include <util/MiscUtils.hxx> + +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/util/XMacroExpander.hpp> +#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp> + +#include <com/sun/star/script/browse/BrowseNodeTypes.hpp> + +#include "MasterScriptProvider.hxx" +#include "ActiveMSPList.hxx" + +#include <tools/diagnose_ex.h> + +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::script; +using namespace ::scripting_util; +using namespace ::sf_misc; + +namespace func_provider +{ + +ActiveMSPList::ActiveMSPList( const Reference< XComponentContext > & xContext ) : m_xContext( xContext ) +{ + userDirString = ::rtl::OUString::createFromAscii("user"); + shareDirString = ::rtl::OUString::createFromAscii("share"); + bundledDirString = ::rtl::OUString::createFromAscii("bundled"); +} + +ActiveMSPList::~ActiveMSPList() +{ +} + +Reference< provider::XScriptProvider > +ActiveMSPList::createNewMSP( const uno::Any& context ) +{ + ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider"); + Sequence< Any > args( &context, 1 ); + + Reference< provider::XScriptProvider > msp( + m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( + serviceName, args, m_xContext ), UNO_QUERY ); + return msp; +} + +Reference< provider::XScriptProvider > +ActiveMSPList::getMSPFromAnyContext( const Any& aContext ) + SAL_THROW(( lang::IllegalArgumentException, RuntimeException )) +{ + Reference< provider::XScriptProvider > msp; + ::rtl::OUString sContext; + if ( aContext >>= sContext ) + { + msp = getMSPFromStringContext( sContext ); + return msp; + } + + Reference< frame::XModel > xModel( aContext, UNO_QUERY ); + + Reference< document::XScriptInvocationContext > xScriptContext( aContext, UNO_QUERY ); + if ( xScriptContext.is() ) + { + try + { + // the component supports executing scripts embedded in a - possibly foreign document. + // Check whether this other document its the component itself. + if ( !xModel.is() || ( xModel != xScriptContext->getScriptContainer() ) ) + { + msp = getMSPFromInvocationContext( xScriptContext ); + return msp; + } + } + catch( const lang::IllegalArgumentException& ) + { + xModel.set( Reference< frame::XModel >() ); + } + } + + if ( xModel.is() ) + { + sContext = MiscUtils::xModelToTdocUrl( xModel, m_xContext ); + msp = getMSPFromStringContext( sContext ); + return msp; + } + + createNonDocMSPs(); + return m_hMsps[ shareDirString ]; +} + +Reference< provider::XScriptProvider > + ActiveMSPList::getMSPFromInvocationContext( const Reference< document::XScriptInvocationContext >& xContext ) + SAL_THROW(( lang::IllegalArgumentException, RuntimeException )) +{ + Reference< provider::XScriptProvider > msp; + + Reference< document::XEmbeddedScripts > xScripts; + if ( xContext.is() ) + xScripts.set( xContext->getScriptContainer() ); + if ( !xScripts.is() ) + { + ::rtl::OUStringBuffer buf; + buf.appendAscii( "Failed to create MasterScriptProvider for ScriptInvocationContext: " ); + buf.appendAscii( "Component supporting XEmbeddScripts interface not found." ); + throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 ); + } + + ::osl::MutexGuard guard( m_mutex ); + + Reference< XInterface > xNormalized( xContext, UNO_QUERY ); + ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized ); + if ( pos == m_mScriptComponents.end() ) + { + // TODO + msp = createNewMSP( uno::makeAny( xContext ) ); + addActiveMSP( xNormalized, msp ); + } + else + { + msp = pos->second; + } + + return msp; +} + +Reference< provider::XScriptProvider > + ActiveMSPList::getMSPFromStringContext( const ::rtl::OUString& context ) + SAL_THROW(( lang::IllegalArgumentException, RuntimeException )) +{ + Reference< provider::XScriptProvider > msp; + try + { + if ( context.indexOf( OUSTR( "vnd.sun.star.tdoc" ) ) == 0 ) + { + Reference< frame::XModel > xModel( MiscUtils::tDocUrlToModel( context ) ); + + Reference< document::XEmbeddedScripts > xScripts( xModel, UNO_QUERY ); + Reference< document::XScriptInvocationContext > xScriptsContext( xModel, UNO_QUERY ); + if ( !xScripts.is() && !xScriptsContext.is() ) + { + ::rtl::OUStringBuffer buf; + buf.appendAscii( "Failed to create MasterScriptProvider for '" ); + buf.append ( context ); + buf.appendAscii( "': Either XEmbeddScripts or XScriptInvocationContext need to be supported by the document." ); + throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 ); + } + + ::osl::MutexGuard guard( m_mutex ); + Reference< XInterface > xNormalized( xModel, UNO_QUERY ); + ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized ); + if ( pos == m_mScriptComponents.end() ) + { + msp = createNewMSP( context ); + addActiveMSP( xNormalized, msp ); + } + else + { + msp = pos->second; + } + } + else + { + ::osl::MutexGuard guard( m_mutex ); + Msp_hash::iterator h_itEnd = m_hMsps.end(); + Msp_hash::const_iterator itr = m_hMsps.find( context ); + if ( itr == h_itEnd ) + { + msp = createNewMSP( context ); + m_hMsps[ context ] = msp; + } + else + { + msp = m_hMsps[ context ]; + } + } + } + catch( const lang::IllegalArgumentException& ) + { + // allowed to leave + } + catch( const RuntimeException& ) + { + // allowed to leave + } + catch( const Exception& ) + { + ::rtl::OUStringBuffer aMessage; + aMessage.appendAscii( "Failed to create MasterScriptProvider for context '" ); + aMessage.append ( context ); + aMessage.appendAscii( "'." ); + throw lang::WrappedTargetRuntimeException( + aMessage.makeStringAndClear(), *this, ::cppu::getCaughtException() ); + } + return msp; +} + +void +ActiveMSPList::addActiveMSP( const Reference< uno::XInterface >& xComponent, + const Reference< provider::XScriptProvider >& msp ) +{ + ::osl::MutexGuard guard( m_mutex ); + Reference< XInterface > xNormalized( xComponent, UNO_QUERY ); + ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized ); + if ( pos == m_mScriptComponents.end() ) + { + m_mScriptComponents[ xNormalized ] = msp; + + // add self as listener for component disposal + // should probably throw from this method!!, reexamine + try + { + Reference< lang::XComponent > xBroadcaster = + Reference< lang::XComponent >( xComponent, UNO_QUERY_THROW ); + xBroadcaster->addEventListener( this ); + } + catch ( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + } +} + +//************************************************************************* +void SAL_CALL +ActiveMSPList::disposing( const ::com::sun::star::lang::EventObject& Source ) +throw ( ::com::sun::star::uno::RuntimeException ) + +{ + try + { + Reference< XInterface > xNormalized( Source.Source, UNO_QUERY ); + if ( xNormalized.is() ) + { + ::osl::MutexGuard guard( m_mutex ); + ScriptComponent_map::iterator pos = m_mScriptComponents.find( xNormalized ); + if ( pos != m_mScriptComponents.end() ) + m_mScriptComponents.erase( pos ); + } + } + catch ( const Exception& ) + { + // if we get an exception here, there is not much we can do about + // it can't throw as it will screw up the model that is calling dispose + DBG_UNHANDLED_EXCEPTION(); + } +} + + +void +ActiveMSPList::createNonDocMSPs() +{ + static bool created = false; + if ( created ) + { + return; + } + else + { + ::osl::MutexGuard guard( m_mutex ); + if ( created ) + { + return; + } + // do creation of user and share MSPs here + ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider"); + Sequence< Any > args(1); + + args[ 0 ] <<= userDirString; + Reference< provider::XScriptProvider > userMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY ); + // should check if provider reference is valid + m_hMsps[ userDirString ] = userMsp; + + args[ 0 ] <<= shareDirString; + Reference< provider::XScriptProvider > shareMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY ); + // should check if provider reference is valid + m_hMsps[ shareDirString ] = shareMsp; + + args[ 0 ] <<= bundledDirString; + Reference< provider::XScriptProvider > bundledMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY ); + // should check if provider reference is valid + m_hMsps[ bundledDirString ] = bundledMsp; + + created = true; + } + +} + + +} // namespace func_provider + diff --git a/scripting/source/provider/ActiveMSPList.hxx b/scripting/source/provider/ActiveMSPList.hxx new file mode 100644 index 000000000000..b127f3c41b16 --- /dev/null +++ b/scripting/source/provider/ActiveMSPList.hxx @@ -0,0 +1,114 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#ifndef _FRAMEWORK_SCRIPT_PROVIDER_OPENDOCUMENTLIST_HXX_ +#define _FRAMEWORK_SCRIPT_PROVIDER_OPENDOCUMENTLIST_HXX_ + +#include <hash_map> +#include <map> + +#include <osl/mutex.hxx> +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> + +#include <com/sun/star/lang/XEventListener.hpp> + +#include <com/sun/star/script/provider/XScriptProvider.hpp> +#include <com/sun/star/script/browse/XBrowseNode.hpp> +#include <com/sun/star/document/XScriptInvocationContext.hpp> + +#include <comphelper/stl_types.hxx> + +namespace func_provider +{ +// for simplification +#define css ::com::sun::star + +//Typedefs +//============================================================================= + + +typedef ::std::map < css::uno::Reference< css::uno::XInterface > + , css::uno::Reference< css::script::provider::XScriptProvider > + , ::comphelper::OInterfaceCompare< css::uno::XInterface > + > ScriptComponent_map; + +typedef ::std::hash_map< ::rtl::OUString, + css::uno::Reference< css::script::provider::XScriptProvider >, + ::rtl::OUStringHash, + ::std::equal_to< ::rtl::OUString > > Msp_hash; + +class ActiveMSPList : public ::cppu::WeakImplHelper1< css::lang::XEventListener > +{ + +public: + + ActiveMSPList( const css::uno::Reference< + css::uno::XComponentContext > & xContext ); + ~ActiveMSPList(); + + css::uno::Reference< css::script::provider::XScriptProvider > + getMSPFromStringContext( const ::rtl::OUString& context ) + SAL_THROW(( css::lang::IllegalArgumentException, css::uno::RuntimeException )); + + css::uno::Reference< css::script::provider::XScriptProvider > + getMSPFromAnyContext( const css::uno::Any& context ) + SAL_THROW(( css::lang::IllegalArgumentException, css::uno::RuntimeException )); + + css::uno::Reference< css::script::provider::XScriptProvider > + getMSPFromInvocationContext( const css::uno::Reference< css::document::XScriptInvocationContext >& context ) + SAL_THROW(( css::lang::IllegalArgumentException, css::uno::RuntimeException )); + + //XEventListener + //====================================================================== + + virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) + throw ( css::uno::RuntimeException ); + +private: + void addActiveMSP( const css::uno::Reference< css::uno::XInterface >& xComponent, + const css::uno::Reference< css::script::provider::XScriptProvider >& msp ); + css::uno::Reference< css::script::provider::XScriptProvider > + createNewMSP( const css::uno::Any& context ); + css::uno::Reference< css::script::provider::XScriptProvider > + createNewMSP( const ::rtl::OUString& context ) + { + return createNewMSP( css::uno::makeAny( context ) ); + } + + void createNonDocMSPs(); + Msp_hash m_hMsps; + ScriptComponent_map m_mScriptComponents; + osl::Mutex m_mutex; + ::rtl::OUString userDirString; + ::rtl::OUString shareDirString; + ::rtl::OUString bundledDirString; + css::uno::Reference< css::uno::XComponentContext > m_xContext; +}; +} // func_provider +#endif diff --git a/scripting/source/provider/BrowseNodeFactoryImpl.cxx b/scripting/source/provider/BrowseNodeFactoryImpl.cxx new file mode 100644 index 000000000000..763b1d739d37 --- /dev/null +++ b/scripting/source/provider/BrowseNodeFactoryImpl.cxx @@ -0,0 +1,797 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" + +#include <cppuhelper/weakref.hxx> +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/factory.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <cppuhelper/implbase1.hxx> +#include <comphelper/mediadescriptor.hxx> + +#include <com/sun/star/lang/XMultiComponentFactory.hpp> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/reflection/XProxyFactory.hpp> + +#include <com/sun/star/script/provider/XScriptProviderFactory.hpp> +#include <com/sun/star/script/browse/BrowseNodeFactoryViewTypes.hpp> +#include <com/sun/star/document/XScriptInvocationContext.hpp> + +#include <tools/diagnose_ex.h> + +#include "BrowseNodeFactoryImpl.hxx" +#include "ActiveMSPList.hxx" +#include <util/MiscUtils.hxx> +#include <util/util.hxx> + +#include <vector> +#include <algorithm> +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::script; +using namespace ::sf_misc; + +namespace browsenodefactory +{ +class BrowseNodeAggregator : + public ::cppu::WeakImplHelper1< browse::XBrowseNode > +{ +private: + ::rtl::OUString m_Name; + Sequence< Reference< browse::XBrowseNode > > m_Nodes; + +public: + + BrowseNodeAggregator( const Reference< browse::XBrowseNode >& node ) + { + m_Name = node->getName(); + m_Nodes.realloc( 1 ); + m_Nodes[ 0 ] = node; + } + + ~BrowseNodeAggregator() + { + } + + void addBrowseNode( const Reference< browse::XBrowseNode>& node ) + { + sal_Int32 index = m_Nodes.getLength(); + + m_Nodes.realloc( index + 1 ); + m_Nodes[ index ] = node; + } + + virtual ::rtl::OUString + SAL_CALL getName() + throw ( RuntimeException ) + { + return m_Name; + } + + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL + getChildNodes() + throw ( RuntimeException ) + { + std::vector< Sequence< Reference < browse::XBrowseNode > > > seqs; + seqs.reserve( m_Nodes.getLength() ); + + sal_Int32 numChildren = 0; + + for ( sal_Int32 i = 0; i < m_Nodes.getLength(); i++ ) + { + Sequence< Reference < browse::XBrowseNode > > childs; + try + { + childs = m_Nodes[ i ]->getChildNodes(); + seqs.push_back( childs ); + numChildren += childs.getLength(); + } + catch ( Exception& ) + { + // some form of exception getting child nodes so they + // won't be displayed + } + } + + std::vector< Sequence< Reference < browse::XBrowseNode > > >::const_iterator it = seqs.begin(); + std::vector< Sequence< Reference < browse::XBrowseNode > > >::const_iterator it_end = seqs.end(); + + Sequence< Reference < browse::XBrowseNode > > result( numChildren ); + for ( sal_Int32 index = 0; it != it_end && index < numChildren ; ++it ) + { + Sequence< Reference < browse::XBrowseNode > > childs = *it; + for ( sal_Int32 j = 0; j < childs.getLength(); j++ ) + { + result[ index++ ] = childs[ j ]; + } + } + return result; + } + + virtual sal_Bool SAL_CALL + hasChildNodes() + throw ( RuntimeException ) + { + if ( m_Nodes.getLength() != 0 ) + { + for ( sal_Int32 i = 0 ; i < m_Nodes.getLength(); i++ ) + { + try + { + if ( m_Nodes[ i ]->hasChildNodes() ) + { + return sal_True; + } + } + catch ( Exception& ) + { + // some form of exception getting child nodes so move + // on to the next one + } + } + } + + return sal_False; + } + + virtual sal_Int16 SAL_CALL getType() + throw ( RuntimeException ) + { + return browse::BrowseNodeTypes::CONTAINER; + } +}; + + +//typedef ::std::map< ::rtl::OUString, Reference< browse::XBrowseNode > > +typedef ::std::hash_map< ::rtl::OUString, Reference< browse::XBrowseNode >, + ::rtl::OUStringHash, ::std::equal_to< ::rtl::OUString > > + BrowseNodeAggregatorHash; +typedef ::std::vector< ::rtl::OUString > vString; + + +struct alphaSort +{ + bool operator()( const ::rtl::OUString& a, const ::rtl::OUString& b ) + { + return a.compareTo( b ) < 0; + } +}; +class LocationBrowseNode : + public ::cppu::WeakImplHelper1< browse::XBrowseNode > +{ +private: + BrowseNodeAggregatorHash* m_hBNA; + vString m_vStr; + ::rtl::OUString m_sNodeName; + Reference< browse::XBrowseNode > m_origNode; + +public: + + LocationBrowseNode( const Reference< browse::XBrowseNode >& node ) + { + m_sNodeName = node->getName(); + m_hBNA = NULL; + m_origNode.set( node ); + } + + ~LocationBrowseNode() + { + if (m_hBNA) + { + delete m_hBNA; + } + } + + // ------------------------------------------------------------------------- + // XBrowseNode + // ------------------------------------------------------------------------- + + virtual ::rtl::OUString SAL_CALL getName() + throw ( RuntimeException ) + { + return m_sNodeName; + } + + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL + getChildNodes() + throw ( RuntimeException ) + { + if ( m_hBNA == NULL ) + { + loadChildNodes(); + } + + Sequence< Reference< browse::XBrowseNode > > children( m_hBNA->size() ); + sal_Int32 index = 0; + + vString::const_iterator it = m_vStr.begin(); + + for ( ; it != m_vStr.end(); ++it, index++ ) + { + children[ index ].set( m_hBNA->find( *it )->second ); + } + + return children; + } + + virtual sal_Bool SAL_CALL hasChildNodes() + throw ( RuntimeException ) + { + return sal_True; + } + + virtual sal_Int16 SAL_CALL getType() + throw ( RuntimeException ) + { + return browse::BrowseNodeTypes::CONTAINER; + } + +private: + + void loadChildNodes() + { + m_hBNA = new BrowseNodeAggregatorHash(); + + Sequence< Reference< browse::XBrowseNode > > langNodes = + m_origNode->getChildNodes(); + + for ( sal_Int32 i = 0; i < langNodes.getLength(); i++ ) + { + Reference< browse::XBrowseNode > xbn; + if ( langNodes[ i ]->getName().equals(::rtl::OUString::createFromAscii("uno_packages")) ) + { + xbn.set( new LocationBrowseNode( langNodes[ i ] ) ); + } + else + { + xbn.set( langNodes[ i ] ); + } + + Sequence< Reference< browse::XBrowseNode > > grandchildren = + xbn->getChildNodes(); + + for ( sal_Int32 j = 0; j < grandchildren.getLength(); j++ ) + { + Reference< browse::XBrowseNode > grandchild(grandchildren[j]); + + BrowseNodeAggregatorHash::iterator h_it = + m_hBNA->find( grandchild->getName() ); + + if ( h_it != m_hBNA->end() ) + { + BrowseNodeAggregator* bna = static_cast< BrowseNodeAggregator* >( h_it->second.get() ); + bna->addBrowseNode( grandchild ); + } + else + { + Reference< browse::XBrowseNode > bna( + new BrowseNodeAggregator( grandchild ) ); + (*m_hBNA)[ grandchild->getName() ].set( bna ); + m_vStr.push_back( grandchild->getName() ); + } + } + } + // sort children alpahbetically + ::std::sort( m_vStr.begin(), m_vStr.end(), alphaSort() ); + } +}; + +namespace +{ + +Sequence< Reference< browse::XBrowseNode > > getAllBrowseNodes( const Reference< XComponentContext >& xCtx ) +{ + Reference< lang::XMultiComponentFactory > mcf = + xCtx->getServiceManager(); + + Sequence< ::rtl::OUString > openDocs = + MiscUtils::allOpenTDocUrls( xCtx ); + + Reference< provider::XScriptProviderFactory > xFac; + sal_Int32 initialSize = openDocs.getLength() + 2; + sal_Int32 mspIndex = 0; + + Sequence < Reference < browse::XBrowseNode > > locnBNs( initialSize ); + try + { + xFac.set( + xCtx->getValueByName( + OUSTR("/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW ); + + locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >( xFac->createScriptProvider( makeAny( ::rtl::OUString::createFromAscii("user") ) ), UNO_QUERY_THROW ); + locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >( xFac->createScriptProvider( makeAny( ::rtl::OUString::createFromAscii("share") ) ), UNO_QUERY_THROW ); + } + // TODO proper exception handling, should throw + catch( Exception& e ) + { + (void)e; + OSL_TRACE("Caught Exception %s", + ::rtl::OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).pData->buffer ); + locnBNs.realloc( mspIndex ); + return locnBNs; + } + + for ( sal_Int32 i = 0; i < openDocs.getLength(); i++ ) + { + try + { + Reference< frame::XModel > model( MiscUtils::tDocUrlToModel( openDocs[ i ] ), UNO_QUERY_THROW ); + + // #i44599 Check if it's a real document or something special like Hidden/Preview + css::uno::Reference< css::frame::XController > xCurrentController = model->getCurrentController(); + if( xCurrentController.is() ) + { + comphelper::MediaDescriptor aMD( model->getArgs() ); + sal_Bool bDefault = false; + sal_Bool bHidden = aMD.getUnpackedValueOrDefault( comphelper::MediaDescriptor::PROP_HIDDEN(), bDefault ); + sal_Bool bPreview = aMD.getUnpackedValueOrDefault( comphelper::MediaDescriptor::PROP_PREVIEW(), bDefault ); + if( !bHidden && !bPreview ) + { + Reference< document::XEmbeddedScripts > xScripts( model, UNO_QUERY ); + if ( xScripts.is() ) + locnBNs[ mspIndex++ ] = Reference< browse::XBrowseNode >( + xFac->createScriptProvider( makeAny( model ) ), UNO_QUERY_THROW ); + } + } + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + + } + + Sequence < Reference < browse::XBrowseNode > > locnBNs_Return( mspIndex ); + for ( sal_Int32 j = 0; j < mspIndex; j++ ) + locnBNs_Return[j] = locnBNs[j]; + + return locnBNs_Return; +} + +} // namespace + +typedef ::std::vector< Reference< browse::XBrowseNode > > vXBrowseNodes; + +struct alphaSortForBNodes +{ + bool operator()( const Reference< browse::XBrowseNode >& a, const Reference< browse::XBrowseNode >& b ) + { + return a->getName().compareTo( b->getName() ) < 0; + } +}; + +typedef ::cppu::WeakImplHelper1< browse::XBrowseNode > t_BrowseNodeBase; +class DefaultBrowseNode : + public t_BrowseNodeBase +{ + +private: + Reference< browse::XBrowseNode > m_xWrappedBrowseNode; + Reference< lang::XTypeProvider > m_xWrappedTypeProv; + Reference< XAggregation > m_xAggProxy; + Reference< XComponentContext > m_xCtx; + + DefaultBrowseNode(); +public: + DefaultBrowseNode( const Reference< XComponentContext >& xCtx, const Reference< browse::XBrowseNode>& xNode ) : m_xWrappedBrowseNode( xNode ), m_xWrappedTypeProv( xNode, UNO_QUERY ), m_xCtx( xCtx, UNO_QUERY ) + { + OSL_ENSURE( m_xWrappedBrowseNode.is(), "DefaultBrowseNode::DefaultBrowseNode(): No BrowseNode to wrap" ); + OSL_ENSURE( m_xWrappedTypeProv.is(), "DefaultBrowseNode::DefaultBrowseNode(): No BrowseNode to wrap" ); + OSL_ENSURE( m_xCtx.is(), "DefaultBrowseNode::DefaultBrowseNode(): No ComponentContext" ); + // Use proxy factory service to create aggregatable proxy. + try + { + Reference< lang::XMultiComponentFactory > xMFac( m_xCtx->getServiceManager(), UNO_QUERY_THROW ); + Reference< reflection::XProxyFactory > xProxyFac( + xMFac->createInstanceWithContext( + rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.reflection.ProxyFactory" ) ), + m_xCtx ), UNO_QUERY_THROW ); + m_xAggProxy = xProxyFac->createProxy( m_xWrappedBrowseNode ); + } + catch( uno::Exception& ) + { + OSL_ENSURE( false, "DefaultBrowseNode::DefaultBrowseNode: Caught exception!" ); + } + OSL_ENSURE( m_xAggProxy.is(), + "DefaultBrowseNode::DefaultBrowseNode: Wrapped BrowseNode cannot be aggregated!" ); + + if ( m_xAggProxy.is() ) + { + osl_incrementInterlockedCount( &m_refCount ); + + /* i35609 - Fix crash on Solaris. The setDelegator call needs + to be in its own block to ensure that all temporary Reference + instances that are acquired during the call are released + before m_refCount is decremented again */ + { + m_xAggProxy->setDelegator( + static_cast< cppu::OWeakObject * >( this ) ); + } + + osl_decrementInterlockedCount( &m_refCount ); + } + } + + ~DefaultBrowseNode() + { + if ( m_xAggProxy.is() ) + { + m_xAggProxy->setDelegator( uno::Reference< uno::XInterface >() ); + } + } + + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL + getChildNodes() + throw ( RuntimeException ) + { + if ( hasChildNodes() ) + { + vXBrowseNodes m_vNodes; + Sequence < Reference< browse::XBrowseNode > > nodes = + m_xWrappedBrowseNode->getChildNodes(); + for ( sal_Int32 i=0; i<nodes.getLength(); i++ ) + { + Reference< browse::XBrowseNode > xBrowseNode = nodes[ i ]; + OSL_ENSURE( xBrowseNode.is(), "DefaultBrowseNode::getChildNodes(): Invalid BrowseNode" ); + if( xBrowseNode.is() ) + m_vNodes.push_back( new DefaultBrowseNode( m_xCtx, xBrowseNode ) ); + } + + ::std::sort( m_vNodes.begin(), m_vNodes.end(), alphaSortForBNodes() ); + Sequence < Reference< browse::XBrowseNode > > children( m_vNodes.size() ); + vXBrowseNodes::const_iterator it = m_vNodes.begin(); + for ( sal_Int32 i=0; it != m_vNodes.end() && i<children.getLength(); i++, ++it ) + { + children[ i ].set( *it ); + } + return children; + } + else + { + // no nodes + + Sequence < Reference< browse::XBrowseNode > > none; + return none; + } + } + + virtual sal_Int16 SAL_CALL getType() + throw ( RuntimeException ) + { + return m_xWrappedBrowseNode->getType(); + } + + virtual ::rtl::OUString + SAL_CALL getName() + throw ( RuntimeException ) + { + return m_xWrappedBrowseNode->getName(); + } + + virtual sal_Bool SAL_CALL + hasChildNodes() + throw ( RuntimeException ) + { + return m_xWrappedBrowseNode->hasChildNodes(); + } + + // XInterface + virtual Any SAL_CALL queryInterface( const Type& aType ) + throw ( com::sun::star::uno::RuntimeException ) + { + Any aRet = t_BrowseNodeBase::queryInterface( aType ); + if ( aRet.hasValue() ) + { + return aRet; + } + if ( m_xAggProxy.is() ) + { + return m_xAggProxy->queryAggregation( aType ); + } + else + { + return Any(); + } + } + + virtual void SAL_CALL acquire() + throw () + + { + osl_incrementInterlockedCount( &m_refCount ); + } + virtual void SAL_CALL release() + throw () + { + if ( osl_decrementInterlockedCount( &m_refCount ) == 0 ) + { + delete this; + } + } + // XTypeProvider (implemnented by base, but needs to be overridden for + // delegating to aggregate) + virtual Sequence< Type > SAL_CALL getTypes() + throw ( com::sun::star::uno::RuntimeException ) + { + return m_xWrappedTypeProv->getTypes(); + } + virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() + throw ( com::sun::star::uno::RuntimeException ) + { + return m_xWrappedTypeProv->getImplementationId(); + + } +}; + +class DefaultRootBrowseNode : + public ::cppu::WeakImplHelper1< browse::XBrowseNode > +{ + +private: + vXBrowseNodes m_vNodes; + ::rtl::OUString m_Name; + + DefaultRootBrowseNode(); +public: + DefaultRootBrowseNode( const Reference< XComponentContext >& xCtx ) + { + Sequence < Reference< browse::XBrowseNode > > nodes = + getAllBrowseNodes( xCtx ); + + for ( sal_Int32 i=0; i<nodes.getLength(); i++ ) + { + m_vNodes.push_back( new DefaultBrowseNode( xCtx, nodes[ i ] ) ); + } + m_Name = ::rtl::OUString::createFromAscii( "Root" ); + } + + ~DefaultRootBrowseNode() + { + } + + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL + getChildNodes() + throw ( RuntimeException ) + { + // no need to sort user, share, doc1...docN + //::std::sort( m_vNodes.begin(), m_vNodes.end(), alphaSortForBNodes() ); + Sequence < Reference< browse::XBrowseNode > > children( m_vNodes.size() ); + vXBrowseNodes::const_iterator it = m_vNodes.begin(); + for ( sal_Int32 i=0; it != m_vNodes.end() && i<children.getLength(); i++, ++it ) + { + children[ i ].set( *it ); + } + return children; + } + + virtual sal_Int16 SAL_CALL getType() + throw ( RuntimeException ) + { + return browse::BrowseNodeTypes::ROOT; + } + + virtual ::rtl::OUString + SAL_CALL getName() + throw ( RuntimeException ) + { + return m_Name; + } + + virtual sal_Bool SAL_CALL + hasChildNodes() + throw ( RuntimeException ) + { + sal_Bool result = sal_True; + if ( !m_vNodes.size() ) + { + result = sal_False; + } + return result; + } +}; + + +class SelectorBrowseNode : + public ::cppu::WeakImplHelper1< browse::XBrowseNode > +{ +private: + Reference< XComponentContext > m_xComponentContext; + +public: + SelectorBrowseNode( const Reference< XComponentContext >& xContext ) + : m_xComponentContext( xContext ) + { + } + + ~SelectorBrowseNode() + { + } + + virtual ::rtl::OUString SAL_CALL getName() + throw ( RuntimeException ) + { + return ::rtl::OUString::createFromAscii( "Root" ); + } + + virtual Sequence< Reference< browse::XBrowseNode > > SAL_CALL + getChildNodes() + throw ( RuntimeException ) + { + + Sequence < Reference < browse::XBrowseNode > > locnBNs = getAllBrowseNodes( m_xComponentContext ); + + Sequence< Reference< browse::XBrowseNode > > children( + locnBNs.getLength() ); + + for ( sal_Int32 j = 0; j < locnBNs.getLength(); j++ ) + { + children[j] = new LocationBrowseNode( locnBNs[j] ); + } + + return children; + } + + virtual sal_Bool SAL_CALL hasChildNodes() + throw ( RuntimeException ) + { + return sal_True; // will always be user and share + } + + virtual sal_Int16 SAL_CALL getType() + throw ( RuntimeException ) + { + return browse::BrowseNodeTypes::CONTAINER; + } +}; + +BrowseNodeFactoryImpl::BrowseNodeFactoryImpl( + Reference< XComponentContext > const & xComponentContext ) + : m_xComponentContext( xComponentContext ) +{ +} + +BrowseNodeFactoryImpl::~BrowseNodeFactoryImpl() +{ +} + + +//############################################################################ +// Implementation of XBrowseNodeFactory +//############################################################################ + +/* + * The selector hierarchy is the standard hierarchy for organizers with the + * language nodes removed. + */ +Reference< browse::XBrowseNode > SAL_CALL +BrowseNodeFactoryImpl::createView( sal_Int16 viewType ) + throw (RuntimeException) +{ + switch( viewType ) + { + case browse::BrowseNodeFactoryViewTypes::MACROSELECTOR: + return getSelectorHierarchy(); + case browse::BrowseNodeFactoryViewTypes::MACROORGANIZER: + return getOrganizerHierarchy(); + default: + throw RuntimeException( OUSTR("Unknown view type" ), Reference< XInterface >() ); + } +} + +Reference< browse::XBrowseNode > +BrowseNodeFactoryImpl::getSelectorHierarchy() + throw (RuntimeException) +{ + /*if ( !m_xSelectorBrowseNode.is() ) + { + m_xSelectorBrowseNode = new SelectorBrowseNode( m_xComponentContext ); + }*/ + return new SelectorBrowseNode( m_xComponentContext ); +} + +Reference< browse::XBrowseNode > +BrowseNodeFactoryImpl::getOrganizerHierarchy() + throw (RuntimeException) +{ + Reference< browse::XBrowseNode > xRet = new DefaultRootBrowseNode( m_xComponentContext ); + return xRet; +} +//############################################################################ +// Helper methods +//############################################################################ + +//############################################################################ +// Namespace global methods for setting up BrowseNodeFactory service +//############################################################################ + +Sequence< ::rtl::OUString > SAL_CALL +bnf_getSupportedServiceNames( ) + SAL_THROW( () ) +{ + ::rtl::OUString str_name = ::rtl::OUString::createFromAscii( + "com.sun.star.script.browse.BrowseNodeFactory"); + + return Sequence< ::rtl::OUString >( &str_name, 1 ); +} + +::rtl::OUString SAL_CALL +bnf_getImplementationName( ) + SAL_THROW( () ) +{ + return ::rtl::OUString::createFromAscii( + "com.sun.star.script.browse.BrowseNodeFactory" ); +} + +Reference< XInterface > SAL_CALL +bnf_create( Reference< XComponentContext > const & xComponentContext ) + SAL_THROW( (Exception) ) +{ + return static_cast< ::cppu::OWeakObject * >( + new BrowseNodeFactoryImpl( xComponentContext ) ); +} + +//############################################################################ +// Implementation of XServiceInfo +//############################################################################ + +::rtl::OUString SAL_CALL +BrowseNodeFactoryImpl::getImplementationName() + throw (RuntimeException) +{ + return bnf_getImplementationName(); +} + +Sequence< ::rtl::OUString > SAL_CALL +BrowseNodeFactoryImpl::getSupportedServiceNames() + throw (RuntimeException) +{ + return bnf_getSupportedServiceNames(); +} + +sal_Bool BrowseNodeFactoryImpl::supportsService( + ::rtl::OUString const & serviceName ) + throw (RuntimeException) +{ +// check(); + + Sequence< ::rtl::OUString > supported_services( + getSupportedServiceNames() ); + + ::rtl::OUString const * ar = supported_services.getConstArray(); + + for ( sal_Int32 pos = supported_services.getLength(); pos--; ) + { + if (ar[ pos ].equals( serviceName )) + return sal_True; + } + return sal_False; +} + +} // namespace browsenodefactory diff --git a/scripting/source/provider/BrowseNodeFactoryImpl.hxx b/scripting/source/provider/BrowseNodeFactoryImpl.hxx new file mode 100644 index 000000000000..29a97e2f2bf5 --- /dev/null +++ b/scripting/source/provider/BrowseNodeFactoryImpl.hxx @@ -0,0 +1,88 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase2.hxx> + +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> + +#include <com/sun/star/script/browse/XBrowseNode.hpp> +#include <com/sun/star/script/browse/BrowseNodeTypes.hpp> +#include <com/sun/star/script/browse/XBrowseNodeFactory.hpp> + +namespace browsenodefactory +{ +// for simplification +#define css ::com::sun::star + +class BrowseNodeFactoryImpl : + public ::cppu::WeakImplHelper2 < + css::script::browse::XBrowseNodeFactory, + css::lang::XServiceInfo > +{ +private: + css::uno::Reference< css::uno::XComponentContext > m_xComponentContext; + css::uno::Reference< css::script::browse::XBrowseNode > m_xSelectorBrowseNode; + +protected: + virtual ~BrowseNodeFactoryImpl(); + +public: + BrowseNodeFactoryImpl( + css::uno::Reference< css::uno::XComponentContext > const & xComponentContext ); + + // XServiceInfo + virtual ::rtl::OUString SAL_CALL getImplementationName() + throw ( css::uno::RuntimeException ); + + virtual sal_Bool SAL_CALL + supportsService( ::rtl::OUString const & serviceName ) + throw ( css::uno::RuntimeException ); + + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL + getSupportedServiceNames() + throw ( css::uno::RuntimeException ); + + // XBrowseNodeFactory + virtual css::uno::Reference< css::script::browse::XBrowseNode > SAL_CALL + createView( sal_Int16 viewType ) + throw ( css::uno::RuntimeException ); + private: + css::uno::Reference< css::script::browse::XBrowseNode > + getSelectorHierarchy() + throw ( css::uno::RuntimeException ); + + css::uno::Reference< css::script::browse::XBrowseNode > + getOrganizerHierarchy() + throw ( css::uno::RuntimeException ); +}; + + +} // namespace browsenodefactory diff --git a/scripting/source/provider/MasterScriptProvider.cxx b/scripting/source/provider/MasterScriptProvider.cxx new file mode 100755 index 000000000000..94ea78f80c73 --- /dev/null +++ b/scripting/source/provider/MasterScriptProvider.cxx @@ -0,0 +1,1001 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" + +#include <comphelper/documentinfo.hxx> + +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <cppuhelper/factory.hxx> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/lang/EventObject.hpp> +#include <com/sun/star/container/XContentEnumerationAccess.hpp> +#include <com/sun/star/document/XScriptInvocationContext.hpp> + +#include <com/sun/star/uri/XUriReference.hpp> +#include <com/sun/star/uri/XUriReferenceFactory.hpp> +#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp> + +#include <com/sun/star/deployment/XPackage.hpp> +#include <com/sun/star/script/browse/BrowseNodeTypes.hpp> +#include <com/sun/star/script/provider/XScriptProviderFactory.hpp> +#include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp> + +#include <util/scriptingconstants.hxx> +#include <util/util.hxx> +#include <util/MiscUtils.hxx> + +#include "ActiveMSPList.hxx" +#include "MasterScriptProvider.hxx" +#include "URIHelper.hxx" + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::script; +using namespace ::com::sun::star::document; +using namespace ::sf_misc; +using namespace ::scripting_util; + +namespace func_provider +{ +//************************************************************************* +// Definitions for MasterScriptProviderFactory global methods. +//************************************************************************* + +::rtl::OUString SAL_CALL mspf_getImplementationName() ; +Reference< XInterface > SAL_CALL mspf_create( Reference< XComponentContext > const & xComponentContext ); +Sequence< ::rtl::OUString > SAL_CALL mspf_getSupportedServiceNames(); + + +bool endsWith( const ::rtl::OUString& target, + const ::rtl::OUString& item ) +{ + sal_Int32 index = 0; + if ( ( index = target.indexOf( item ) ) != -1 && + ( index == ( target.getLength() - item.getLength() ) ) ) + { + return true; + } + return false; +} +//::rtl_StandardModuleCount s_moduleCount = MODULE_COUNT_INIT; + +/* should be available in some central location. */ +//************************************************************************* +// XScriptProvider implementation +// +//************************************************************************* +MasterScriptProvider::MasterScriptProvider( const Reference< XComponentContext > & xContext ) throw ( RuntimeException ): + m_xContext( xContext ), m_bIsValid( false ), m_bInitialised( false ), + m_bIsPkgMSP( false ), m_pPCache( 0 ) +{ + validateXRef( m_xContext, "MasterScriptProvider::MasterScriptProvider: No context available\n" ); + m_xMgr = m_xContext->getServiceManager(); + validateXRef( m_xMgr, + "MasterScriptProvider::MasterScriptProvider: No service manager available\n" ); + m_bIsValid = true; +} + +//************************************************************************* +MasterScriptProvider::~MasterScriptProvider() +{ + //s_moduleCount.modCnt.release( &s_moduleCount.modCnt ); + if ( m_pPCache ) + { + delete m_pPCache; + } + m_pPCache = 0; +} + +//************************************************************************* +void SAL_CALL MasterScriptProvider::initialize( const Sequence < Any >& args ) +throw ( Exception, RuntimeException ) +{ + if ( m_bInitialised ) + return; + + m_bIsValid = false; + + sal_Int32 len = args.getLength(); + if ( len > 1 ) + { + throw RuntimeException( + OUSTR( "MasterScriptProvider::initialize: invalid number of arguments" ), + Reference< XInterface >() ); + } + + Sequence< Any > invokeArgs( len ); + + if ( len != 0 ) + { + // check if first parameter is a string + // if it is, this implies that this is a MSP created + // with a user or share ctx ( used for browse functionality ) + // + if ( args[ 0 ] >>= m_sCtxString ) + { + invokeArgs[ 0 ] = args[ 0 ]; + if ( m_sCtxString.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "vnd.sun.star.tdoc" ) ) == 0 ) + { + m_xModel = MiscUtils::tDocUrlToModel( m_sCtxString ); + } + } + else if ( args[ 0 ] >>= m_xInvocationContext ) + { + m_xModel.set( m_xInvocationContext->getScriptContainer(), UNO_QUERY_THROW ); + } + else + { + args[ 0 ] >>= m_xModel; + } + + if ( m_xModel.is() ) + { + // from the arguments, we were able to deduce a model. That alone doesn't + // suffice, we also need an XEmbeddedScripts which actually indicates support + // for embeddeding scripts + Reference< XEmbeddedScripts > xScripts( m_xModel, UNO_QUERY ); + if ( !xScripts.is() ) + { + throw lang::IllegalArgumentException( + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( + "The given document does not support embedding scripts into it, and cannot be associated with such a document." + ) ), + *this, + 1 + ); + } + + try + { + m_sCtxString = MiscUtils::xModelToTdocUrl( m_xModel, m_xContext ); + } + catch ( const Exception& ) + { + Any aError( ::cppu::getCaughtException() ); + + ::rtl::OUStringBuffer buf; + buf.appendAscii( "MasterScriptProvider::initialize: caught " ); + buf.append ( aError.getValueTypeName() ); + buf.appendAscii( ":" ); + + Exception aException; aError >>= aException; + buf.append ( aException.Message ); + throw lang::WrappedTargetException( buf.makeStringAndClear(), *this, aError ); + } + + if ( m_xInvocationContext.is() && m_xInvocationContext != m_xModel ) + invokeArgs[ 0 ] <<= m_xInvocationContext; + else + invokeArgs[ 0 ] <<= m_sCtxString; + } + + ::rtl::OUString pkgSpec = OUSTR("uno_packages"); + sal_Int32 indexOfPkgSpec = m_sCtxString.lastIndexOf( pkgSpec ); + + // if contex string ends with "uno_packages" + if ( indexOfPkgSpec > -1 && ( m_sCtxString.match( pkgSpec, indexOfPkgSpec ) == sal_True ) ) + { + m_bIsPkgMSP = sal_True; + } + else + { + m_bIsPkgMSP = sal_False; + } + } + else // no args + { + // use either scriping context or maybe zero args? + invokeArgs = Sequence< Any >( 0 ); // no arguments + } + m_sAargs = invokeArgs; + // don't create pkg mgr MSP for documents, not supported + if ( m_bIsPkgMSP == sal_False && !m_xModel.is() ) + { + createPkgProvider(); + } + + m_bInitialised = true; + m_bIsValid = true; +} + + +//************************************************************************* +void MasterScriptProvider::createPkgProvider() +{ + try + { + ::rtl::OUString loc = m_sCtxString; + Any location; + ::rtl::OUString sPkgCtx = m_sCtxString.concat( OUSTR(":uno_packages") ); + location <<= sPkgCtx; + + Reference< provider::XScriptProviderFactory > xFac( + m_xContext->getValueByName( + OUSTR( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW ); + + m_xMSPPkg.set( + xFac->createScriptProvider( location ), UNO_QUERY_THROW ); + + } + catch ( Exception& e ) + { + (void)e; + OSL_TRACE("Exception creating MasterScriptProvider for uno_packages in context %s: %s", + ::rtl::OUStringToOString( m_sCtxString, + RTL_TEXTENCODING_ASCII_US ).pData->buffer, + ::rtl::OUStringToOString( e.Message, + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); + } +} + +//************************************************************************* +Reference< provider::XScript > +MasterScriptProvider::getScript( const ::rtl::OUString& scriptURI ) +throw ( provider::ScriptFrameworkErrorException, + RuntimeException ) +{ + if ( !isValid() ) + { + throw provider::ScriptFrameworkErrorException( + OUSTR( "MasterScriptProvider not initialised" ), Reference< XInterface >(), + scriptURI, OUSTR(""), + provider::ScriptFrameworkErrorType::UNKNOWN ); + } + + // need to get the language from the string + + Reference< uri::XUriReferenceFactory > xFac ( + m_xMgr->createInstanceWithContext( rtl::OUString::createFromAscii( + "com.sun.star.uri.UriReferenceFactory"), m_xContext ) , UNO_QUERY ); + if ( !xFac.is() ) + { + ::rtl::OUString message = ::rtl::OUString::createFromAscii("Failed to instantiate UriReferenceFactory"); + throw provider::ScriptFrameworkErrorException( + message, Reference< XInterface >(), + scriptURI, ::rtl::OUString(), + provider::ScriptFrameworkErrorType::UNKNOWN ); + } + + Reference< uri::XUriReference > uriRef( + xFac->parse( scriptURI ), UNO_QUERY ); + + Reference < uri::XVndSunStarScriptUrl > sfUri( uriRef, UNO_QUERY ); + + if ( !uriRef.is() || !sfUri.is() ) + { + ::rtl::OUString errorMsg = OUSTR( "Incorrect format for Script URI: " ); + errorMsg = errorMsg.concat( scriptURI ); + throw provider::ScriptFrameworkErrorException( + errorMsg, Reference< XInterface >(), + scriptURI, OUSTR(""), + provider::ScriptFrameworkErrorType::UNKNOWN ); + } + + ::rtl::OUString langKey = ::rtl::OUString::createFromAscii( "language" ); + ::rtl::OUString locKey = ::rtl::OUString::createFromAscii( "location" ); + + if ( sfUri->hasParameter( langKey ) == sal_False || + sfUri->hasParameter( locKey ) == sal_False || + ( sfUri->getName().getLength() == 0 ) ) + { + ::rtl::OUString errorMsg = OUSTR( "Incorrect format for Script URI: " ); + errorMsg = errorMsg.concat( scriptURI ); + throw provider::ScriptFrameworkErrorException( + errorMsg, Reference< XInterface >(), + scriptURI, OUSTR(""), + provider::ScriptFrameworkErrorType::UNKNOWN ); + } + + ::rtl::OUString language = sfUri->getParameter( langKey ); + ::rtl::OUString location = sfUri->getParameter( locKey ); + + // if script us located in uno pkg + sal_Int32 index = -1; + ::rtl::OUString pkgTag = + ::rtl::OUString::createFromAscii( ":uno_packages" ); + // for languages other than basic, scripts located in uno packages + // are merged into the user/share location context. + // For other languages the location attribute in script url has the form + // location = [user|share]:uno_packages or location :uno_pacakges/xxxx.uno.pkg + // we need to extract the value of location part from the + // location attribute of the script, if the script is located in an + // uno package then that is the location part up to and including + // ":uno_packages", if the script is not in an uno package then the + // normal value is used e.g. user or share. + // The value extracted will be used to determine if the script is + // located in the same location context as this MSP. + // For Basic, the language script provider can handle the execution of a + // script in any location context + if ( ( index = location.indexOf( pkgTag ) ) > -1 ) + { + location = location.copy( 0, index + pkgTag.getLength() ); + } + + Reference< provider::XScript > xScript; + + // If the script location is in the same location context as this + // MSP then delate to the lanaguage provider controlled by this MSP + // ** Special case is BASIC, all calls to getScript will be handled + // by the language script provider in the current location context + // even if its different + if ( ( location.equals( OUSTR( "document" ) ) + && m_xModel.is() + ) + || ( endsWith( m_sCtxString, location ) ) + || ( language.equals( OUSTR( "Basic" ) ) ) + ) + { + Reference< provider::XScriptProvider > xScriptProvider; + ::rtl::OUStringBuffer buf( 80 ); + buf.appendAscii( "com.sun.star.script.provider.ScriptProviderFor"); + buf.append( language ); + ::rtl::OUString serviceName = buf.makeStringAndClear(); + if ( providerCache() ) + { + try + { + xScriptProvider.set( + providerCache()->getProvider( serviceName ), + UNO_QUERY_THROW ); + } + catch( const Exception& e ) + { + throw provider::ScriptFrameworkErrorException( + e.Message, Reference< XInterface >(), + sfUri->getName(), language, + provider::ScriptFrameworkErrorType::NOTSUPPORTED ); + } + } + else + { + throw provider::ScriptFrameworkErrorException( + OUSTR( "No LanguageProviders detected" ), + Reference< XInterface >(), + sfUri->getName(), language, + provider::ScriptFrameworkErrorType::NOTSUPPORTED ); + } + xScript=xScriptProvider->getScript( scriptURI ); + } + else + { + Reference< provider::XScriptProviderFactory > xFac_( + m_xContext->getValueByName( + OUSTR( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory") ), UNO_QUERY_THROW ); + + Reference< provider::XScriptProvider > xSP( + xFac_->createScriptProvider( makeAny( location ) ), UNO_QUERY_THROW ); + xScript = xSP->getScript( scriptURI ); + } + + return xScript; +} +//************************************************************************* +bool +MasterScriptProvider::isValid() +{ + return m_bIsValid; +} + +//************************************************************************* +ProviderCache* +MasterScriptProvider::providerCache() +{ + if ( !m_pPCache ) + { + ::osl::MutexGuard aGuard( m_mutex ); + if ( !m_pPCache ) + { + ::rtl::OUString serviceName1 = OUSTR("com.sun.star.script.provider.ScriptProviderForBasic"); + Sequence< ::rtl::OUString > blacklist(1); + blacklist[ 0 ] = serviceName1; + + if ( !m_bIsPkgMSP ) + { + m_pPCache = new ProviderCache( m_xContext, m_sAargs ); + } + else + { + m_pPCache = new ProviderCache( m_xContext, m_sAargs, blacklist ); + } + } + } + return m_pPCache; +} + + +//************************************************************************* +::rtl::OUString SAL_CALL +MasterScriptProvider::getName() + throw ( css::uno::RuntimeException ) +{ + if ( !isPkgProvider() ) + { + ::rtl::OUString sCtx = getContextString(); + if ( sCtx.indexOf( OUSTR( "vnd.sun.star.tdoc" ) ) == 0 ) + { + Reference< frame::XModel > xModel = m_xModel; + if ( !xModel.is() ) + { + xModel = MiscUtils::tDocUrlToModel( sCtx ); + } + + m_sNodeName = ::comphelper::DocumentInfo::getDocumentTitle( xModel ); + } + else + { + m_sNodeName = parseLocationName( getContextString() ); + } + } + else + { + m_sNodeName = OUSTR("uno_packages"); + } + return m_sNodeName; +} + +//************************************************************************* +Sequence< Reference< browse::XBrowseNode > > SAL_CALL +MasterScriptProvider::getChildNodes() + throw ( css::uno::RuntimeException ) +{ + Sequence< Reference< provider::XScriptProvider > > providers = getAllProviders(); + + Reference< provider::XScriptProvider > pkgProv = getPkgProvider(); + sal_Int32 size = providers.getLength(); + bool hasPkgs = pkgProv.is(); + if ( hasPkgs ) + { + size++; + } + Sequence< Reference< browse::XBrowseNode > > children( size ); + sal_Int32 provIndex = 0; + for ( ; provIndex < providers.getLength(); provIndex++ ) + { + children[ provIndex ] = Reference< browse::XBrowseNode >( providers[ provIndex ], UNO_QUERY ); + } + + if ( hasPkgs ) + { + children[ provIndex ] = Reference< browse::XBrowseNode >( pkgProv, UNO_QUERY ); + + } + + return children; +} + +//************************************************************************* +sal_Bool SAL_CALL +MasterScriptProvider::hasChildNodes() + throw ( css::uno::RuntimeException ) +{ + return sal_True; +} + +//************************************************************************* +sal_Int16 SAL_CALL +MasterScriptProvider::getType() + throw ( css::uno::RuntimeException ) +{ + return browse::BrowseNodeTypes::CONTAINER; +} + +//************************************************************************* + +::rtl::OUString +MasterScriptProvider::parseLocationName( const ::rtl::OUString& location ) +{ + // strip out the last leaf of location name + // e.g. file://dir1/dir2/Blah.sxw - > Blah.sxw + ::rtl::OUString temp = location; + INetURLObject aURLObj( temp ); + if ( !aURLObj.HasError() ) + temp = aURLObj.getName( INetURLObject::LAST_SEGMENT, true, INetURLObject::DECODE_WITH_CHARSET ); + return temp; +} + +//************************************************************************* +// Register Package +void SAL_CALL +MasterScriptProvider::insertByName( const ::rtl::OUString& aName, const Any& aElement ) throw ( lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, css::uno::RuntimeException) +{ + if ( !m_bIsPkgMSP ) + { + if ( m_xMSPPkg.is() ) + { + Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY ); + if ( !xCont.is() ) + { + throw RuntimeException( + OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"), + Reference< XInterface >() ); + } + xCont->insertByName( aName, aElement ); + } + else + { + throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"), + Reference< XInterface >() ); + } + + } + else + { + Reference< deployment::XPackage > xPkg( aElement, UNO_QUERY ); + if ( !xPkg.is() ) + { + throw lang::IllegalArgumentException( OUSTR("Couldn't convert to XPackage"), + Reference < XInterface > (), 2 ); + } + if ( !aName.getLength() ) + { + throw lang::IllegalArgumentException( OUSTR("Name not set!!"), + Reference < XInterface > (), 1 ); + } + // TODO for library pacakge parse the language, for the moment will try + // to get each provider to process the new Package, the first one the succeeds + // will terminate processing + if ( !providerCache() ) + { + throw RuntimeException( + OUSTR("insertByName cannot instantiate " + "child script providers."), + Reference< XInterface >() ); + } + Sequence < Reference< provider::XScriptProvider > > xSProviders = + providerCache()->getAllProviders(); + sal_Int32 index = 0; + + for ( ; index < xSProviders.getLength(); index++ ) + { + Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY ); + if ( !xCont.is() ) + { + continue; + } + try + { + xCont->insertByName( aName, aElement ); + break; + } + catch ( Exception& ) + { + } + + } + if ( index == xSProviders.getLength() ) + { + // No script providers could process the package + ::rtl::OUString message = OUSTR("Failed to register package for "); + message = message.concat( aName ); + throw lang::IllegalArgumentException( message, + Reference < XInterface > (), 2 ); + } + } +} + +//************************************************************************* +// Revoke Package +void SAL_CALL +MasterScriptProvider::removeByName( const ::rtl::OUString& Name ) throw ( container::NoSuchElementException, lang::WrappedTargetException, RuntimeException) +{ + if ( !m_bIsPkgMSP ) + { + if ( m_xMSPPkg.is() ) + { + Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY ); + if ( !xCont.is() ) + { + throw RuntimeException( + OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"), + Reference< XInterface >() ); + } + xCont->removeByName( Name ); + } + else + { + throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"), + Reference< XInterface >() ); + } + + } + else + { + if ( !Name.getLength() ) + { + throw lang::IllegalArgumentException( OUSTR("Name not set!!"), + Reference < XInterface > (), 1 ); + } + // TODO for Script library pacakge url parse the language, + // for the moment will just try to get each provider to process remove/revoke + // request, the first one the succeeds will terminate processing + + if ( !providerCache() ) + { + throw RuntimeException( + OUSTR("removeByName() cannot instantiate " + "child script providers."), + Reference< XInterface >() ); + } + Sequence < Reference< provider::XScriptProvider > > xSProviders = + providerCache()->getAllProviders(); + sal_Int32 index = 0; + for ( ; index < xSProviders.getLength(); index++ ) + { + Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY ); + if ( !xCont.is() ) + { + continue; + } + try + { + xCont->removeByName( Name ); + break; + } + catch ( Exception& ) + { + } + + } + if ( index == xSProviders.getLength() ) + { + // No script providers could process the package + ::rtl::OUString message = OUSTR("Failed to revoke package for "); + message = message.concat( Name ); + throw lang::IllegalArgumentException( message, + Reference < XInterface > (), 1 ); + } + + } +} + +//************************************************************************* +void SAL_CALL +MasterScriptProvider::replaceByName( const ::rtl::OUString& aName, const Any& aElement ) throw ( lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException) +{ + (void)aName; + (void)aElement; + + // TODO needs implementing + if ( true ) + { + throw RuntimeException( OUSTR("replaceByName not implemented!!!!") , + Reference< XInterface >() ); + } +} +//************************************************************************* +Any SAL_CALL +MasterScriptProvider::getByName( const ::rtl::OUString& aName ) throw ( container::NoSuchElementException, lang::WrappedTargetException, RuntimeException) +{ + (void)aName; + + // TODO needs to be implemented + Any result; + if ( true ) + { + throw RuntimeException( OUSTR("getByName not implemented!!!!") , + Reference< XInterface >() ); + } + return result; +} +//************************************************************************* +sal_Bool SAL_CALL +MasterScriptProvider::hasByName( const ::rtl::OUString& aName ) throw (RuntimeException) +{ + sal_Bool result = sal_False; + if ( !m_bIsPkgMSP ) + { + if ( m_xMSPPkg.is() ) + { + Reference< container::XNameContainer > xCont( m_xMSPPkg, UNO_QUERY ); + if ( !xCont.is() ) + { + throw RuntimeException( + OUSTR("PackageMasterScriptProvider doesn't implement XNameContainer"), + Reference< XInterface >() ); + } + + result = xCont->hasByName( aName ); + } + else + { + throw RuntimeException( OUSTR("PackageMasterScriptProvider is unitialised"), + Reference< XInterface >() ); + } + + } + else + { + if ( !aName.getLength() ) + { + throw lang::IllegalArgumentException( OUSTR("Name not set!!"), + Reference < XInterface > (), 1 ); + } + // TODO for Script library pacakge url parse the language, + // for the moment will just try to get each provider to see if the + // package exists in any provider, first one that succeed will + // terminate the loop + + if ( !providerCache() ) + { + throw RuntimeException( + OUSTR("removeByName() cannot instantiate " + "child script providers."), + Reference< XInterface >() ); + } + Sequence < Reference< provider::XScriptProvider > > xSProviders = + providerCache()->getAllProviders(); + for ( sal_Int32 index = 0; index < xSProviders.getLength(); index++ ) + { + Reference< container::XNameContainer > xCont( xSProviders[ index ], UNO_QUERY ); + if ( !xCont.is() ) + { + continue; + } + try + { + result = xCont->hasByName( aName ); + if ( result == sal_True ) + { + break; + } + } + catch ( Exception& ) + { + } + + } + } + return result; +} + +//************************************************************************* +Sequence< ::rtl::OUString > SAL_CALL +MasterScriptProvider::getElementNames( ) throw ( RuntimeException) +{ + // TODO needs implementing + Sequence< ::rtl::OUString > names; + if ( true ) + { + throw RuntimeException( OUSTR("getElementNames not implemented!!!!") , + Reference< XInterface >() ); + } + return names; +} +//************************************************************************* +Type SAL_CALL +MasterScriptProvider::getElementType( ) throw ( RuntimeException) +{ + // TODO needs implementing + Type t; + return t; +} +//************************************************************************* +sal_Bool SAL_CALL MasterScriptProvider::hasElements( ) throw ( RuntimeException) +{ + // TODO needs implementing + if ( true ) + { + throw RuntimeException( OUSTR("hasElements not implemented!!!!") , + Reference< XInterface >() ); + } + return false; +} + +//************************************************************************* +Sequence< Reference< provider::XScriptProvider > > SAL_CALL +MasterScriptProvider::getAllProviders() throw ( css::uno::RuntimeException ) +{ + if ( providerCache() ) + { + return providerCache()->getAllProviders(); + } + else + { + ::rtl::OUString errorMsg = ::rtl::OUString::createFromAscii( + "MasterScriptProvider::getAllProviders, cache not initialised"); + throw RuntimeException( errorMsg.concat( errorMsg ), + Reference< XInterface >() ); + } +} + + +//************************************************************************* +::rtl::OUString SAL_CALL MasterScriptProvider::getImplementationName( ) +throw( RuntimeException ) +{ + return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.MasterScriptProvider" ) ); +} + +//************************************************************************* +sal_Bool SAL_CALL MasterScriptProvider::supportsService( const ::rtl::OUString& serviceName ) +throw( RuntimeException ) +{ + Sequence< ::rtl::OUString > serviceNames( getSupportedServiceNames() ); + ::rtl::OUString const * pNames = serviceNames.getConstArray(); + for ( sal_Int32 nPos = serviceNames.getLength(); nPos--; ) + { + if ( serviceName.equals( pNames[ nPos ] ) ) + { + return sal_True; + } + } + return sal_False; +} + +//************************************************************************* +Sequence< ::rtl::OUString > SAL_CALL MasterScriptProvider::getSupportedServiceNames( ) +throw( RuntimeException ) +{ + ::rtl::OUString names[3]; + + names[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.MasterScriptProvider" ) ); + names[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.browse.BrowseNode" ) ); + names[2] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.ScriptProvider" ) ); + + return Sequence< ::rtl::OUString >( names, 3 ); +} + +} // namespace func_provider + + +namespace browsenodefactory +{ +::rtl::OUString SAL_CALL bnf_getImplementationName() ; +Reference< XInterface > SAL_CALL bnf_create( Reference< XComponentContext > const & xComponentContext ); +Sequence< ::rtl::OUString > SAL_CALL bnf_getSupportedServiceNames(); +} + +namespace scripting_runtimemgr +{ +//************************************************************************* +Reference< XInterface > SAL_CALL sp_create( + const Reference< XComponentContext > & xCompC ) +{ + return ( cppu::OWeakObject * ) new ::func_provider::MasterScriptProvider( xCompC ); +} + +//************************************************************************* +Sequence< ::rtl::OUString > sp_getSupportedServiceNames( ) + SAL_THROW( () ) +{ + ::rtl::OUString names[3]; + + names[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.MasterScriptProvider" ) ); + names[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.browse.BrowseNode" ) ); + names[2] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.ScriptProvider" ) ); + + return Sequence< ::rtl::OUString >( names, 3 ); +} + +//************************************************************************* +::rtl::OUString sp_getImplementationName( ) +SAL_THROW( () ) +{ + return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.MasterScriptProvider" ) ); +} + +// ***** registration or ScriptingFrameworkURIHelper +Reference< XInterface > SAL_CALL urihelper_create( + const Reference< XComponentContext > & xCompC ) +{ + return ( cppu::OWeakObject * ) + new ::func_provider::ScriptingFrameworkURIHelper( xCompC ); +} + +Sequence< ::rtl::OUString > urihelper_getSupportedServiceNames( ) + SAL_THROW( () ) +{ + ::rtl::OUString serviceNameList[] = { + ::rtl::OUString::createFromAscii( + "com.sun.star.script.provider.ScriptURIHelper" ) }; + + Sequence< ::rtl::OUString > serviceNames = Sequence < + ::rtl::OUString > ( serviceNameList, 1 ); + + return serviceNames; +} + +::rtl::OUString urihelper_getImplementationName( ) + SAL_THROW( () ) +{ + return ::rtl::OUString::createFromAscii( + "com.sun.star.script.provider.ScriptURIHelper"); +} + +static struct cppu::ImplementationEntry s_entries [] = + { + { + sp_create, sp_getImplementationName, + sp_getSupportedServiceNames, cppu::createSingleComponentFactory, + 0, 0 + }, + { + urihelper_create, + urihelper_getImplementationName, + urihelper_getSupportedServiceNames, + cppu::createSingleComponentFactory, + 0, 0 + }, + { + func_provider::mspf_create, func_provider::mspf_getImplementationName, + func_provider::mspf_getSupportedServiceNames, cppu::createSingleComponentFactory, + 0, 0 + }, + { + browsenodefactory::bnf_create, browsenodefactory::bnf_getImplementationName, + browsenodefactory::bnf_getSupportedServiceNames, cppu::createSingleComponentFactory, + 0, 0 + }, + { 0, 0, 0, 0, 0, 0 } + }; +} + +//############################################################################ +//#### EXPORTED ############################################################## +//############################################################################ + +/** + * Gives the environment this component belongs to. + */ +extern "C" +{ + SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( + const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv ) + { + (void)ppEnv; + *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; + } + + /** + * This function is called to get service factories for an implementation. + * + * @param pImplName name of implementation + * @param pServiceManager a service manager, need for component creation + * @param pRegistryKey the registry key for this component, need for persistent + * data + * @return a component factory + */ + SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory( + const sal_Char * pImplName, + lang::XMultiServiceFactory * pServiceManager, + registry::XRegistryKey * pRegistryKey ) + { + return ::cppu::component_getFactoryHelper( pImplName, pServiceManager, + pRegistryKey, ::scripting_runtimemgr::s_entries ); + } +} diff --git a/scripting/source/provider/MasterScriptProvider.hxx b/scripting/source/provider/MasterScriptProvider.hxx new file mode 100644 index 000000000000..76deec00ab8f --- /dev/null +++ b/scripting/source/provider/MasterScriptProvider.hxx @@ -0,0 +1,157 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_ +#define _FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_ + +#include <rtl/ustring.hxx> + +#include <cppuhelper/implbase5.hxx> + +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/container/XNameContainer.hpp> +#include <com/sun/star/document/XScriptInvocationContext.hpp> + +#include <com/sun/star/lang/XInitialization.hpp> + +#include <com/sun/star/script/provider/XScriptProvider.hpp> +#include <com/sun/star/script/browse/XBrowseNode.hpp> + +#include "ProviderCache.hxx" + +namespace func_provider +{ +// for simplification +#define css ::com::sun::star + + typedef ::cppu::WeakImplHelper5< + css::script::provider::XScriptProvider, + css::script::browse::XBrowseNode, css::lang::XServiceInfo, + css::lang::XInitialization, + css::container::XNameContainer > t_helper; + +class MasterScriptProvider : + public t_helper +{ +public: + MasterScriptProvider( + const css::uno::Reference< css::uno::XComponentContext > + & xContext ) throw( css::uno::RuntimeException ); + ~MasterScriptProvider(); + + // XServiceInfo implementation + virtual ::rtl::OUString SAL_CALL getImplementationName( ) + throw( css::uno::RuntimeException ); + + // XBrowseNode implementation + virtual ::rtl::OUString SAL_CALL getName() + throw ( css::uno::RuntimeException ); + virtual css::uno::Sequence< css::uno::Reference< css::script::browse::XBrowseNode > > SAL_CALL getChildNodes() + throw ( css::uno::RuntimeException ); + virtual sal_Bool SAL_CALL hasChildNodes() + throw ( css::uno::RuntimeException ); + virtual sal_Int16 SAL_CALL getType() + throw ( css::uno::RuntimeException ); + // XNameContainer + virtual void SAL_CALL insertByName( const ::rtl::OUString& aName, const css::uno::Any& aElement ) throw ( css::lang::IllegalArgumentException, css::container::ElementExistException, css::lang::WrappedTargetException, css::uno::RuntimeException); + virtual void SAL_CALL removeByName( const ::rtl::OUString& Name ) throw ( css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException); + + // XNameReplace + virtual void SAL_CALL replaceByName( const ::rtl::OUString& aName, const css::uno::Any& aElement ) throw ( css::lang::IllegalArgumentException, css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException); + // XNameAccess + virtual css::uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw ( css::container::NoSuchElementException, css::lang::WrappedTargetException, css::uno::RuntimeException); + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw ( css::uno::RuntimeException); + virtual sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException); + + // XElementAccess + virtual css::uno::Type SAL_CALL getElementType( ) throw ( css::uno::RuntimeException); + virtual sal_Bool SAL_CALL hasElements( ) throw ( css::uno::RuntimeException); + virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) + throw( css::uno::RuntimeException ); + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) + throw( css::uno::RuntimeException ); + + // XScriptProvider implementation + virtual css::uno::Reference < css::script::provider::XScript > SAL_CALL + getScript( const ::rtl::OUString& scriptURI ) + throw( css::script::provider::ScriptFrameworkErrorException, + css::uno::RuntimeException ); + + /** + * XInitialise implementation + * + * @param args expected to contain a single ::rtl::OUString + * containing the URI + */ + virtual void SAL_CALL initialize( const css::uno::Sequence < css::uno::Any > & args ) + throw ( css::uno::Exception, css::uno::RuntimeException); + + // Public method to return all Language Providers in this MasterScriptProviders + // context. + css::uno::Sequence< css::uno::Reference< css::script::provider::XScriptProvider > > SAL_CALL + getAllProviders() throw ( css::uno::RuntimeException ); + + bool isPkgProvider() { return m_bIsPkgMSP; } + css::uno::Reference< css::script::provider::XScriptProvider > getPkgProvider() { return m_xMSPPkg; } + // returns context string for this provider, eg + ::rtl::OUString getContextString() { return m_sCtxString; } + +private: + ::rtl::OUString parseLocationName( const ::rtl::OUString& location ); + void createPkgProvider(); + bool isValid(); + ::rtl::OUString getURLForModel(); + const css::uno::Sequence< ::rtl::OUString >& getProviderNames(); + + ProviderCache* providerCache(); + /* to obtain other services if needed */ + css::uno::Reference< css::uno::XComponentContext > m_xContext; + css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr; + css::uno::Reference< css::frame::XModel > m_xModel; + css::uno::Reference< css::document::XScriptInvocationContext > m_xInvocationContext; + css::uno::Sequence< css::uno::Any > m_sAargs; + ::rtl::OUString m_sNodeName; + + // This component supports XInitialization, it can be created + // using createInstanceXXX() or createInstanceWithArgumentsXXX using + // the service Mangager. + // Need to detect proper initialisation and validity + // for the object, so m_bIsValid indicates that the object is valid is set in ctor + // in case of createInstanceWithArgumentsXXX() called m_bIsValid is set to reset + // and then set to true when initialisation is complete + bool m_bIsValid; + // m_bInitialised ensure initialisation only takes place once. + bool m_bInitialised; + bool m_bIsPkgMSP; + css::uno::Reference< css::script::provider::XScriptProvider > m_xMSPPkg; + ProviderCache* m_pPCache; + osl::Mutex m_mutex; + ::rtl::OUString m_sCtxString; +}; +} // namespace func_provider +#endif //_FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_ diff --git a/scripting/source/provider/MasterScriptProviderFactory.cxx b/scripting/source/provider/MasterScriptProviderFactory.cxx new file mode 100644 index 000000000000..cd1aa83da3c7 --- /dev/null +++ b/scripting/source/provider/MasterScriptProviderFactory.cxx @@ -0,0 +1,153 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" +#include <cppuhelper/weakref.hxx> +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/factory.hxx> +#include <cppuhelper/exc_hlp.hxx> +#include <cppuhelper/implbase1.hxx> + +#include <util/util.hxx> + +#include "MasterScriptProviderFactory.hxx" + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::script; + +namespace func_provider +{ + +MasterScriptProviderFactory::MasterScriptProviderFactory( + Reference< XComponentContext > const & xComponentContext ) + : m_xComponentContext( xComponentContext ) +{ +} + +MasterScriptProviderFactory::~MasterScriptProviderFactory() +{ +} + + +//############################################################################ +// Implementation of XScriptProviderFactory +//############################################################################ + + +Reference< provider::XScriptProvider > SAL_CALL +MasterScriptProviderFactory::createScriptProvider( const Any& context ) throw ( lang::IllegalArgumentException, RuntimeException) +{ + Reference< provider::XScriptProvider > xMsp( getActiveMSPList() ->getMSPFromAnyContext( context ), UNO_QUERY_THROW ); + return xMsp; +} + +//############################################################################ +// Helper methods +//############################################################################ + +const rtl::Reference< ActiveMSPList > & +MasterScriptProviderFactory::getActiveMSPList() const +{ + if ( !m_MSPList.is() ) + { + ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() ); + if ( !m_MSPList.is() ) + m_MSPList = new ActiveMSPList( m_xComponentContext ); + } + return m_MSPList; +} + +//############################################################################ +// Namespace global methods for setting up MasterScriptProviderFactory service +//############################################################################ + +Sequence< ::rtl::OUString > SAL_CALL +mspf_getSupportedServiceNames( ) + SAL_THROW( () ) +{ + ::rtl::OUString str_name = ::rtl::OUString::createFromAscii( + "com.sun.star.script.provider.MasterScriptProviderFactory"); + + return Sequence< ::rtl::OUString >( &str_name, 1 ); +} + +::rtl::OUString SAL_CALL +mspf_getImplementationName( ) + SAL_THROW( () ) +{ + return ::rtl::OUString::createFromAscii( + "com.sun.star.script.provider.MasterScriptProviderFactory"); +} + +Reference< XInterface > SAL_CALL +mspf_create( Reference< XComponentContext > const & xComponentContext ) + SAL_THROW( (Exception) ) +{ + return static_cast< ::cppu::OWeakObject * >( + new MasterScriptProviderFactory( xComponentContext ) ); +} + +//############################################################################ +// Implementation of XServiceInfo +//############################################################################ + +::rtl::OUString SAL_CALL +MasterScriptProviderFactory::getImplementationName() + throw (RuntimeException) +{ + return mspf_getImplementationName(); +} + +Sequence< ::rtl::OUString > SAL_CALL +MasterScriptProviderFactory::getSupportedServiceNames() + throw (RuntimeException) +{ + return mspf_getSupportedServiceNames(); +} + +sal_Bool MasterScriptProviderFactory::supportsService( + ::rtl::OUString const & serviceName ) + throw (RuntimeException) +{ +// check(); + + Sequence< ::rtl::OUString > supported_services( + getSupportedServiceNames() ); + + ::rtl::OUString const * ar = supported_services.getConstArray(); + + for ( sal_Int32 pos = supported_services.getLength(); pos--; ) + { + if (ar[ pos ].equals( serviceName )) + return true; + } + return false; +} + +} // namespace browsenodefactory diff --git a/scripting/source/provider/MasterScriptProviderFactory.hxx b/scripting/source/provider/MasterScriptProviderFactory.hxx new file mode 100644 index 000000000000..16018bf13a54 --- /dev/null +++ b/scripting/source/provider/MasterScriptProviderFactory.hxx @@ -0,0 +1,85 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ +#include "rtl/ustring.hxx" +#include "rtl/ref.hxx" +#include <cppuhelper/implbase2.hxx> + +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/registry/XRegistryKey.hpp> + +#include <com/sun/star/script/provider/XScriptProviderFactory.hpp> +#include <com/sun/star/script/provider/XScriptProvider.hpp> + +#include "ActiveMSPList.hxx" + +namespace func_provider +{ +// for simplification +#define css ::com::sun::star + +class MasterScriptProviderFactory : + public ::cppu::WeakImplHelper2 < + css::script::provider::XScriptProviderFactory, + css::lang::XServiceInfo > +{ +private: + + mutable rtl::Reference< ActiveMSPList > m_MSPList; + + const css::uno::Reference< css::uno::XComponentContext > m_xComponentContext; + + const rtl::Reference< ActiveMSPList > & getActiveMSPList() const; + +protected: + virtual ~MasterScriptProviderFactory(); + +public: + MasterScriptProviderFactory( + css::uno::Reference< css::uno::XComponentContext > const & xComponentContext ); + + // XServiceInfo + virtual ::rtl::OUString SAL_CALL getImplementationName() + throw ( css::uno::RuntimeException ); + + virtual sal_Bool SAL_CALL + supportsService( ::rtl::OUString const & serviceName ) + throw ( css::uno::RuntimeException ); + + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL + getSupportedServiceNames() + throw ( css::uno::RuntimeException ); + + // XScriptProviderFactory + virtual css::uno::Reference< css::script::provider::XScriptProvider > + SAL_CALL createScriptProvider( const css::uno::Any& context ) + throw ( css::lang::IllegalArgumentException, css::uno::RuntimeException); +}; + + +} // namespace func_provider diff --git a/scripting/source/provider/ProviderCache.cxx b/scripting/source/provider/ProviderCache.cxx new file mode 100644 index 000000000000..5d3350f635e3 --- /dev/null +++ b/scripting/source/provider/ProviderCache.cxx @@ -0,0 +1,222 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/factory.hxx> + +#include <util/scriptingconstants.hxx> +#include <util/util.hxx> + +#include <com/sun/star/container/XContentEnumerationAccess.hpp> +#include "ProviderCache.hxx" + +using namespace com::sun::star; +using namespace com::sun::star::uno; +using namespace com::sun::star::script; +using namespace ::scripting_util; + +namespace func_provider +{ + +ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext ) + throw ( RuntimeException ) : m_Sctx( scriptContext ), m_xContext( xContext ) +{ + // initialise m_hProviderDetailsCache with details of ScriptProviders + // will use createContentEnumeration + + m_xMgr = m_xContext->getServiceManager(); + validateXRef( m_xMgr, "ProviderCache::ProviderCache() failed to obtain ServiceManager" ); + populateCache(); +} + + +ProviderCache::ProviderCache( const Reference< XComponentContext >& xContext, const Sequence< Any >& scriptContext, const Sequence< ::rtl::OUString >& blackList ) + throw ( RuntimeException ) : m_sBlackList( blackList ), m_Sctx( scriptContext ), m_xContext( xContext ) + +{ + // initialise m_hProviderDetailsCache with details of ScriptProviders + // will use createContentEnumeration + + m_xMgr = m_xContext->getServiceManager(); + validateXRef( m_xMgr, "ProviderCache::ProviderCache() failed to obtain ServiceManager" ); + populateCache(); +} + +ProviderCache::~ProviderCache() +{ +} + +Reference< provider::XScriptProvider > +ProviderCache::getProvider( const ::rtl::OUString& providerName ) +{ + ::osl::Guard< osl::Mutex > aGuard( m_mutex ); + Reference< provider::XScriptProvider > provider; + ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.find( providerName ); + if ( h_it != m_hProviderDetailsCache.end() ) + { + if ( h_it->second.provider.is() ) + { + provider = h_it->second.provider; + } + else + { + // need to create provider and insert into hash + provider = createProvider( h_it->second ); + } + } + return provider; +} + +Sequence < Reference< provider::XScriptProvider > > +ProviderCache::getAllProviders() throw ( RuntimeException ) +{ + Sequence < Reference< provider::XScriptProvider > > providers ( m_hProviderDetailsCache.size() ); + // need to create providers that haven't been created already + // so check what providers exist and what ones don't + + ::osl::Guard< osl::Mutex > aGuard( m_mutex ); + ProviderDetails_hash::iterator h_itEnd = m_hProviderDetailsCache.end(); + ProviderDetails_hash::iterator h_it = m_hProviderDetailsCache.begin(); + // should assert if size !> 0 + if ( m_hProviderDetailsCache.size() ) + { + sal_Int32 providerIndex = 0; + sal_Int32 index = 0; + for ( index = 0; h_it != h_itEnd; ++h_it, index++ ) + { + Reference< provider::XScriptProvider > xScriptProvider = h_it->second.provider; + if ( xScriptProvider.is() ) + { + providers[ providerIndex++ ] = xScriptProvider; + } + else + { + // create provider + try + { + xScriptProvider = createProvider( h_it->second ); + providers[ providerIndex++ ] = xScriptProvider; + } + catch ( Exception& e ) + { + ::rtl::OUString temp = OUSTR( "ProviderCache::getAllProviders: failed to create provider, " ); + temp.concat( e.Message ); + //throw RuntimeException( temp.concat( e.Message ), + // Reference< XInterface >() ); + } + } + } + + if ( providerIndex < index ) + { + providers.realloc( providerIndex ); + } + + } + else + { + OSL_TRACE("no available providers, something very wrong!!!"); + } + return providers; +} + +void +ProviderCache::populateCache() throw ( RuntimeException ) +{ + // wrong name in services.rdb + ::rtl::OUString serviceName; + ::osl::Guard< osl::Mutex > aGuard( m_mutex ); + try + { + ::rtl::OUString languageProviderName( RTL_CONSTASCII_USTRINGPARAM( + "com.sun.star.script.provider.LanguageScriptProvider" ) ); + + Reference< container::XContentEnumerationAccess > xEnumAccess = Reference< container::XContentEnumerationAccess >( m_xMgr, UNO_QUERY_THROW ); + Reference< container::XEnumeration > xEnum = xEnumAccess->createContentEnumeration ( languageProviderName ); + + while ( xEnum->hasMoreElements() ) + { + + Reference< lang::XSingleComponentFactory > factory; + if ( sal_False == ( xEnum->nextElement() >>= factory ) ) + { + throw new RuntimeException( ::rtl::OUString::createFromAscii( " error extracting XSingleComponentFactory from Content enumeration. " ), Reference< XInterface >() ); + } + validateXRef( factory, "ProviderCache::populateCache() invalid factory" ); + Reference< lang::XServiceInfo > xServiceInfo( factory, UNO_QUERY_THROW ); + validateXRef( xServiceInfo, "ProviderCache::populateCache() failed to get XServiceInfo from factory" ); + + Sequence< ::rtl::OUString > serviceNames = xServiceInfo->getSupportedServiceNames(); + + if ( serviceNames.getLength() > 0 ) + { + ::rtl::OUString searchString( RTL_CONSTASCII_USTRINGPARAM ( + "com.sun.star.script.provider.ScriptProviderFor" ) ); + + for ( sal_Int32 index = 0; index < serviceNames.getLength(); index++ ) + { + if ( serviceNames[ index ].indexOf( searchString ) == 0 && !isInBlackList( serviceNames[ index ] ) ) + { + serviceName = serviceNames[ index ]; + ProviderDetails details; + details.factory = factory; + m_hProviderDetailsCache[ serviceName ] = details; + break; + } + } + } + } + } + catch ( Exception e ) + { + ::rtl::OUString temp = OUSTR( + "ProviderCache::populateCache: couldn't obtain XSingleComponentFactory for " ); + temp.concat( serviceName ); + throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); + } +} + +Reference< provider::XScriptProvider > +ProviderCache::createProvider( ProviderDetails& details ) throw ( RuntimeException ) +{ + try + { + details.provider = Reference< provider::XScriptProvider >( + details.factory->createInstanceWithArgumentsAndContext( m_Sctx, m_xContext ), UNO_QUERY_THROW ); + validateXRef( details.provider, "ProviderCache::createProvider, failed to create provider"); + } + catch ( RuntimeException& e ) + { + ::rtl::OUString temp = ::rtl::OUString::createFromAscii("ProviderCache::createProvider() Error creating provider from factory!!!"); + throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); + } + + return details.provider; +} +} //end namespace diff --git a/scripting/source/provider/ProviderCache.hxx b/scripting/source/provider/ProviderCache.hxx new file mode 100644 index 000000000000..f747c067ac3d --- /dev/null +++ b/scripting/source/provider/ProviderCache.hxx @@ -0,0 +1,108 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _FRAMEWORK_SCRIPT_PROVIDER_PROVIDERCACHE_HXX_ +#define _FRAMEWORK_SCRIPT_PROVIDER_PROVIDERCACHE_HXX_ + +#include <hash_map> +#include <osl/mutex.hxx> +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase1.hxx> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XSingleServiceFactory.hpp> +#include <com/sun/star/lang/XSingleComponentFactory.hpp> +#include <com/sun/star/lang/XMultiComponentFactory.hpp> + +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/script/provider/XScriptProvider.hpp> + +#include "ScriptingContext.hxx" + +namespace func_provider +{ +// for simplification +#define css ::com::sun::star + +//Typedefs +//============================================================================= + +struct ProviderDetails +{ + //css::uno::Reference< css::lang::XSingleServiceFactory > factory; + css::uno::Reference< css::lang::XSingleComponentFactory > factory; + css::uno::Reference< css::script::provider::XScriptProvider > provider; +}; +typedef ::std::hash_map < ::rtl::OUString, ProviderDetails , ::rtl::OUStringHash, + ::std::equal_to< ::rtl::OUString > > ProviderDetails_hash; + + +class ProviderCache +{ + +public: + ProviderCache( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Sequence< css::uno::Any >& scriptContext ) + throw ( css::uno::RuntimeException ); + ProviderCache( const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Sequence< css::uno::Any >& scriptContext, + const css::uno::Sequence< ::rtl::OUString >& blackList ) + throw ( css::uno::RuntimeException ); + ~ProviderCache(); + css::uno::Reference< css::script::provider::XScriptProvider > + getProvider( const ::rtl::OUString& providerName ); + css::uno::Sequence < css::uno::Reference< css::script::provider::XScriptProvider > > + getAllProviders() throw ( css::uno::RuntimeException ); +private: + void populateCache() + throw ( css::uno::RuntimeException ); + + css::uno::Reference< css::script::provider::XScriptProvider > + createProvider( ProviderDetails& details ) throw ( css::uno::RuntimeException ); + bool isInBlackList( const ::rtl::OUString& serviceName ) + { + if ( m_sBlackList.getLength() > 0 ) + { + for ( sal_Int32 index = 0; index < m_sBlackList.getLength(); index++ ) + { + if ( m_sBlackList[ index ].equals( serviceName ) ) + { + return true; + } + } + } + return false; + } + css::uno::Sequence< ::rtl::OUString > m_sBlackList; + ProviderDetails_hash m_hProviderDetailsCache; + osl::Mutex m_mutex; + css::uno::Sequence< css::uno::Any > m_Sctx; + css::uno::Reference< css::uno::XComponentContext > m_xContext; + css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr; + + +}; +} // func_provider +#endif diff --git a/scripting/source/provider/ScriptImpl.cxx b/scripting/source/provider/ScriptImpl.cxx new file mode 100644 index 000000000000..f5b93a802138 --- /dev/null +++ b/scripting/source/provider/ScriptImpl.cxx @@ -0,0 +1,122 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" + +#include <stdio.h> + +#include "ScriptImpl.hxx" +#include <util/util.hxx> + +using namespace ::com::sun::star; +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::script::framework; + +namespace func_provider +{ + +//************************************************************************* +ScriptImpl::ScriptImpl( + const Reference< beans::XPropertySet > & scriptingContext, + const Reference< runtime::XScriptInvocation > & runtimeMgr, + const ::rtl::OUString& scriptURI ) +throw ( RuntimeException ) : + m_XScriptingContext( scriptingContext ), + m_RunTimeManager( runtimeMgr ), + m_ScriptURI( scriptURI ) +{ + OSL_TRACE( "<!constucting a ScriptImpl>\n" ); + validateXRef( m_XScriptingContext, + "ScriptImpl::ScriptImpl: No XScriptingContext\n" ); + validateXRef( m_RunTimeManager, + "ScriptImpl::ScriptImpl: No XScriptInvocation\n" ); +} + +//************************************************************************* +ScriptImpl::~ScriptImpl() +{ + OSL_TRACE( "<Destructing a ScriptImpl>\n" ); +} + +//************************************************************************* +Any SAL_CALL +ScriptImpl::invoke( const Sequence< Any >& aParams, + Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam ) +throw ( lang::IllegalArgumentException, script::CannotConvertException, + reflection::InvocationTargetException, RuntimeException ) +{ + OSL_TRACE( "<ScriptImpl::invoke>" ); + Any result; + Any anyScriptingContext; + + anyScriptingContext <<= m_XScriptingContext; + try + { + result = m_RunTimeManager->invoke( m_ScriptURI, anyScriptingContext, aParams, + aOutParamIndex, aOutParam ); + } + catch ( lang::IllegalArgumentException & iae ) + { + ::rtl::OUString temp = OUSTR( "ScriptImpl::invoke IllegalArgumentException : " ); + throw lang::IllegalArgumentException( temp.concat( iae.Message ), + Reference< XInterface > (), + iae.ArgumentPosition ); + } + catch ( script::CannotConvertException & cce ) + { + ::rtl::OUString temp = OUSTR( "ScriptImpl::invoke CannotConvertException : " ); + throw script::CannotConvertException( temp.concat( cce.Message ), + Reference< XInterface > (), + cce.DestinationTypeClass, + cce.Reason, + cce.ArgumentIndex ); + } + catch ( reflection::InvocationTargetException & ite ) + { + ::rtl::OUString temp = OUSTR( "ScriptImpl::invoke InvocationTargetException : " ); + throw reflection::InvocationTargetException( temp.concat( ite.Message ), + Reference< XInterface > (), + ite.TargetException ); + } + catch ( RuntimeException & re ) + { + ::rtl::OUString temp = OUSTR( "ScriptImpl::invoke RuntimeException : " ); + throw RuntimeException( temp.concat( re.Message ), + Reference< XInterface > () ); + } +#ifdef _DEBUG + catch ( ... ) + { + throw RuntimeException( + OUSTR( "ScriptImpl::invoke Unknown Exception caught - RuntimeException rethrown" ), + Reference< XInterface > () ); + } +#endif + return result; +} +} // namespace func_provider diff --git a/scripting/source/provider/ScriptImpl.hxx b/scripting/source/provider/ScriptImpl.hxx new file mode 100644 index 000000000000..fddaf11a35df --- /dev/null +++ b/scripting/source/provider/ScriptImpl.hxx @@ -0,0 +1,113 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _FRAMEWORK_SCRIPT_PROVIDER_FUNCTIONIMPL_HXX_ +#define _FRAMEWORK_SCRIPT_PROVIDER_FUNCTIONIMPL_HXX_ + +#include <cppuhelper/implbase1.hxx> // helper for XInterface, XTypeProvider etc. +#include <osl/mutex.hxx> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/script/CannotConvertException.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <com/sun/star/reflection/InvocationTargetException.hpp> + +#include <drafts/com/sun/star/script/framework/provider/XScript.hpp> +#include <drafts/com/sun/star/script/framework/runtime/XScriptInvocation.hpp> + +namespace func_provider +{ +// for simplification +#define css ::com::sun::star +#define dcsssf ::drafts::com::sun::star::script::framework + + +class ScriptImpl : + public ::cppu::WeakImplHelper1 < dcsssf::provider::XScript > +{ + +public: + /************************************************************* + ScriptImpl Constructor + @param runtimeMgr which is a service that implement a XScriptInvocation + @param scriptURI the received ScriptURI that needs to be resolve and invoked + */ + ScriptImpl( + const css::uno::Reference< css::beans::XPropertySet > & scriptingContext, + const css::uno::Reference< dcsssf::runtime::XScriptInvocation > & runtimeMgr, + const ::rtl::OUString& scriptURI ) + throw ( css::uno::RuntimeException ); + + /************************************************************* + ScriptImpl Destructor + */ + ~ScriptImpl(); + + /************************************************************* + Invoke + @param aParams all parameters; pure, out params are undefined in sequence, + i.e., the value has to be ignored by the callee + @param aOutParamIndex out indices + @param aOutParam out parameters + + @returns + the value returned from the function being invoked + + @throws IllegalArgumentException + if there is no matching script name + + @throws CannotConvertException + if args do not match or cannot be converted the those + of the invokee + + @throws InvocationTargetException + if the running script throws an exception this information is captured and + rethrown as this exception type. + + */ + virtual css::uno::Any SAL_CALL invoke( + const css::uno::Sequence< css::uno::Any > & aParams, + css::uno::Sequence< sal_Int16 > & aOutParamIndex, + css::uno::Sequence< css::uno::Any > & aOutParam ) + throw ( css::lang::IllegalArgumentException, + css::script::CannotConvertException, + css::reflection::InvocationTargetException, + css::uno::RuntimeException ); + +private: + css::uno::Reference< css::beans::XPropertySet > m_XScriptingContext; + css::uno::Reference < dcsssf::runtime::XScriptInvocation > m_RunTimeManager; + ::rtl::OUString m_ScriptURI; + + /* copy ctor disabled, i.e. not defined */ + ScriptImpl( const ScriptImpl& ); + /* assignment disabled, i.e. not defined */ + ScriptImpl& operator = ( const ScriptImpl& ); +}; +} // namespace func_provider +#endif //_FRAMEWORK_SCRIPT_PROVIDER_FUNCTIONIMPL_HXX_ diff --git a/scripting/source/provider/ScriptingContext.cxx b/scripting/source/provider/ScriptingContext.cxx new file mode 100755 index 000000000000..08a27a19562f --- /dev/null +++ b/scripting/source/provider/ScriptingContext.cxx @@ -0,0 +1,116 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" +#include <com/sun/star/beans/PropertyAttribute.hpp> +#include <com/sun/star/frame/XModel.hpp> + +#include <cppuhelper/implementationentry.hxx> +#include <cppuhelper/factory.hxx> + +#include <util/scriptingconstants.hxx> +#include <util/util.hxx> + +#include "ScriptingContext.hxx" + +using namespace com::sun::star; +using namespace com::sun::star::uno; +#define DOC_REF_PROPID 1 +#define DOC_STORAGE_ID_PROPID 2 +#define DOC_URI_PROPID 3 +#define RESOLVED_STORAGE_ID_PROPID 4 +#define SCRIPT_INFO_PROPID 5 +#define SCRIPTINGCONTEXT_DEFAULT_ATTRIBS() beans::PropertyAttribute::TRANSIENT | beans::PropertyAttribute::MAYBEVOID +namespace func_provider +{ + +//************************************************************************* +// XScriptingContext implementation +// +//************************************************************************* +ScriptingContext::ScriptingContext( const Reference< XComponentContext > & xContext ) : //ScriptingContextImpl_BASE( GetMutex()), + OPropertyContainer( GetBroadcastHelper() ), + m_xContext( xContext ) +{ + OSL_TRACE( "< ScriptingContext ctor called >\n" ); + + validateXRef( m_xContext, + "ScriptingContext::ScriptingContext: No context available\n" ); + + Any nullAny; + + scripting_constants::ScriptingConstantsPool& scriptingConstantsPool = + scripting_constants::ScriptingConstantsPool::instance(); + registerPropertyNoMember( scriptingConstantsPool.DOC_REF, DOC_REF_PROPID, SCRIPTINGCONTEXT_DEFAULT_ATTRIBS(),::getCppuType( (const Reference< css::frame::XModel >* ) NULL ), NULL ) ; + registerPropertyNoMember( scriptingConstantsPool.DOC_STORAGE_ID, DOC_STORAGE_ID_PROPID, SCRIPTINGCONTEXT_DEFAULT_ATTRIBS(), ::getCppuType( (const sal_Int32* ) NULL ), NULL ) ; + registerPropertyNoMember( scriptingConstantsPool.DOC_URI, DOC_URI_PROPID, SCRIPTINGCONTEXT_DEFAULT_ATTRIBS(), ::getCppuType( (const ::rtl::OUString* ) NULL ), NULL ) ; + registerPropertyNoMember( scriptingConstantsPool.RESOLVED_STORAGE_ID, RESOLVED_STORAGE_ID_PROPID, SCRIPTINGCONTEXT_DEFAULT_ATTRIBS(), ::getCppuType( (const sal_Int32* ) NULL ), NULL ); + registerPropertyNoMember( scriptingConstantsPool.SCRIPT_INFO, SCRIPT_INFO_PROPID, SCRIPTINGCONTEXT_DEFAULT_ATTRIBS(), ::getCppuType( (const sal_Int32* ) NULL ), NULL ); +} + +ScriptingContext::~ScriptingContext() +{ + OSL_TRACE( "< ScriptingContext dtor called >\n" ); +} +// ----------------------------------------------------------------------------- +// OPropertySetHelper +// ----------------------------------------------------------------------------- + +::cppu::IPropertyArrayHelper& ScriptingContext::getInfoHelper( ) +{ + return *getArrayHelper(); +} + +// ----------------------------------------------------------------------------- +// OPropertyArrayUsageHelper +// ----------------------------------------------------------------------------- + +::cppu::IPropertyArrayHelper* ScriptingContext::createArrayHelper( ) const +{ + Sequence< beans::Property > aProps; + describeProperties( aProps ); + return new ::cppu::OPropertyArrayHelper( aProps ); +} +// ----------------------------------------------------------------------------- +// XPropertySet +// ----------------------------------------------------------------------------- + +Reference< beans::XPropertySetInfo > ScriptingContext::getPropertySetInfo( ) throw (RuntimeException) +{ + Reference< beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) ); + return xInfo; +} +// -----------------------------------------------------------------------------// XTypeProvider +// ----------------------------------------------------------------------------- +IMPLEMENT_GET_IMPLEMENTATION_ID( ScriptingContext ) + +css::uno::Sequence< css::uno::Type > SAL_CALL ScriptingContext::getTypes( ) throw (css::uno::RuntimeException) +{ + return OPropertyContainer::getTypes(); +} +} // namespace func_provider diff --git a/scripting/source/provider/ScriptingContext.hxx b/scripting/source/provider/ScriptingContext.hxx new file mode 100644 index 000000000000..8c0b25814142 --- /dev/null +++ b/scripting/source/provider/ScriptingContext.hxx @@ -0,0 +1,91 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _FRAMEWORK_SCRIPT_PROTOCOLHANDLER_SCRIPTING_CONTEXT_HXX_ +#define _FRAMEWORK_SCRIPT_PROTOCOLHANDLER_SCRIPTING_CONTEXT_HXX_ + + +#include <osl/mutex.hxx> +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase1.hxx> +#include <comphelper/uno3.hxx> +#include <comphelper/propertycontainer.hxx> +#include <comphelper/proparrhlp.hxx> + +#include <cppuhelper/implbase1.hxx> +#include <cppuhelper/weak.hxx> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> +#include <comphelper/broadcasthelper.hxx> +namespace func_provider +{ +// for simplification +#define css ::com::sun::star + +//Typedefs +//============================================================================= +//typedef ::cppu::WeakImplHelper1< css::beans::XPropertySet > ScriptingContextImpl_BASE; + +class ScriptingContext : public ::comphelper::OMutexAndBroadcastHelper, public ::comphelper::OPropertyContainer, + public ::comphelper::OPropertyArrayUsageHelper< ScriptingContext >, public css::lang::XTypeProvider, public ::cppu::OWeakObject +{ + +public: + ScriptingContext( const css::uno::Reference< css::uno::XComponentContext > & xContext ); + ~ScriptingContext(); + // XInterface + + css::uno::Any SAL_CALL queryInterface( const css::uno::Type& rType ) + throw( css::uno::RuntimeException ) + { + css::uno::Any aRet( OPropertySetHelper::queryInterface( rType ) ); + return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType )); + } + void SAL_CALL acquire() throw() { ::cppu::OWeakObject::acquire(); } + void SAL_CALL release() throw() { ::cppu::OWeakObject::release(); } + // XPropertySet + virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) + throw ( css::uno::RuntimeException ); + //XTypeProvider + DECLARE_XTYPEPROVIDER( ) + +protected: + + // OPropertySetHelper + virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper( ); + + // OPropertyArrayUsageHelper + virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const; +private: + css::uno::Reference< css::uno::XComponentContext > m_xContext; + + +}; +} // func_provider +#endif //_FRAMEWORK_SCRIPT_PROVIDER_XFUNCTIONPROVIDER_HXX_ diff --git a/scripting/source/provider/URIHelper.cxx b/scripting/source/provider/URIHelper.cxx new file mode 100644 index 000000000000..d6d8e9d01337 --- /dev/null +++ b/scripting/source/provider/URIHelper.cxx @@ -0,0 +1,322 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +// MARKER(update_precomp.py): autogen include statement, do not remove +#include "precompiled_scripting.hxx" + +#include <com/sun/star/uri/XVndSunStarScriptUrl.hpp> +#include <rtl/ustrbuf.hxx> +#include "URIHelper.hxx" + +#define PRTSTR(x) ::rtl::OUStringToOString(x, RTL_TEXTENCODING_ASCII_US).pData->buffer + +namespace func_provider +{ + +using ::rtl::OUString; +namespace uno = ::com::sun::star::uno; +namespace ucb = ::com::sun::star::ucb; +namespace lang = ::com::sun::star::lang; +namespace uri = ::com::sun::star::uri; +namespace script = ::com::sun::star::script; + +static const char SHARE[] = "share"; +static const char SHARE_URI[] = + "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::BaseInstallation}"; + +static const char SHARE_UNO_PACKAGES[] = "share:uno_packages"; +static const char SHARE_UNO_PACKAGES_URI[] = + "vnd.sun.star.expand:$UNO_SHARED_PACKAGES_CACHE"; + +static const char USER[] = "user"; +static const char USER_URI[] = + "vnd.sun.star.expand:${$BRAND_BASE_DIR/program/" SAL_CONFIGFILE( "bootstrap") "::UserInstallation}"; + +static const char USER_UNO_PACKAGES[] = "user:uno_packages"; +static const char USER_UNO_PACKAGES_DIR[] = + "/user/uno_packages/cache"; + +static const char DOCUMENT[] = "document"; +static const char TDOC_SCHEME[] = "vnd.sun.star.tdoc"; + +ScriptingFrameworkURIHelper::ScriptingFrameworkURIHelper( + const uno::Reference< uno::XComponentContext >& xContext) + throw( uno::RuntimeException ) +{ + try + { + m_xSimpleFileAccess = uno::Reference< ucb::XSimpleFileAccess >( + xContext->getServiceManager()->createInstanceWithContext( + OUString::createFromAscii( + "com.sun.star.ucb.SimpleFileAccess"), + xContext), uno::UNO_QUERY_THROW); + } + catch (uno::Exception&) + { + OSL_ENSURE(false, + "Scripting Framework error initialising XSimpleFileAccess"); + } + + try + { + m_xUriReferenceFactory = uno::Reference< uri::XUriReferenceFactory >( + xContext->getServiceManager()->createInstanceWithContext( + OUString::createFromAscii( + "com.sun.star.uri.UriReferenceFactory"), + xContext ), uno::UNO_QUERY_THROW ); + } + catch (uno::Exception&) + { + OSL_ENSURE(false, + "Scripting Framework error initialising XUriReferenceFactory"); + } +} + +ScriptingFrameworkURIHelper::~ScriptingFrameworkURIHelper() +{ + // currently does nothing +} + +void SAL_CALL +ScriptingFrameworkURIHelper::initialize( + const uno::Sequence < uno::Any >& args ) +throw ( uno::Exception, uno::RuntimeException ) +{ + if ( args.getLength() != 2 || + args[0].getValueType() != ::getCppuType((const OUString*)NULL) || + args[1].getValueType() != ::getCppuType((const OUString*)NULL) ) + { + throw uno::RuntimeException( OUString::createFromAscii( + "ScriptingFrameworkURIHelper got invalid argument list" ), + uno::Reference< uno::XInterface >() ); + } + + if ( (args[0] >>= m_sLanguage) == sal_False || + (args[1] >>= m_sLocation) == sal_False ) + { + throw uno::RuntimeException( OUString::createFromAscii( + "ScriptingFrameworkURIHelper error parsing args" ), + uno::Reference< uno::XInterface >() ); + } + + SCRIPTS_PART = OUString::createFromAscii( "/Scripts/" ); + SCRIPTS_PART = SCRIPTS_PART.concat( m_sLanguage.toAsciiLowerCase() ); + + if ( !initBaseURI() ) + { + throw uno::RuntimeException( OUString::createFromAscii( + "ScriptingFrameworkURIHelper cannot find script directory"), + uno::Reference< uno::XInterface >() ); + } +} + +bool +ScriptingFrameworkURIHelper::initBaseURI() +{ + OUString uri, test; + bool bAppendScriptsPart = false; + + if ( m_sLocation.equalsAscii(USER)) + { + test = OUString::createFromAscii(USER); + uri = OUString::createFromAscii(USER_URI); + bAppendScriptsPart = true; + } + else if ( m_sLocation.equalsAscii(USER_UNO_PACKAGES)) + { + test = OUString::createFromAscii("uno_packages"); + uri = OUString::createFromAscii(USER_URI); + uri = uri.concat(OUString::createFromAscii(USER_UNO_PACKAGES_DIR)); + } + else if (m_sLocation.equalsAscii(SHARE)) + { + test = OUString::createFromAscii(SHARE); + uri = OUString::createFromAscii(SHARE_URI); + bAppendScriptsPart = true; + } + else if (m_sLocation.equalsAscii(SHARE_UNO_PACKAGES)) + { + test = OUString::createFromAscii("uno_packages"); + uri = OUString::createFromAscii(SHARE_UNO_PACKAGES_URI); + } + else if (m_sLocation.indexOf(OUString::createFromAscii(TDOC_SCHEME)) == 0) + { + m_sBaseURI = m_sLocation.concat( SCRIPTS_PART ); + m_sLocation = OUString::createFromAscii( DOCUMENT ); + return true; + } + else + { + return false; + } + + if ( !m_xSimpleFileAccess->exists( uri ) || + !m_xSimpleFileAccess->isFolder( uri ) ) + { + return false; + } + + uno::Sequence< OUString > children = + m_xSimpleFileAccess->getFolderContents( uri, true ); + + for ( sal_Int32 i = 0; i < children.getLength(); i++ ) + { + OUString child = children[i]; + sal_Int32 idx = child.lastIndexOf(test); + + // OSL_TRACE("Trying: %s", PRTSTR(child)); + // OSL_TRACE("idx=%d, testlen=%d, children=%d", + // idx, test.getLength(), child.getLength()); + + if ( idx != -1 && (idx + test.getLength()) == child.getLength() ) + { + // OSL_TRACE("FOUND PATH: %s", PRTSTR(child)); + if ( bAppendScriptsPart ) + { + m_sBaseURI = child.concat( SCRIPTS_PART ); + } + else + { + m_sBaseURI = child; + } + return true; + } + } + return false; +} + +OUString +ScriptingFrameworkURIHelper::getLanguagePart(const OUString& rStorageURI) +{ + OUString result; + + sal_Int32 idx = rStorageURI.indexOf(m_sBaseURI); + sal_Int32 len = m_sBaseURI.getLength() + 1; + + if ( idx != -1 ) + { + result = rStorageURI.copy(idx + len); + result = result.replace('/', '|'); + } + return result; +} + +OUString +ScriptingFrameworkURIHelper::getLanguagePath(const OUString& rLanguagePart) +{ + OUString result; + result = rLanguagePart.replace('|', '/'); + return result; +} + +OUString SAL_CALL +ScriptingFrameworkURIHelper::getScriptURI(const OUString& rStorageURI) + throw( lang::IllegalArgumentException, uno::RuntimeException ) +{ + ::rtl::OUStringBuffer buf(120); + + buf.appendAscii("vnd.sun.star.script:"); + buf.append(getLanguagePart(rStorageURI)); + buf.appendAscii("?language="); + buf.append(m_sLanguage); + buf.appendAscii("&location="); + buf.append(m_sLocation); + + return buf.makeStringAndClear(); +} + +OUString SAL_CALL +ScriptingFrameworkURIHelper::getStorageURI(const OUString& rScriptURI) + throw( lang::IllegalArgumentException, uno::RuntimeException ) +{ + OUString sLanguagePart; + try + { + uno::Reference < uri::XVndSunStarScriptUrl > xURI( + m_xUriReferenceFactory->parse( rScriptURI ), uno::UNO_QUERY_THROW ); + sLanguagePart = xURI->getName(); + } + catch ( uno::Exception& ) + { + throw lang::IllegalArgumentException( + OUString::createFromAscii( "Script URI not valid" ), + uno::Reference< uno::XInterface >(), 1 ); + } + + ::rtl::OUStringBuffer buf(120); + buf.append(m_sBaseURI); + buf.append(OUString::createFromAscii("/")); + buf.append(getLanguagePath(sLanguagePart)); + + OUString result = buf.makeStringAndClear(); + + return result; +} + +OUString SAL_CALL +ScriptingFrameworkURIHelper::getRootStorageURI() + throw( uno::RuntimeException ) +{ + return m_sBaseURI; +} + +OUString SAL_CALL +ScriptingFrameworkURIHelper::getImplementationName() + throw( uno::RuntimeException ) +{ + return OUString::createFromAscii( + "com.sun.star.script.provider.ScriptURIHelper" ); +} + +sal_Bool SAL_CALL +ScriptingFrameworkURIHelper::supportsService( const OUString& serviceName ) + throw( uno::RuntimeException ) +{ + OUString m_sServiceName = OUString::createFromAscii( + "com.sun.star.script.provider.ScriptURIHelper" ); + + if ( serviceName.equals( m_sServiceName ) ) + { + return sal_True; + } + return sal_False; +} + +uno::Sequence< ::rtl::OUString > SAL_CALL +ScriptingFrameworkURIHelper::getSupportedServiceNames() + throw( uno::RuntimeException ) +{ + ::rtl::OUString serviceNameList[] = { + ::rtl::OUString::createFromAscii( + "com.sun.star.script.provider.ScriptURIHelper" ) }; + + uno::Sequence< ::rtl::OUString > serviceNames = uno::Sequence < + ::rtl::OUString > ( serviceNameList, 1 ); + + return serviceNames; +} +} diff --git a/scripting/source/provider/URIHelper.hxx b/scripting/source/provider/URIHelper.hxx new file mode 100644 index 000000000000..2b0b38a4de0e --- /dev/null +++ b/scripting/source/provider/URIHelper.hxx @@ -0,0 +1,108 @@ +/************************************************************************* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * + * OpenOffice.org - a multi-platform office productivity suite + * + * This file is part of OpenOffice.org. + * + * OpenOffice.org is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 3 + * only, as published by the Free Software Foundation. + * + * OpenOffice.org is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License version 3 for more details + * (a copy is included in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU Lesser General Public License + * version 3 along with OpenOffice.org. If not, see + * <http://www.openoffice.org/license.html> + * for a copy of the LGPLv3 License. + * + ************************************************************************/ + +#ifndef _FRAMEWORK_SCRIPT_PROVIDER_XSCRIPTURIHELPER_HXX_ +#define _FRAMEWORK_SCRIPT_PROVIDER_XSCRIPTURIHELPER_HXX_ + +#include <com/sun/star/script/provider/XScriptURIHelper.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/uno/XComponentContext.hpp> +#include <com/sun/star/uno/RuntimeException.hpp> +#include <com/sun/star/ucb/XSimpleFileAccess.hpp> +#include <com/sun/star/uri/XUriReferenceFactory.hpp> + +#include <rtl/ustring.hxx> +#include <cppuhelper/implbase3.hxx> + +namespace func_provider +{ + +#define css ::com::sun::star + +class ScriptingFrameworkURIHelper : + public ::cppu::WeakImplHelper3< + css::script::provider::XScriptURIHelper, + css::lang::XServiceInfo, + css::lang::XInitialization > +{ +private: + + css::uno::Reference< css::ucb::XSimpleFileAccess > m_xSimpleFileAccess; + css::uno::Reference<css::uri::XUriReferenceFactory> m_xUriReferenceFactory; + + ::rtl::OUString m_sLanguage; + ::rtl::OUString m_sLocation; + ::rtl::OUString m_sBaseURI; + + ::rtl::OUString SCRIPTS_PART; + + bool initBaseURI(); + ::rtl::OUString getLanguagePart(const ::rtl::OUString& rStorageURI); + ::rtl::OUString getLanguagePath(const ::rtl::OUString& rLanguagePart); + +public: + + ScriptingFrameworkURIHelper( + const css::uno::Reference< css::uno::XComponentContext >& xContext ) + throw( css::uno::RuntimeException ); + + ~ScriptingFrameworkURIHelper(); + + virtual void SAL_CALL + initialize( const css::uno::Sequence < css::uno::Any > & args ) + throw ( css::uno::Exception, css::uno::RuntimeException); + + virtual ::rtl::OUString SAL_CALL + getRootStorageURI() + throw ( ::com::sun::star::uno::RuntimeException ); + + virtual ::rtl::OUString SAL_CALL + getScriptURI( const ::rtl::OUString& rStorageURI ) + throw( css::lang::IllegalArgumentException, + css::uno::RuntimeException ); + + virtual ::rtl::OUString SAL_CALL + getStorageURI( const ::rtl::OUString& rScriptURI ) + throw( css::lang::IllegalArgumentException, + css::uno::RuntimeException ); + + virtual ::rtl::OUString SAL_CALL + getImplementationName() + throw( css::uno::RuntimeException ); + + virtual sal_Bool SAL_CALL + supportsService( const ::rtl::OUString& ServiceName ) + throw( css::uno::RuntimeException ); + + virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL + getSupportedServiceNames() + throw( css::uno::RuntimeException ); +}; + +} // namespace func_provider +#endif //_FRAMEWORK_SCRIPT_PROVIDER_XSCRIPTURIHELPER_HXX_ diff --git a/scripting/source/provider/exports.dxp b/scripting/source/provider/exports.dxp new file mode 100644 index 000000000000..f0e1c69934bc --- /dev/null +++ b/scripting/source/provider/exports.dxp @@ -0,0 +1,2 @@ +component_getImplementationEnvironment +component_getFactory diff --git a/scripting/source/provider/makefile.mk b/scripting/source/provider/makefile.mk new file mode 100644 index 000000000000..a63ae078d837 --- /dev/null +++ b/scripting/source/provider/makefile.mk @@ -0,0 +1,53 @@ +#************************************************************************* +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# +# OpenOffice.org - a multi-platform office productivity suite +# +# This file is part of OpenOffice.org. +# +# OpenOffice.org is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License version 3 +# only, as published by the Free Software Foundation. +# +# OpenOffice.org is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License version 3 for more details +# (a copy is included in the LICENSE file that accompanied this code). +# +# You should have received a copy of the GNU Lesser General Public License +# version 3 along with OpenOffice.org. If not, see +# <http://www.openoffice.org/license.html> +# for a copy of the LGPLv3 License. +# +#************************************************************************* +PRJ=..$/.. + +PRJNAME= scripting +TARGET= provider +USE_DEFFILE= TRUE +ENABLE_EXCEPTIONS=TRUE +VISIBILITY_HIDDEN=TRUE +COMP1TYPELIST=$(TARGET) + +# --- Settings ----------------------------------------------------- + +.INCLUDE : settings.mk + +# ------------------------------------------------------------------ + +SLOFILES=\ + $(SLO)$/MasterScriptProvider.obj\ + $(SLO)$/URIHelper.obj\ + $(SLO)$/ActiveMSPList.obj\ + $(SLO)$/ProviderCache.obj\ + $(SLO)$/MasterScriptProviderFactory.obj\ + $(SLO)$/BrowseNodeFactoryImpl.obj\ + + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk diff --git a/scripting/source/provider/provider.xml b/scripting/source/provider/provider.xml new file mode 100755 index 000000000000..47127d879cd2 --- /dev/null +++ b/scripting/source/provider/provider.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE module-description PUBLIC "-//StarOffice//DTD ComponentDescription 1.0//EN" "module-description.dtd"> +<module-description xmlns:xlink="http://www.w3.org/1999/xlink"> + <module-name> ScriptProvider </module-name> + <component-description> + <author> Noel Power</author> + <name> com.sun.star.script.provider.ScriptProvider </name> + <description> + This component is part of the Scripting Framework +</description> + <loader-name> com.sun.star.loader.SharedLibrary </loader-name> + <language> c++ </language> + <status value="drafts"/> + <supported-service> com.sun.star.script.provider.ScriptProvider </supported-service> + <type> com.sun.star.beans.XPropertySet </type> + <type> com.sun.star.uno.Exception </type> + <type> com.sun.star.io.IOException </type> + <type> com.sun.star.io.XStream </type> + <type> com.sun.star.script.provider.XScript </type> + <type> com.sun.star.script.provider.XScriptProvider </type> + <type> com.sun.star.script.browse.XBrowseNode </type> + <type> com.sun.star.script.browse.BrowseNodeTypes </type> + <type> com.sun.star.uri.XUriReference </type> + <type> com.sun.star.uri.XUriReferenceFactory </type> + <type> com.sun.star.uri.XVndSunStarScriptUrl </type> + + </component-description> + <project-build-dependency> cppuhelper </project-build-dependency> + <project-build-dependency> cppu </project-build-dependency> + <project-build-dependency> sal </project-build-dependency> + <runtime-module-dependency> cppuhelper2$(COM) </runtime-module-dependency> + <runtime-module-dependency> cppu2 </runtime-module-dependency> + <runtime-module-dependency> sal2 </runtime-module-dependency> +</module-description> |