1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: resource.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "res_services.hxx"
35 #include <vos/mutex.hxx>
36 #include <uno/lbnames.h> // CPPU_CURRENT_LANGUAGE_BINDING_NAME macro, which specify the environment type
37 #include <cppuhelper/factory.hxx> // helper for factories
38 #include <cppuhelper/implbase3.hxx> // helper for implementations
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/script/XInvocation.hpp>
42 #include <com/sun/star/script/XTypeConverter.hpp>
43 #include <com/sun/star/reflection/InvocationTargetException.hpp>
44 #include <com/sun/star/beans/XExactName.hpp>
45 #include <com/sun/star/beans/PropertyValue.hpp>
46 #include <com/sun/star/beans/PropertyState.hpp>
48 #include <tools/resmgr.hxx>
49 #include <tools/rcid.h>
50 #include <tools/resary.hxx>
51 #include <vcl/svapp.hxx>
53 #include <rtl/ustring.hxx>
54 #include <rtl/strbuf.hxx>
58 using namespace com::sun::star::uno
;
59 using namespace com::sun::star::lang
;
60 using namespace com::sun::star::registry
;
61 using namespace com::sun::star::script
;
62 using namespace com::sun::star::beans
;
63 using namespace com::sun::star::reflection
;
65 //------------------------------------------------------------------------
66 //------------------------------------------------------------------------
67 //------------------------------------------------------------------------
68 class ResourceService
: public cppu::WeakImplHelper3
< XInvocation
, XExactName
, XServiceInfo
>
71 ResourceService( const Reference
< XMultiServiceFactory
> & );
75 OUString SAL_CALL
getImplementationName() throw();
76 BOOL SAL_CALL
supportsService(const OUString
& ServiceName
) throw();
77 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames(void) throw();
79 static Sequence
< OUString
> getSupportedServiceNames_Static(void) throw();
80 static OUString
getImplementationName_Static() throw()
82 return OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.extensions.ResourceService"));
84 static Reference
< XInterface
> Create( const Reference
< XComponentContext
>& _rxContext
);
87 OUString SAL_CALL
getExactName( const OUString
& ApproximateName
) throw(RuntimeException
);
90 Reference
< XIntrospectionAccess
> SAL_CALL
getIntrospection(void) throw(RuntimeException
);
91 Any SAL_CALL
invoke(const OUString
& FunctionName
, const Sequence
< Any
>& Params
, Sequence
< sal_Int16
>& OutParamIndex
, Sequence
< Any
>& OutParam
) throw(IllegalArgumentException
, CannotConvertException
, InvocationTargetException
, RuntimeException
);
92 void SAL_CALL
setValue(const OUString
& PropertyName
, const Any
& Value
) throw(UnknownPropertyException
, CannotConvertException
, InvocationTargetException
, RuntimeException
);
93 Any SAL_CALL
getValue(const OUString
& PropertyName
) throw(UnknownPropertyException
, RuntimeException
);
94 BOOL SAL_CALL
hasMethod(const OUString
& Name
) throw(RuntimeException
);
95 BOOL SAL_CALL
hasProperty(const OUString
& Name
) throw(RuntimeException
);
97 Reference
< XTypeConverter
> getTypeConverter() const;
98 Reference
< XInvocation
> getDefaultInvocation() const;
100 Reference
< XMultiServiceFactory
> xSMgr
;
101 Reference
< XInvocation
> xDefaultInvocation
;
102 Reference
< XTypeConverter
> xTypeConverter
;
108 //-----------------------------------------------------------------------------
109 ResourceService::ResourceService( const Reference
< XMultiServiceFactory
> & rSMgr
)
115 //-----------------------------------------------------------------------------
116 Reference
< XInterface
> ResourceService::Create( const Reference
< XComponentContext
>& _rxContext
)
118 Reference
< XMultiServiceFactory
> xFactory( _rxContext
->getServiceManager(), UNO_QUERY_THROW
);
119 return *( new ResourceService( xFactory
) );
122 //-----------------------------------------------------------------------------
123 ResourceService::~ResourceService()
129 OUString
ResourceService::getImplementationName() throw()
131 return getImplementationName_Static();
135 BOOL SAL_CALL
ResourceService::supportsService(const OUString
& ServiceName
) throw()
137 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
138 const OUString
* pArray
= aSNL
.getConstArray();
139 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
140 if( pArray
[i
] == ServiceName
)
146 Sequence
< OUString
> SAL_CALL
ResourceService::getSupportedServiceNames(void) throw()
148 return getSupportedServiceNames_Static();
152 Sequence
< OUString
> ResourceService::getSupportedServiceNames_Static(void) throw()
154 Sequence
< OUString
> aSNS( 1 );
155 aSNS
.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.resource.VclStringResourceLoader"));
160 Reference
< XTypeConverter
> ResourceService::getTypeConverter() const
162 OGuard
aGuard( Application::GetSolarMutex() );
165 Reference
< XTypeConverter
> xConv( xSMgr
->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.script.Converter" ))), UNO_QUERY
);
166 ((ResourceService
*)this)->xTypeConverter
= xConv
;
168 return xTypeConverter
;
172 Reference
< XInvocation
> ResourceService::getDefaultInvocation() const
174 OGuard
aGuard( Application::GetSolarMutex() );
175 /* f�hrt zur Zeit noch zu einer rekursion
178 Reference< XSingleServiceFactory > xFact( xSMgr->createInstance( OUString::createFromAscii("com.sun.star.script.Invocation") ), UNO_QUERY );
181 Sequence< Any > aArgs( 1 );
182 Reference< XInterface > xThis( *this );
183 aArgs.getArray()[0].set( &xThis, XInterface_Reference< get >lection() );
184 Reference< XInvokation > xI( xFact->createInstanceWithArguments( aArgs ), UNO_QUERY );
185 ((ResourceService*)this)->xDefaultInvocation = xI;
189 return xDefaultInvocation
;
193 OUString SAL_CALL
ResourceService::getExactName( const OUString
& ApproximateName
) throw(RuntimeException
)
195 OUString
aName( ApproximateName
);
196 aName
= aName
.toAsciiLowerCase();
197 if( aName
.equalsAscii("filename") )
198 return OUString(RTL_CONSTASCII_USTRINGPARAM("FileName"));
199 else if( aName
.equalsAscii("getstring" ))
200 return OUString(RTL_CONSTASCII_USTRINGPARAM("getString"));
201 else if( aName
.equalsAscii("getstrings" ))
202 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStrings"));
203 else if( aName
.equalsAscii("hasstring") )
204 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasString"));
205 else if( aName
.equalsAscii("hasstrings") )
206 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStrings"));
207 else if( aName
.equalsAscii("getstringlist") )
208 return OUString(RTL_CONSTASCII_USTRINGPARAM("getStringList"));
209 else if( aName
.equalsAscii("hasStringList") )
210 return OUString(RTL_CONSTASCII_USTRINGPARAM("hasStringList"));
211 Reference
< XExactName
> xEN( getDefaultInvocation(), UNO_QUERY
);
213 return xEN
->getExactName( ApproximateName
);
218 Reference
< XIntrospectionAccess
> SAL_CALL
ResourceService::getIntrospection(void)
219 throw(RuntimeException
)
221 Reference
< XInvocation
> xI
= getDefaultInvocation();
223 return xI
->getIntrospection();
224 return Reference
< XIntrospectionAccess
>();
228 Any SAL_CALL
ResourceService::invoke
230 const OUString
& FunctionName
,
231 const Sequence
< Any
>& Params
,
232 Sequence
< sal_Int16
>& OutParamIndex
,
233 Sequence
< Any
>& OutParam
235 throw(IllegalArgumentException
, CannotConvertException
, InvocationTargetException
, RuntimeException
)
238 if( FunctionName
.equalsAscii("getString")
239 || FunctionName
.equalsAscii("getStrings" )
240 || FunctionName
.equalsAscii("hasString" )
241 || FunctionName
.equalsAscii("hasStrings" )
244 sal_Int32 nElements
= Params
.getLength();
246 throw IllegalArgumentException();
247 if( nElements
> 1 && (FunctionName
.equalsAscii("getString") || FunctionName
.equalsAscii("hasString") ) )
248 throw IllegalArgumentException();
250 throw IllegalArgumentException();
252 Sequence
< OUString
> aStrings( Params
.getLength() );
253 Sequence
< sal_Bool
> aBools( Params
.getLength() );
254 const Any
* pIn
= Params
.getConstArray();
255 OUString
* pOutString
= aStrings
.getArray();
256 sal_Bool
* pOutBool
= aBools
.getArray();
258 Reference
< XTypeConverter
> xC
= getTypeConverter();
259 bool bGetBranch
= FunctionName
.equalsAscii( "getString" ) || FunctionName
.equalsAscii( "getStrings" );
261 OGuard
aGuard( Application::GetSolarMutex() );
262 for( sal_Int32 n
= 0; n
< nElements
; n
++ )
265 if( !(pIn
[n
] >>= nId
) )
269 xC
->convertToSimpleType( pIn
[n
], TypeClass_LONG
) >>= nId
;
272 throw CannotConvertException();
274 if( nId
> 0xFFFF || nId
< 0 )
275 throw IllegalArgumentException();
279 ResId
aId( (USHORT
)nId
, *pResMgr
);
280 aId
.SetRT( RSC_STRING
);
281 if( pResMgr
->IsAvailable( aId
) )
284 pOutString
[n
] = aStr
;
287 throw IllegalArgumentException();
291 sal_Bool bRet
= sal_False
;
294 ResId
aId( (USHORT
)nId
, *pResMgr
);
295 aId
.SetRT( RSC_STRING
);
296 bRet
= pResMgr
->IsAvailable( aId
);
301 if( FunctionName
.equalsAscii("getString") )
302 aRet
<<= pOutString
[0];
303 else if( FunctionName
.equalsAscii("getStrings" ) )
305 else if( FunctionName
.equalsAscii("hasString" ) )
306 aRet
<<= pOutBool
[0];
310 else if( FunctionName
.equalsAscii("getStringList") || FunctionName
.equalsAscii("hasStringList" ) )
312 if( Params
.getLength() != 1 )
313 throw IllegalArgumentException();
314 Reference
< XTypeConverter
> xC
= getTypeConverter();
315 OGuard
aGuard( Application::GetSolarMutex() );
318 if( !(Params
.getConstArray()[0] >>= nId
) )
322 xC
->convertToSimpleType( Params
.getConstArray()[0], TypeClass_LONG
) >>= nId
;
325 throw CannotConvertException();
328 if( FunctionName
.equalsAscii("getStringList") )
330 ResId
aId( (USHORT
)nId
, *pResMgr
);
331 aId
.SetRT( RSC_STRINGARRAY
);
332 if( pResMgr
->IsAvailable( aId
) )
334 ResStringArray
aStr( aId
);
335 int nEntries
= aStr
.Count();
336 Sequence
< PropertyValue
> aPropSeq( nEntries
);
337 PropertyValue
* pOut
= aPropSeq
.getArray();
338 for( int i
= 0; i
< nEntries
; i
++ )
340 pOut
[i
].Name
= aStr
.GetString( i
);
342 pOut
[i
].Value
<<= aStr
.GetValue( i
);
343 pOut
[i
].State
= PropertyState_DIRECT_VALUE
;
348 throw IllegalArgumentException();
350 else // hasStringList
352 sal_Bool bRet
= sal_False
;
355 ResId
aId( (USHORT
)nId
, *pResMgr
);
356 aId
.SetRT( RSC_STRINGARRAY
);
357 bRet
= pResMgr
->IsAvailable( aId
);
364 Reference
< XInvocation
> xI
= getDefaultInvocation();
366 return xI
->invoke( FunctionName
, Params
, OutParamIndex
, OutParam
);
368 throw IllegalArgumentException();
374 void SAL_CALL
ResourceService::setValue(const OUString
& PropertyName
, const Any
& Value
)
375 throw(UnknownPropertyException
, CannotConvertException
, InvocationTargetException
, RuntimeException
)
377 if( PropertyName
.equalsAscii("FileName") )
380 if( !(Value
>>= aName
) )
382 Reference
< XTypeConverter
> xC
= getTypeConverter();
384 xC
->convertToSimpleType( Value
, TypeClass_STRING
) >>= aName
;
386 throw CannotConvertException();
389 OGuard
aGuard( Application::GetSolarMutex() );
390 OStringBuffer
aBuf( aName
.getLength()+8 );
391 aBuf
.append( OUStringToOString( aName
, osl_getThreadTextEncoding() ) );
392 ResMgr
* pRM
= ResMgr::CreateResMgr( aBuf
.getStr() );
394 throw InvocationTargetException();
398 aFileName
= OStringToOUString( aBuf
.makeStringAndClear(), osl_getThreadTextEncoding() );
402 Reference
< XInvocation
> xI
= getDefaultInvocation();
404 xI
->setValue( PropertyName
, Value
);
406 throw UnknownPropertyException();
411 Any SAL_CALL
ResourceService::getValue(const OUString
& PropertyName
)
412 throw(UnknownPropertyException
, RuntimeException
)
414 OGuard
aGuard( Application::GetSolarMutex() );
415 if( PropertyName
.equalsAscii("FileName" ))
416 return makeAny( aFileName
);
418 Reference
< XInvocation
> xI
= getDefaultInvocation();
420 return xI
->getValue( PropertyName
);
422 throw UnknownPropertyException();
426 BOOL SAL_CALL
ResourceService::hasMethod(const OUString
& Name
)
427 throw(RuntimeException
)
429 if( Name
.equalsAscii("getString") ||
430 Name
.equalsAscii("getStrings") ||
431 Name
.equalsAscii("hasString") ||
432 Name
.equalsAscii("hasStrings") ||
433 Name
.equalsAscii("getStringList") ||
434 Name
.equalsAscii("hasStringList")
439 Reference
< XInvocation
> xI
= getDefaultInvocation();
441 return xI
->hasMethod( Name
);
448 BOOL SAL_CALL
ResourceService::hasProperty(const OUString
& Name
)
449 throw(RuntimeException
)
451 if( Name
.equalsAscii("FileName") )
455 Reference
< XInvocation
> xI
= getDefaultInvocation();
457 return xI
->hasProperty( Name
);
465 ComponentInfo
getComponentInfo_VclStringResourceLoader()
468 aInfo
.aSupportedServices
= ResourceService::getSupportedServiceNames_Static();
469 aInfo
.sImplementationName
= ResourceService::getImplementationName_Static();
470 aInfo
.pFactory
= &ResourceService::Create
;