2010-10-12 15:53:47 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2002-09-20 13:37:46 +00:00
/*************************************************************************
*
2008-04-11 09:39:13 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
2002-09-20 13:37:46 +00:00
*
2010-02-12 15:01:35 +01:00
* Copyright 2000 , 2010 Oracle and / or its affiliates .
2002-09-20 13:37:46 +00:00
*
2008-04-11 09:39:13 +00:00
* OpenOffice . org - a multi - platform office productivity suite
2002-09-20 13:37:46 +00:00
*
2008-04-11 09:39:13 +00:00
* This file is part of OpenOffice . org .
2002-09-20 13:37:46 +00:00
*
2008-04-11 09:39:13 +00:00
* 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 .
2002-09-20 13:37:46 +00:00
*
2008-04-11 09:39:13 +00:00
* 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 ) .
2002-09-20 13:37:46 +00:00
*
2008-04-11 09:39:13 +00:00
* 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 .
2002-09-20 13:37:46 +00:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-09-16 11:29:43 +00:00
// MARKER(update_precomp.py): autogen include statement, do not remove
# include "precompiled_scripting.hxx"
2002-09-20 13:37:46 +00:00
# include <vector>
2003-06-19 10:11:41 +00:00
# include <stdlib.h>
2002-09-20 13:37:46 +00:00
# include <cppuhelper/implementationentry.hxx>
2002-10-23 13:13:25 +00:00
# include <com/sun/star/beans/XPropertySet.hpp>
2003-03-04 11:33:34 +00:00
# include <com/sun/star/security/AccessControlException.hpp>
2002-09-27 11:16:30 +00:00
2002-09-20 13:37:46 +00:00
# include <util/util.hxx>
# include <util/scriptingconstants.hxx>
2002-11-06 15:26:40 +00:00
# include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
2003-01-27 16:18:27 +00:00
# include <drafts/com/sun/star/script/framework/security/XScriptSecurity.hpp>
2002-11-06 15:26:40 +00:00
2002-09-20 13:37:46 +00:00
# include "ScriptNameResolverImpl.hxx"
# include "ScriptRuntimeManager.hxx"
using namespace : : rtl ;
using namespace : : com : : sun : : star ;
using namespace : : com : : sun : : star : : uno ;
using namespace : : drafts : : com : : sun : : star : : script : : framework ;
namespace scripting_runtimemgr
{
2003-02-06 17:22:14 +00:00
const sal_Char * const LANGUAGE_TO_RESOLVE_ON [ ] = { " All " } ; // should be configurable
2002-09-27 11:16:30 +00:00
OUString nrs_implName = OUString : : createFromAscii (
2003-03-03 19:17:33 +00:00
" drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver " ) ;
2002-09-27 11:16:30 +00:00
OUString nrs_serviceName = OUString : : createFromAscii (
2003-03-03 19:17:33 +00:00
" drafts.com.sun.star.script.framework.runtime.DefaultScriptNameResolver " ) ;
2002-09-20 13:37:46 +00:00
Sequence < OUString > nrs_serviceNames = Sequence < OUString > ( & nrs_serviceName , 1 ) ;
2002-11-06 15:26:40 +00:00
const char * const SCRIPTSTORAGEMANAGER_SERVICE =
" /singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager " ;
2002-09-20 13:37:46 +00:00
extern : : rtl_StandardModuleCount s_moduleCount ;
// define storages to search
2002-10-17 09:04:15 +00:00
static : : std : : vector < sal_Int32 > * m_pSearchIDs = NULL ;
2002-09-20 13:37:46 +00:00
//*************************************************************************
2002-09-27 11:16:30 +00:00
ScriptNameResolverImpl : : ScriptNameResolverImpl (
const Reference < XComponentContext > & xContext ) :
2002-11-06 15:26:40 +00:00
m_xContext ( xContext )
2002-09-20 13:37:46 +00:00
{
OSL_TRACE ( " < ScriptNameResolverImpl ctor called > \n " ) ;
2002-11-06 15:26:40 +00:00
validateXRef ( m_xContext , " ScriptNameResolverImpl::ScriptNameResolverImpl: invalid context " ) ;
m_xMultiComFac = m_xContext - > getServiceManager ( ) ;
validateXRef ( m_xMultiComFac , " ScriptNameResolverImpl::ScriptNameResolverImpl: invalid XMultiComponentFactory " ) ;
2002-10-24 12:57:03 +00:00
if ( ! m_pSearchIDs )
2002-09-27 11:16:30 +00:00
{
osl : : Guard < osl : : Mutex > aGuard ( m_mutex ) ;
2002-10-24 12:57:03 +00:00
if ( ! m_pSearchIDs )
2002-09-27 11:16:30 +00:00
{
2002-10-10 15:09:48 +00:00
scripting_constants : : ScriptingConstantsPool & scriptingConstantsPool =
scripting_constants : : ScriptingConstantsPool : : instance ( ) ;
2002-10-17 09:04:15 +00:00
m_pSearchIDs = new : : std : : vector < sal_Int32 > ( ) ;
2002-10-10 15:09:48 +00:00
m_pSearchIDs - > push_back ( scriptingConstantsPool . DOC_STORAGE_ID_NOT_SET ) ;
m_pSearchIDs - > push_back ( scriptingConstantsPool . USER_STORAGE_ID ) ;
m_pSearchIDs - > push_back ( scriptingConstantsPool . SHARED_STORAGE_ID ) ;
2002-09-27 11:16:30 +00:00
}
}
2002-09-20 13:37:46 +00:00
s_moduleCount . modCnt . acquire ( & s_moduleCount . modCnt ) ;
}
//*************************************************************************
ScriptNameResolverImpl : : ~ ScriptNameResolverImpl ( )
{
OSL_TRACE ( " < ScriptNameResolverImpl dtor called > \n " ) ;
s_moduleCount . modCnt . release ( & s_moduleCount . modCnt ) ;
}
//*************************************************************************
2003-01-28 10:52:17 +00:00
Reference < storage : : XScriptInfo > ScriptNameResolverImpl : : resolve (
2002-10-23 13:13:25 +00:00
const : : rtl : : OUString & scriptURI , Any & invocationCtx )
2002-09-27 11:16:30 +00:00
throw ( lang : : IllegalArgumentException , script : : CannotConvertException , RuntimeException )
2002-09-20 13:37:46 +00:00
{
2003-01-28 10:52:17 +00:00
Reference < storage : : XScriptInfo > resolvedName ;
2002-09-20 13:37:46 +00:00
Reference < beans : : XPropertySet > xPropSetScriptingContext ;
2002-10-30 13:25:26 +00:00
scripting_constants : : ScriptingConstantsPool & scriptingConstantsPool =
scripting_constants : : ScriptingConstantsPool : : instance ( ) ;
2002-09-20 13:37:46 +00:00
OSL_TRACE ( " ScriptNameResolverImpl::resolve: in resolve - start " ) ;
2002-09-27 11:16:30 +00:00
if ( sal_False = = ( invocationCtx > > = xPropSetScriptingContext ) )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
throw RuntimeException ( OUSTR (
" ScriptNameResolverImpl::resolve : unable to get XScriptingContext from param " ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
Any any ;
OUString docUri ;
2003-06-19 10:11:41 +00:00
sal_Int32 filesysScriptStorageID = - 1 ;
2003-05-21 08:04:36 +00:00
Reference < storage : : XScriptStorageManager > xScriptStorageMgr ;
2002-10-17 09:04:15 +00:00
sal_Int32 docSid ;
2002-09-20 13:37:46 +00:00
try
{
2002-10-10 15:09:48 +00:00
any = xPropSetScriptingContext - > getPropertyValue (
scriptingConstantsPool . DOC_URI ) ;
2002-09-20 13:37:46 +00:00
OSL_TRACE ( " ScriptNameResolverImpl::resolve: in resolve - got anyUri " ) ;
if ( sal_False = = ( any > > = docUri ) )
{
2002-09-27 11:16:30 +00:00
throw RuntimeException ( OUSTR (
" ScriptNameResolverImpl::resolve : unable to get doc Uri from xPropSetScriptingContext " ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
2002-10-10 15:09:48 +00:00
any = xPropSetScriptingContext - > getPropertyValue (
scriptingConstantsPool . DOC_STORAGE_ID ) ;
2002-09-20 13:37:46 +00:00
if ( sal_False = = ( any > > = docSid ) )
{
2002-09-27 11:16:30 +00:00
throw RuntimeException ( OUSTR (
" ScriptNameResolverImpl::resolve : unable to get doc storage id from xPropSetScriptingContext " ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
}
2002-09-27 11:16:30 +00:00
catch ( Exception & e )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : problem with getPropertyValue " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# ifdef _DEBUG
catch ( . . . )
{
2002-09-27 11:16:30 +00:00
throw RuntimeException ( OUSTR (
" ScriptNameResolverImpl::resolve Unknown Exception caught - RuntimeException rethrown " ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# endif
2002-09-27 11:16:30 +00:00
: : rtl : : OString docUriO (
: : rtl : : OUStringToOString ( docUri , RTL_TEXTENCODING_ASCII_US ) ) ;
2002-11-06 15:26:40 +00:00
OSL_TRACE (
2002-09-27 11:16:30 +00:00
" ScriptNameResolverImpl::resolve: *** >>> DOC URI: %s, doc sid is %d \n " ,
docUriO . pData - > buffer , docSid ) ;
2002-09-20 13:37:46 +00:00
2002-09-27 11:16:30 +00:00
OSL_TRACE ( " ScriptNameResolverImpl::resolve Starting... " ) ;
2003-02-19 15:06:09 +00:00
OUString docString = OUString : : createFromAscii ( " location=document " ) ;
OUString userString = OUString : : createFromAscii ( " location=user " ) ;
OUString shareString = OUString : : createFromAscii ( " location=share " ) ;
2003-05-16 09:14:24 +00:00
OUString filesysString = OUString : : createFromAscii ( " location=filesystem " ) ;
2003-02-19 15:06:09 +00:00
// initialise vector with doc, user and share
// m_pSearchIDs is initialised as follows,
// m_pSearchIDs [ 0 ] empty
// m_pSearchIDs [ 1 ] user storage id
// m_pSearchIDs [ 2 ] share " "
: : std : : vector < sal_Int32 > m_vSearchIDs = * m_pSearchIDs ;
2002-09-27 11:16:30 +00:00
m_vSearchIDs [ 0 ] = docSid ;
2003-02-19 15:06:09 +00:00
if ( scriptURI . indexOf ( docString ) ! = - 1 )
{
OSL_TRACE ( " Full resolution available, search document " ) ;
// search in document
m_vSearchIDs . resize ( 1 ) ;
}
else if ( scriptURI . indexOf ( userString ) ! = - 1 )
{
OSL_TRACE ( " Full resolution available, search user " ) ;
// search in user
m_vSearchIDs [ 0 ] = ( * m_pSearchIDs ) [ 1 ] ;
m_vSearchIDs . resize ( 1 ) ;
}
else if ( scriptURI . indexOf ( shareString ) ! = - 1 )
{
OSL_TRACE ( " Full resolution available, search share " ) ;
// search in share
m_vSearchIDs [ 0 ] = ( * m_pSearchIDs ) [ 2 ] ;
m_vSearchIDs . resize ( 1 ) ;
}
2003-05-16 09:14:24 +00:00
else if ( scriptURI . indexOf ( filesysString ) ! = - 1 )
{
OSL_TRACE ( " Full resolution available, create & search filesystem " ) ;
OUString filesysURL ;
try
{
filesysURL = getFilesysURL ( scriptURI ) ;
}
2003-05-23 13:59:52 +00:00
catch ( lang : : IllegalArgumentException & e )
2003-05-16 09:14:24 +00:00
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::resolve: " ) ;
throw RuntimeException ( temp . concat ( e . Message ) , Reference < XInterface > ( ) ) ;
}
Reference < XInterface > xInterface = m_xMultiComFac - > createInstanceWithContext (
: : rtl : : OUString : : createFromAscii (
" com.sun.star.ucb.SimpleFileAccess " ) , m_xContext ) ;
validateXRef ( xInterface ,
2003-07-23 13:05:13 +00:00
" ScriptProvider::initialise: cannot get SimpleFileAccess Service \n " ) ;
2003-05-16 09:14:24 +00:00
Reference < ucb : : XSimpleFileAccess > xSimpleFileAccess = Reference <
ucb : : XSimpleFileAccess > ( xInterface , UNO_QUERY_THROW ) ;
// do we need to encode this? hope not.
OSL_TRACE ( " >>>> About to create storage for %s " ,
: : rtl : : OUStringToOString ( filesysURL ,
RTL_TEXTENCODING_ASCII_US ) . pData - > buffer ) ;
// ask storage manager to create storage
try
{
// need to get the ScriptStorageManager
Any a = m_xContext - > getValueByName (
scriptingConstantsPool . SCRIPTSTORAGEMANAGER_SERVICE ) ;
if ( sal_False = = ( a > > = xScriptStorageMgr ) )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::resolve: failed to get ScriptStorageManager " ) ;
throw RuntimeException ( temp , Reference < XInterface > ( ) ) ;
// need to throw
}
validateXRef ( xScriptStorageMgr , " Cannot get ScriptStorageManager " ) ;
filesysScriptStorageID =
xScriptStorageMgr - > createScriptStorageWithURI (
xSimpleFileAccess , filesysURL ) ;
OSL_TRACE ( " >>>> Created storage %d - for %s " ,
filesysScriptStorageID , : : rtl : : OUStringToOString (
filesysURL , RTL_TEXTENCODING_ASCII_US ) . pData - > buffer ) ;
}
catch ( RuntimeException & e )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::resolve: " ) ;
throw RuntimeException ( temp . concat ( e . Message ) , Reference < XInterface > ( ) ) ;
}
m_vSearchIDs [ 0 ] = filesysScriptStorageID ;
m_vSearchIDs . resize ( 1 ) ;
}
2003-02-19 15:06:09 +00:00
else
{
OSL_TRACE ( " Only partial uri available, search doc, user & share " ) ;
// is this illegal or do we search in a default way
// if we get to here a uri has been passed in that has:
// a) not got a location specified
// b) an illegal location
// detect illegal location
if ( scriptURI . indexOf ( OUString : : createFromAscii ( " location= " ) ) ! = - 1 )
{
OSL_TRACE (
" ScriptNameResolver::resolve, throwing IllegalArgException " ) ;
throw lang : : IllegalArgumentException (
OUSTR ( " invalid URI: " ) . concat ( scriptURI ) ,
Reference < XInterface > ( ) , 1 ) ;
}
// leave vSearchIDs take care of the search...
}
2002-10-17 09:04:15 +00:00
: : std : : vector < sal_Int32 > : : const_iterator iter ;
: : std : : vector < sal_Int32 > : : const_iterator iterEnd = m_vSearchIDs . end ( ) ;
2002-09-20 13:37:46 +00:00
2002-09-27 11:16:30 +00:00
for ( iter = m_vSearchIDs . begin ( ) ; iter ! = iterEnd ; + + iter )
2002-09-20 13:37:46 +00:00
{
try
{
2002-09-27 11:16:30 +00:00
OSL_TRACE ( " ** about to resolve from storage using id %d from vector of size %d " ,
* iter , m_vSearchIDs . size ( ) ) ;
2003-01-28 16:09:27 +00:00
if ( ( resolvedName = resolveURIFromStorageID ( * iter , docUri , scriptURI ) ) . is ( ) )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
OSL_TRACE ( " found match in uri from storage %d " , * iter ) ;
2002-11-06 15:26:40 +00:00
xPropSetScriptingContext - > setPropertyValue (
2003-02-19 15:06:09 +00:00
scriptingConstantsPool . RESOLVED_STORAGE_ID , makeAny ( * iter ) ) ;
2002-09-20 13:37:46 +00:00
break ;
}
}
2003-03-04 17:34:54 +00:00
catch ( css : : security : : AccessControlException & e )
{
// no execute permission
OSL_TRACE ( " ScriptNameResolverImpl::resolve : AccessControlException " ) ;
continue ;
}
2003-02-19 15:06:09 +00:00
catch ( beans : : UnknownPropertyException & e )
2002-10-30 13:25:26 +00:00
{
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : UnknownPropertyException " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
2003-02-19 15:06:09 +00:00
Reference < XInterface > ( ) ) ;
2002-10-30 13:25:26 +00:00
}
2003-03-04 11:33:34 +00:00
catch ( beans : : PropertyVetoException & e )
2002-10-30 13:25:26 +00:00
{
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : PropertyVetoException " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
2003-02-19 15:06:09 +00:00
Reference < XInterface > ( ) ) ;
2002-10-30 13:25:26 +00:00
}
2003-02-19 15:06:09 +00:00
catch ( lang : : IllegalArgumentException & e )
2002-10-30 13:25:26 +00:00
{
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : IllegalArgumentException " ) ;
2003-02-25 15:17:26 +00:00
throw lang : : IllegalArgumentException ( temp . concat ( e . Message ) ,
Reference < XInterface > ( ) , e . ArgumentPosition ) ;
2002-10-30 13:25:26 +00:00
}
2003-03-04 11:33:34 +00:00
catch ( lang : : WrappedTargetException & e )
2002-10-30 13:25:26 +00:00
{
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : WrappedTargetException " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
2003-02-19 15:06:09 +00:00
Reference < XInterface > ( ) ) ;
2002-10-30 13:25:26 +00:00
}
2002-09-27 11:16:30 +00:00
catch ( Exception & e )
2002-09-20 13:37:46 +00:00
{
2003-02-19 15:06:09 +00:00
OSL_TRACE (
" Exception thrown by storage %d, failed to match uri: %s " ,
* iter ,
: : rtl : : OUStringToOString ( e . Message ,
RTL_TEXTENCODING_ASCII_US ) . pData - > buffer ) ;
2002-10-30 13:25:26 +00:00
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve : unknown exception " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
2003-02-19 15:06:09 +00:00
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# ifdef _DEBUG
catch ( . . . )
{
2003-02-19 15:06:09 +00:00
OSL_TRACE (
" unknown exception thrown by storage %d, failed to match uri " ,
2002-09-27 11:16:30 +00:00
* iter ) ;
2002-10-30 13:25:26 +00:00
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolve Unknown exception caught - RuntimeException rethrown " ) ;
throw RuntimeException ( temp ,
2003-02-19 15:06:09 +00:00
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# endif
2002-09-24 11:59:32 +00:00
2002-09-20 13:37:46 +00:00
}
2003-03-06 14:31:02 +00:00
if ( ! resolvedName . is ( ) )
{
2003-05-21 08:04:36 +00:00
if ( filesysScriptStorageID > 2 )
{
// get the filesys storage and dispose of it
Reference < XInterface > xScriptStorage =
xScriptStorageMgr - > getScriptStorage ( filesysScriptStorageID ) ;
validateXRef ( xScriptStorage ,
" ScriptNameResolverImpl::getStorageInstance: cannot get Script Storage service " ) ;
Reference < storage : : XScriptInfoAccess > xScriptInfoAccess = Reference <
storage : : XScriptInfoAccess > ( xScriptStorage , UNO_QUERY_THROW ) ;
validateXRef ( xScriptInfoAccess ,
" ScriptNameResolverImpl::resolveURIFromStorageID: cannot get XScriptInfoAccess " ) ;
Sequence < Reference < storage : : XScriptInfo > > results =
xScriptInfoAccess - > getAllImplementations ( ) ;
Reference < lang : : XEventListener > xEL_ScriptStorageMgr =
Reference < lang : : XEventListener >
( xScriptStorageMgr , UNO_QUERY_THROW ) ;
validateXRef ( xEL_ScriptStorageMgr , " ScriptNameResolverImpl::resolve: can't get ScriptStorageManager XEventListener interface when trying to dispose of filesystem storage " ) ;
lang : : EventObject event ( results [ 0 ] ) ;
xEL_ScriptStorageMgr - > disposing ( event ) ;
}
2003-03-06 14:31:02 +00:00
throw lang : : IllegalArgumentException ( OUSTR (
" ScriptNameResolverImpl::resolve: no script found for uri= " ) . concat ( scriptURI ) ,
Reference < XInterface > ( ) , 0 ) ;
}
2002-09-20 13:37:46 +00:00
return resolvedName ;
}
//*************************************************************************
OUString SAL_CALL
2002-09-27 11:16:30 +00:00
ScriptNameResolverImpl : : getImplementationName ( )
throw ( RuntimeException )
2002-09-20 13:37:46 +00:00
{
return nrs_implName ;
}
//*************************************************************************
sal_Bool SAL_CALL
ScriptNameResolverImpl : : supportsService ( const OUString & serviceName )
2002-09-27 11:16:30 +00:00
throw ( RuntimeException )
2002-09-20 13:37:46 +00:00
{
OUString const * pNames = nrs_serviceNames . getConstArray ( ) ;
for ( sal_Int32 nPos = nrs_serviceNames . getLength ( ) ; nPos - - ; )
{
2002-09-27 11:16:30 +00:00
if ( serviceName . equals ( pNames [ nPos ] ) )
2002-09-20 13:37:46 +00:00
{
return sal_True ;
}
}
return sal_False ;
}
//*************************************************************************
2003-01-28 10:52:17 +00:00
Reference < storage : : XScriptInfo >
2002-09-27 11:16:30 +00:00
ScriptNameResolverImpl : : resolveURIFromStorageID
2003-01-28 16:09:27 +00:00
( sal_Int32 sid , const : : rtl : : OUString & docURI ,
const : : rtl : : OUString & scriptURI )
2003-03-04 17:34:54 +00:00
SAL_THROW ( ( lang : : IllegalArgumentException , css : : security : : AccessControlException , RuntimeException ) )
2002-09-20 13:37:46 +00:00
{
2003-01-28 10:52:17 +00:00
Reference < storage : : XScriptInfo > resolvedScriptInfo ;
2002-10-10 15:09:48 +00:00
scripting_constants : : ScriptingConstantsPool & scriptingConstantsPool =
scripting_constants : : ScriptingConstantsPool : : instance ( ) ;
if ( sid = = scriptingConstantsPool . DOC_STORAGE_ID_NOT_SET )
2002-09-20 13:37:46 +00:00
{
2002-09-30 11:57:03 +00:00
OSL_TRACE ( " @@@@ **** ScriptNameResolverImpl::resolve DOC_STORAGE_ID_NOT_SET " ) ;
2002-11-06 15:26:40 +00:00
return resolvedScriptInfo ;
2002-09-20 13:37:46 +00:00
}
try
{
2003-05-16 09:14:24 +00:00
OUString permissionURI = docURI ;
OUString filesysString = OUString : : createFromAscii ( " location=filesystem " ) ;
if ( scriptURI . indexOf ( filesysString ) ! = - 1 )
{
// in the case of filesys scripts we're checking whether the
// location of the script, rather than the location of the document,
// has execute permission
try
{
permissionURI = getFilesysURL ( scriptURI ) ;
}
2003-05-23 13:59:52 +00:00
catch ( lang : : IllegalArgumentException & e )
2003-05-16 09:14:24 +00:00
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::resolveFromURI: " ) ;
throw RuntimeException ( temp . concat ( e . Message ) , Reference < XInterface > ( ) ) ;
}
}
Reference < storage : : XScriptInfoAccess > storage = getStorageInstance ( sid , permissionURI ) ;
2002-09-27 11:16:30 +00:00
validateXRef ( storage ,
2002-10-23 13:13:25 +00:00
" ScriptNameResolverImpl::resolveURIFromStorageID: cannot get XScriptInfoAccess " ) ;
Sequence < Reference < storage : : XScriptInfo > > results =
2002-09-27 11:16:30 +00:00
storage - > getImplementations ( scriptURI ) ;
2002-10-23 13:13:25 +00:00
const sal_Int32 length = results . getLength ( ) ;
2002-09-27 11:16:30 +00:00
2002-10-23 13:13:25 +00:00
if ( ! length )
2002-09-20 13:37:46 +00:00
{
2002-11-06 15:26:40 +00:00
return resolvedScriptInfo ;
2002-09-20 13:37:46 +00:00
}
2002-09-27 11:16:30 +00:00
OSL_TRACE ( " ScriptNameResolverImpl::resolve Got some results... " ) ;
2003-02-19 15:06:09 +00:00
// if we get results, just return first in list,
// storage has already matched language, function name etc. if
// that information was in the uri
resolvedScriptInfo = results [ 0 ] ;
2002-09-20 13:37:46 +00:00
}
2003-03-04 11:33:34 +00:00
catch ( css : : security : : AccessControlException & ace )
{
OUString temp = OUSTR (
" ScriptRuntimeManager::resolveURIFromStorageID AccessControlException: " ) ;
throw css : : security : : AccessControlException ( temp . concat ( ace . Message ) ,
Reference < XInterface > ( ) ,
ace . LackingPermission ) ;
}
2002-09-27 11:16:30 +00:00
catch ( lang : : IllegalArgumentException & iae )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
OUString temp = OUSTR (
" ScriptRuntimeManager::resolveURIFromStorageID IllegalArgumentException: " ) ;
throw lang : : IllegalArgumentException ( temp . concat ( iae . Message ) ,
Reference < XInterface > ( ) ,
iae . ArgumentPosition ) ;
2002-09-20 13:37:46 +00:00
}
2002-09-27 11:16:30 +00:00
catch ( RuntimeException & re )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
OUString temp = OUSTR (
" ScriptRuntimeManager::resolveURIFromStorageID RuntimeException: " ) ;
throw RuntimeException ( temp . concat ( re . Message ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
2002-09-27 11:16:30 +00:00
catch ( Exception & e )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
OUString temp = OUSTR (
" ScriptNameResolverImpl::resolveURIFromStorageID : Exception caught - RuntimeException rethrown " ) ;
throw RuntimeException ( temp . concat ( e . Message ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# ifdef _DEBUG
catch ( . . . )
{
2002-09-27 11:16:30 +00:00
throw RuntimeException ( OUSTR (
" ScriptNameResolverImpl::resolveURIFromStorageID Unknown exception caught - RuntimeException rethrown " ) ,
Reference < XInterface > ( ) ) ;
2002-09-20 13:37:46 +00:00
}
# endif
2002-11-06 15:26:40 +00:00
return resolvedScriptInfo ;
}
//*************************************************************************
Reference < storage : : XScriptInfoAccess >
2003-01-28 16:09:27 +00:00
ScriptNameResolverImpl : : getStorageInstance ( sal_Int32 sid ,
2003-05-16 09:14:24 +00:00
const : : rtl : : OUString & permissionURI ) SAL_THROW ( ( RuntimeException , css : : security : : AccessControlException , lang : : IllegalArgumentException ) )
2002-11-06 15:26:40 +00:00
{
Reference < storage : : XScriptInfoAccess > xScriptInfoAccess ;
try
{
Reference < XInterface > xInterface ;
Any a = m_xContext - > getValueByName (
OUString : : createFromAscii ( SCRIPTSTORAGEMANAGER_SERVICE ) ) ;
if ( sal_False = = ( a > > = xInterface ) )
{
throw RuntimeException (
OUSTR ( " ScriptNameResolverImpl::getStorageInstance: could not obtain ScriptStorageManager singleton " ) ,
Reference < XInterface > ( ) ) ;
}
validateXRef ( xInterface ,
" ScriptNameResolverImpl::getStorageInstance: cannot get Storage service " ) ;
2003-01-27 16:18:27 +00:00
// check that we have permissions for this storage
2003-03-04 11:33:34 +00:00
Reference < dcsssf : : security : : XScriptSecurity > xScriptSecurity ( xInterface , UNO_QUERY_THROW ) ;
2003-01-27 16:18:27 +00:00
validateXRef ( xScriptSecurity ,
" ScriptNameResolverImpl::getStorageInstance: cannot get Script Security service " ) ;
scripting_constants : : ScriptingConstantsPool & scriptingConstantsPool =
scripting_constants : : ScriptingConstantsPool : : instance ( ) ;
// if we dealing with a document storage (ie. not user or share
// we need to check the permission
2003-03-04 17:34:54 +00:00
if ( ( sid ! = scriptingConstantsPool . USER_STORAGE_ID ) & &
2003-03-12 14:54:18 +00:00
( sid ! = scriptingConstantsPool . SHARED_STORAGE_ID ) )
2003-03-04 11:33:34 +00:00
{
2003-05-16 09:14:24 +00:00
xScriptSecurity - > checkPermission ( permissionURI ,
2003-03-12 14:54:18 +00:00
OUString : : createFromAscii ( " execute " ) ) ;
// if we get here, the checkPermission hasn't thrown an
// AccessControlException, ie. permission has been granted
2003-03-04 17:34:54 +00:00
OSL_TRACE ( " ScriptNameResolverImpl::getStorageInstance: got execute permission for ID=%d " , sid ) ;
2003-01-27 16:18:27 +00:00
}
2002-11-06 15:26:40 +00:00
Reference < storage : : XScriptStorageManager > xScriptStorageManager ( xInterface , UNO_QUERY_THROW ) ;
validateXRef ( xScriptStorageManager ,
" ScriptNameResolverImpl::getStorageInstance: cannot get Script Storage Manager service " ) ;
Reference < XInterface > xScriptStorage =
xScriptStorageManager - > getScriptStorage ( sid ) ;
validateXRef ( xScriptStorage ,
" ScriptNameResolverImpl::getStorageInstance: cannot get Script Storage service " ) ;
xScriptInfoAccess = Reference <
storage : : XScriptInfoAccess > ( xScriptStorage , UNO_QUERY_THROW ) ;
}
2003-03-04 17:34:54 +00:00
catch ( lang : : IllegalArgumentException & e )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::getStorageInstance: " ) ;
throw lang : : IllegalArgumentException ( temp . concat ( e . Message ) ,
Reference < XInterface > ( ) , e . ArgumentPosition ) ;
}
catch ( css : : security : : AccessControlException & e )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::getStorageInstance: AccessControlException " ) ;
throw css : : security : : AccessControlException ( temp . concat ( e . Message ) , Reference < XInterface > ( ) , e . LackingPermission ) ;
}
2002-11-06 15:26:40 +00:00
catch ( RuntimeException & re )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::getStorageInstance: " ) ;
throw RuntimeException ( temp . concat ( re . Message ) , Reference < XInterface > ( ) ) ;
}
catch ( Exception & e )
{
OUString temp = OUSTR ( " ScriptNameResolverImpl::getStorageInstance: " ) ;
throw RuntimeException ( temp . concat ( e . Message ) , Reference < XInterface > ( ) ) ;
}
return xScriptInfoAccess ;
2002-09-20 13:37:46 +00:00
}
//*************************************************************************
2003-05-16 09:14:24 +00:00
OUString
ScriptNameResolverImpl : : getFilesysURL ( const OUString & scriptURI )
2003-05-23 13:59:52 +00:00
throw ( lang : : IllegalArgumentException )
2003-05-16 09:14:24 +00:00
{
OUString filePath ;
OUString fileName ;
OUString filesysString = OUString : : createFromAscii ( " location=filesystem " ) ;
sal_Int32 locationPos = scriptURI . indexOf ( filesysString ) ;
// expect location=filesys:file:///foo/bar/myscript.bsh etc
// except the file url at this point is encoded
// so we should be ok searching for the '&'
sal_Int32 filesysStrLen = filesysString . getLength ( ) + 1 ;
sal_Int32 endOfLocn = scriptURI . indexOf ( ' & ' , locationPos ) ;
if ( endOfLocn = = - 1 )
{
filePath = scriptURI . copy ( locationPos + filesysString . getLength ( ) + 1 ) ;
}
else
{
filePath = scriptURI . copy ( locationPos + filesysStrLen ,
endOfLocn - locationPos - filesysStrLen ) ;
}
//file name shoul also be encoded so again ok to search for '&'
OUString functionKey = OUString : : createFromAscii ( " function= " ) ;
sal_Int32 functionKeyLength = functionKey . getLength ( ) ;
sal_Int32 functionNamePos = scriptURI . indexOf ( functionKey ) ;
if ( functionNamePos > 0 )
{
sal_Int32 endOfFn = scriptURI . indexOf ( ' & ' , functionNamePos ) ;
if ( endOfFn = = - 1 )
{
fileName = scriptURI . copy ( functionNamePos + functionKeyLength ) ;
}
else
{
fileName = scriptURI . copy ( functionNamePos + functionKeyLength ,
endOfFn - functionNamePos - functionKeyLength ) ;
}
}
else
{
// we need to throw
OUString temp = OUSTR ( " ScriptNameResolverImpl::getFilesysURL: error getting the filesysURL " ) ;
2003-05-23 13:59:52 +00:00
throw lang : : IllegalArgumentException ( temp , Reference < XInterface > ( ) , 0 ) ;
2003-05-16 09:14:24 +00:00
}
filePath + = fileName ;
OSL_TRACE ( " ScriptNameResolverImpl::getFilesysURL: filesys URL = %s " ,
: : rtl : : OUStringToOString ( filePath ,
RTL_TEXTENCODING_ASCII_US ) . pData - > buffer ) ;
return filePath ;
}
//*************************************************************************
2002-09-20 13:37:46 +00:00
Sequence < OUString > SAL_CALL
2002-09-27 11:16:30 +00:00
ScriptNameResolverImpl : : getSupportedServiceNames ( )
throw ( RuntimeException )
2002-09-20 13:37:46 +00:00
{
return nrs_serviceNames ;
}
//*************************************************************************
Reference < XInterface > SAL_CALL scriptnri_create (
Reference < XComponentContext > const & xComponentContext )
2002-09-27 11:16:30 +00:00
SAL_THROW ( ( Exception ) )
2002-09-20 13:37:46 +00:00
{
2002-09-27 11:16:30 +00:00
return ( cppu : : OWeakObject * ) new ScriptNameResolverImpl ( xComponentContext ) ;
2002-09-20 13:37:46 +00:00
}
//*************************************************************************
Sequence < OUString > scriptnri_getSupportedServiceNames ( ) SAL_THROW ( ( ) )
{
return nrs_serviceNames ;
}
//*************************************************************************
OUString scriptnri_getImplementationName ( ) SAL_THROW ( ( ) )
{
return nrs_implName ;
}
} // namespace scripting_runtimemgr
2010-10-12 15:53:47 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */