1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "res_services.hxx"
32 #include <vos/mutex.hxx>
33 #include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type
34 #include <cppuhelper/factory.hxx> // helper for factories
35 #include <cppuhelper/implbase3.hxx> // helper for implementations
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/script/XInvocation.hpp>
39 #include <com/sun/star/script/XTypeConverter.hpp>
40 #include <com/sun/star/reflection/InvocationTargetException.hpp>
41 #include <com/sun/star/beans/XExactName.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/beans/PropertyState.hpp>
45 #include <tools/resmgr.hxx>
46 #include <tools/rcid.h>
47 #include <tools/resary.hxx>
48 #include <vcl/svapp.hxx>
50 #include <rtl/ustring.hxx>
51 #include <rtl/strbuf.hxx>
55 using namespace com::sun::star::uno
;
56 using namespace com::sun::star::lang
;
57 using namespace com::sun::star::registry
;
58 using namespace com::sun::star::script
;
59 using namespace com::sun::star::beans
;
60 using namespace com::sun::star::reflection
;
62 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
64 //------------------------------------------------------------------------
65 class ResourceService
: public cppu::WeakImplHelper3
< XInvocation
, XExactName
, XServiceInfo
>
68 ResourceService( const Reference
< XMultiServiceFactory
> & );
72 OUString SAL_CALL
getImplementationName() throw();
73 BOOL SAL_CALL
supportsService(const OUString
& ServiceName
) throw();
74 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames(void) throw();
76 static Sequence
< OUString
> getSupportedServiceNames_Static(void) throw();
77 static OUString
getImplementationName_Static() throw()
79 return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.extensions.ResourceService"));
81 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
84 OUString SAL_CALL
getExactName( const OUString
& ApproximateName
) throw(RuntimeException
);
87 Reference
< XIntrospectionAccess
> SAL_CALL
getIntrospection(void) throw(RuntimeException
);
88 Any SAL_CALL
invoke(const OUString
& FunctionName
, const Sequence
< Any
>& Params
, Sequence
< sal_Int16
>& OutParamIndex
, Sequence
< Any
>& OutParam
) throw(IllegalArgumentException
, CannotConvertException
, InvocationTargetException
, RuntimeException
);
89 void SAL_CALL
setValue(const OUString
& PropertyName
, const Any
& Value
) throw(UnknownPropertyException
, CannotConvertException
, InvocationTargetException
, RuntimeException
);
90 Any SAL_CALL
getValue(const OUString
& PropertyName
) throw(UnknownPropertyException
, RuntimeException
);
91 BOOL SAL_CALL
hasMethod(const OUString
& Name
) throw(RuntimeException
);
92 BOOL SAL_CALL
hasProperty(const OUString
& Name
) throw(RuntimeException
);
94 Reference
< XTypeConverter
> getTypeConverter() const;
95 Reference
< XInvocation
> getDefaultInvocation() const;
97 Reference
< XMultiServiceFactory
> xSMgr
;
98 Reference
< XInvocation
> xDefaultInvocation
;
99 Reference
< XTypeConverter
> xTypeConverter
;
105 //-----------------------------------------------------------------------------
106 ResourceService::ResourceService( const Reference
< XMultiServiceFactory
> & rSMgr
)
112 //-----------------------------------------------------------------------------
113 Reference
< XInterface
> ResourceService::Create( const Reference
< XComponentContext
>& _rxContext
)
115 Reference
< XMultiServiceFactory
> xFactory( _rxContext
->getServiceManager(), UNO_QUERY_THROW
);
116 return *( new ResourceService( xFactory
) );
119 //-----------------------------------------------------------------------------
120 ResourceService::~ResourceService()
126 OUString
ResourceService::getImplementationName() throw()
128 return getImplementationName_Static();
132 BOOL SAL_CALL
ResourceService::supportsService(const OUString
& ServiceName
) throw()
134 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
135 const OUString
* pArray
= aSNL
.getConstArray();
136 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
137 if( pArray
[i
] == ServiceName
)
143 Sequence
< OUString
> SAL_CALL
ResourceService::getSupportedServiceNames(void) throw()
145 return getSupportedServiceNames_Static();
149 Sequence
< OUString
> ResourceService::getSupportedServiceNames_Static(void) throw()
151 Sequence
< OUString
> aSNS( 1 );
152 aSNS
.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.resource.VclStringResourceLoader"));
157 Reference
< XTypeConverter
> ResourceService::getTypeConverter() const
159 OGuard
aGuard( Application::GetSolarMutex() );
162 Reference
< XTypeConverter
> xConv( xSMgr
->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter" ))), UNO_QUERY
);
163 ((ResourceService
*)this)->xTypeConverter
= xConv
;
165 return xTypeConverter
;
169 Reference
< XInvocation
> ResourceService::getDefaultInvocation() const
171 OGuard
aGuard( Application::GetSolarMutex() );
172 /* f�hrt zur Zeit noch zu einer rekursion
175 Reference< XSingleServiceFactory > xFact( xSMgr->createInstance( OUString::createFromAscii("com.sun.star.script.Invocation") ), UNO_QUERY );
178 Sequence< Any > aArgs( 1 );
179 Reference< XInterface > xThis( *this );
180 aArgs.getArray()[0].set( &xThis, XInterface_Reference< get >lection() );
181 Reference< XInvokation > xI( xFact->createInstanceWithArguments( aArgs ), UNO_QUERY );
182 ((ResourceService*)this)->xDefaultInvocation = xI;
186 return xDefaultInvocation
;
190 OUString SAL_CALL
ResourceService::getExactName( const OUString
& ApproximateName
) throw(RuntimeException
)
192 OUString
aName( ApproximateName
);
193 aName
= aName
.toAsciiLowerCase();
194 if( aName
.equalsAscii("filename") )
195 return OUString(RTL_CONSTASCII_USTRINGPARAM("FileName"));
196 else if( aName
.equalsAscii("getstring" ))
197 return OUString(RTL_CONSTASCII_USTRINGPARAM("getString"));
198 else if( aName
.equalsAscii("getstrings" ))
199 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStrings"));
200 else if( aName
.equalsAscii("hasstring") )
201 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasString"));
202 else if( aName
.equalsAscii("hasstrings") )
203 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStrings"));
204 else if( aName
.equalsAscii("getstringlist") )
205 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStringList"));
206 else if( aName
.equalsAscii("hasStringList") )
207 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStringList"));
208 Reference
< XExactName
> xEN( getDefaultInvocation(), UNO_QUERY
);
210 return xEN
->getExactName( ApproximateName
);
215 Reference
< XIntrospectionAccess
> SAL_CALL
ResourceService::getIntrospection(void)
216 throw(RuntimeException
)
218 Reference
< XInvocation
> xI
= getDefaultInvocation();
220 return xI
->getIntrospection();
221 return Reference
< XIntrospectionAccess
>();
225 Any SAL_CALL
ResourceService::invoke
227 const OUString
& FunctionName
,
228 const Sequence
< Any
>& Params
,
229 Sequence
< sal_Int16
>& OutParamIndex
,
230 Sequence
< Any
>& OutParam
232 throw(IllegalArgumentException
, CannotConvertException
, InvocationTargetException
, RuntimeException
)
235 if( FunctionName
.equalsAscii("getString")
236 || FunctionName
.equalsAscii("getStrings" )
237 || FunctionName
.equalsAscii("hasString" )
238 || FunctionName
.equalsAscii("hasStrings" )
241 sal_Int32 nElements
= Params
.getLength();
243 throw IllegalArgumentException();
244 if( nElements
> 1 && (FunctionName
.equalsAscii("getString") || FunctionName
.equalsAscii("hasString") ) )
245 throw IllegalArgumentException();
247 throw IllegalArgumentException();
249 Sequence
< OUString
> aStrings( Params
.getLength() );
250 Sequence
< sal_Bool
> aBools( Params
.getLength() );
251 const Any
* pIn
= Params
.getConstArray();
252 OUString
* pOutString
= aStrings
.getArray();
253 sal_Bool
* pOutBool
= aBools
.getArray();
255 Reference
< XTypeConverter
> xC
= getTypeConverter();
256 bool bGetBranch
= FunctionName
.equalsAscii( "getString" ) || FunctionName
.equalsAscii( "getStrings" );
258 OGuard
aGuard( Application::GetSolarMutex() );
259 for( sal_Int32 n
= 0; n
< nElements
; n
++ )
262 if( !(pIn
[n
] >>= nId
) )
266 xC
->convertToSimpleType( pIn
[n
], TypeClass_LONG
) >>= nId
;
269 throw CannotConvertException();
271 if( nId
> 0xFFFF || nId
< 0 )
272 throw IllegalArgumentException();
276 ResId
aId( (USHORT
)nId
, *pResMgr
);
277 aId
.SetRT( RSC_STRING
);
278 if( pResMgr
->IsAvailable( aId
) )
281 pOutString
[n
] = aStr
;
284 throw IllegalArgumentException();
288 sal_Bool bRet
= sal_False
;
291 ResId
aId( (USHORT
)nId
, *pResMgr
);
292 aId
.SetRT( RSC_STRING
);
293 bRet
= pResMgr
->IsAvailable( aId
);
298 if( FunctionName
.equalsAscii("getString") )
299 aRet
<<= pOutString
[0];
300 else if( FunctionName
.equalsAscii("getStrings" ) )
302 else if( FunctionName
.equalsAscii("hasString" ) )
303 aRet
<<= pOutBool
[0];
307 else if( FunctionName
.equalsAscii("getStringList") || FunctionName
.equalsAscii("hasStringList" ) )
309 if( Params
.getLength() != 1 )
310 throw IllegalArgumentException();
311 Reference
< XTypeConverter
> xC
= getTypeConverter();
312 OGuard
aGuard( Application::GetSolarMutex() );
315 if( !(Params
.getConstArray()[0] >>= nId
) )
319 xC
->convertToSimpleType( Params
.getConstArray()[0], TypeClass_LONG
) >>= nId
;
322 throw CannotConvertException();
325 if( FunctionName
.equalsAscii("getStringList") )
327 ResId
aId( (USHORT
)nId
, *pResMgr
);
328 aId
.SetRT( RSC_STRINGARRAY
);
329 if( pResMgr
->IsAvailable( aId
) )
331 ResStringArray
aStr( aId
);
332 int nEntries
= aStr
.Count();
333 Sequence
< PropertyValue
> aPropSeq( nEntries
);
334 PropertyValue
* pOut
= aPropSeq
.getArray();
335 for( int i
= 0; i
< nEntries
; i
++ )
337 pOut
[i
].Name
= aStr
.GetString( i
);
339 pOut
[i
].Value
<<= aStr
.GetValue( i
);
340 pOut
[i
].State
= PropertyState_DIRECT_VALUE
;
345 throw IllegalArgumentException();
347 else // hasStringList
349 sal_Bool bRet
= sal_False
;
352 ResId
aId( (USHORT
)nId
, *pResMgr
);
353 aId
.SetRT( RSC_STRINGARRAY
);
354 bRet
= pResMgr
->IsAvailable( aId
);
361 Reference
< XInvocation
> xI
= getDefaultInvocation();
363 return xI
->invoke( FunctionName
, Params
, OutParamIndex
, OutParam
);
365 throw IllegalArgumentException();
371 void SAL_CALL
ResourceService::setValue(const OUString
& PropertyName
, const Any
& Value
)
372 throw(UnknownPropertyException
, CannotConvertException
, InvocationTargetException
, RuntimeException
)
374 if( PropertyName
.equalsAscii("FileName") )
377 if( !(Value
>>= aName
) )
379 Reference
< XTypeConverter
> xC
= getTypeConverter();
381 xC
->convertToSimpleType( Value
, TypeClass_STRING
) >>= aName
;
383 throw CannotConvertException();
386 OGuard
aGuard( Application::GetSolarMutex() );
387 OStringBuffer
aBuf( aName
.getLength()+8 );
388 aBuf
.append( OUStringToOString( aName
, osl_getThreadTextEncoding() ) );
389 ResMgr
* pRM
= ResMgr::CreateResMgr( aBuf
.getStr() );
391 throw InvocationTargetException();
395 aFileName
= OStringToOUString( aBuf
.makeStringAndClear(), osl_getThreadTextEncoding() );
399 Reference
< XInvocation
> xI
= getDefaultInvocation();
401 xI
->setValue( PropertyName
, Value
);
403 throw UnknownPropertyException();
408 Any SAL_CALL
ResourceService::getValue(const OUString
& PropertyName
)
409 throw(UnknownPropertyException
, RuntimeException
)
411 OGuard
aGuard( Application::GetSolarMutex() );
412 if( PropertyName
.equalsAscii("FileName" ))
413 return makeAny( aFileName
);
415 Reference
< XInvocation
> xI
= getDefaultInvocation();
417 return xI
->getValue( PropertyName
);
419 throw UnknownPropertyException();
423 BOOL SAL_CALL
ResourceService::hasMethod(const OUString
& Name
)
424 throw(RuntimeException
)
426 if( Name
.equalsAscii("getString") ||
427 Name
.equalsAscii("getStrings") ||
428 Name
.equalsAscii("hasString") ||
429 Name
.equalsAscii("hasStrings") ||
430 Name
.equalsAscii("getStringList") ||
431 Name
.equalsAscii("hasStringList")
436 Reference
< XInvocation
> xI
= getDefaultInvocation();
438 return xI
->hasMethod( Name
);
445 BOOL SAL_CALL
ResourceService::hasProperty(const OUString
& Name
)
446 throw(RuntimeException
)
448 if( Name
.equalsAscii("FileName") )
452 Reference
< XInvocation
> xI
= getDefaultInvocation();
454 return xI
->hasProperty( Name
);
462 ComponentInfo
getComponentInfo_VclStringResourceLoader()
465 aInfo
.aSupportedServices
= ResourceService::getSupportedServiceNames_Static();
466 aInfo
.sImplementationName
= ResourceService::getImplementationName_Static();
467 aInfo
.pFactory
= &ResourceService::Create
;