diff options
author | Jörg Barfurth <jb@openoffice.org> | 2001-05-28 14:33:37 +0000 |
---|---|---|
committer | Jörg Barfurth <jb@openoffice.org> | 2001-05-28 14:33:37 +0000 |
commit | 3492abfdff1f94ded28f6bf2b7a62f021741e973 (patch) | |
tree | 1581a2cad62d7b560814c6c877d6e67e12557494 /configmgr | |
parent | b7929a06039cf8f85b3f8590d6fc2a760cedc513 (diff) |
#87512# Improved argument handling for access instantiation.
Diffstat (limited to 'configmgr')
-rw-r--r-- | configmgr/source/api2/confproviderimpl2.cxx | 57 | ||||
-rw-r--r-- | configmgr/source/api2/providerimpl.cxx | 322 | ||||
-rw-r--r-- | configmgr/source/api2/providerimpl.hxx | 82 |
3 files changed, 279 insertions, 182 deletions
diff --git a/configmgr/source/api2/confproviderimpl2.cxx b/configmgr/source/api2/confproviderimpl2.cxx index c44a7537d5a7..f5c8781a9868 100644 --- a/configmgr/source/api2/confproviderimpl2.cxx +++ b/configmgr/source/api2/confproviderimpl2.cxx @@ -2,9 +2,9 @@ * * $RCSfile: confproviderimpl2.cxx,v $ * - * $Revision: 1.22 $ + * $Revision: 1.23 $ * - * last change: $Author: jb $ $Date: 2001-04-03 16:33:57 $ + * last change: $Author: jb $ $Date: 2001-05-28 15:33:37 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -81,8 +81,6 @@ #include <com/sun/star/beans/PropertyValue.hpp> #endif -#include "hashhelper.hxx" - namespace configmgr { namespace css = ::com::sun::star; @@ -109,15 +107,18 @@ namespace configmgr CFG_TRACE_INFO("config provider: going to create a read access instance"); // extract the args - ::rtl::OUString sUser, sPath, sLocale; + OUString sPath; sal_Int32 nLevels; - bool bNoCache; - bool bLazyWrite; // no need here + vos::ORef<OOptions> xOptions = new OOptions(getDefaultOptions()); - OProviderImpl::FactoryArguments::extractArgs(aArgs, sPath, sUser, sLocale, nLevels, bNoCache, bLazyWrite); + OProviderImpl::FactoryArguments::extractArgs(aArgs, sPath, nLevels, xOptions); - vos::ORef<OOptions> xOptions = new OOptions(getDefaultOptions()); + CFG_TRACE_INFO_NI("config provider: node accessor extracted from the args is %s", OUSTRING2ASCII(sPath)); + CFG_TRACE_INFO_NI("config provider: level depth extracted from the args is %i", nLevels); + + if (!xOptions->canUseCache()) CFG_TRACE_INFO_NI("config provider: Ignoring cache for request"); + OUString sUser = xOptions->getUser(); if (sUser.getLength()) { if (xOptions->getDefaultUser() == sUser) @@ -142,13 +143,6 @@ namespace configmgr } } - xOptions->setLocale(sLocale); - if (bNoCache) xOptions->setNoCache(bNoCache); - - CFG_TRACE_INFO_NI("config provider: node accessor extracted from the args is %s", OUSTRING2ASCII(sPath)); - CFG_TRACE_INFO_NI("config provider: level depth extracted from the args is %i", nLevels); - if (!xOptions->canUseCache()) CFG_TRACE_INFO_NI("config provider: Ignoring cache for request"); - // create the access object uno::Reference< uno::XInterface > xReturn; try @@ -182,23 +176,18 @@ namespace configmgr CFG_TRACE_INFO("config provider: going to create an update access instance"); // extract the args - ::rtl::OUString sUser, sPath, sLocale; + OUString sPath; sal_Int32 nLevels; - bool bNoCache; - bool bLazyWrite; + vos::ORef<OOptions> xOptions = new OOptions(getDefaultOptions()); - OProviderImpl::FactoryArguments::extractArgs(aArgs, sPath, sUser, sLocale, nLevels, bNoCache, bLazyWrite); -#ifdef LLA_PRIVAT_DEBUG - // HACK for Test only - ConfigurationName aName(sPath); - if (!aName.isEmpty() && aName.moduleName().equalsIgnoreCase(ASCII("org.openoffice.Office.Common"))) - { - // bLazyWrite = true; - } - bLazyWrite = true; -#endif + OProviderImpl::FactoryArguments::extractArgs(aArgs, sPath, nLevels, xOptions); - vos::ORef<OOptions> xOptions = new OOptions(getDefaultOptions()); + CFG_TRACE_INFO_NI("config provider: node accessor extracted from the args is %s", OUSTRING2ASCII(sPath)); + CFG_TRACE_INFO_NI("config provider: level depth extracted from the args is %i", nLevels); + + if (!xOptions->canUseCache()) CFG_TRACE_INFO_NI("config provider: Ignoring cache for request"); + + OUString sUser = xOptions->getUser(); if (sUser.getLength()) { if (xOptions->getDefaultUser() == sUser) @@ -223,14 +212,6 @@ namespace configmgr } } - if (sLocale.getLength()) xOptions->setLocale(sLocale); - if (bNoCache) xOptions->setNoCache(bNoCache); - if (bLazyWrite) xOptions->setLazyWrite(bLazyWrite); - - CFG_TRACE_INFO_NI("config provider: node accessor extracted from the args is %s", OUSTRING2ASCII(sPath)); - CFG_TRACE_INFO_NI("config provider: level depth extracted from the args is %i", nLevels); - if (!xOptions->canUseCache()) CFG_TRACE_INFO_NI("config provider: Ignoring cache for request"); - // create the access object uno::Reference< uno::XInterface > xReturn; try diff --git a/configmgr/source/api2/providerimpl.cxx b/configmgr/source/api2/providerimpl.cxx index efea102bdb6f..1d1fd1e8b2c5 100644 --- a/configmgr/source/api2/providerimpl.cxx +++ b/configmgr/source/api2/providerimpl.cxx @@ -2,9 +2,9 @@ * * $RCSfile: providerimpl.cxx,v $ * - * $Revision: 1.31 $ + * $Revision: 1.32 $ * - * last change: $Author: jb $ $Date: 2001-05-18 16:20:59 $ + * last change: $Author: jb $ $Date: 2001-05-28 15:33:37 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -509,157 +509,235 @@ namespace configmgr //============================================================================= //= OProvider::FactoryArguments //============================================================================= - rtl::OUString OProviderImpl::FactoryArguments::sUser(ASCII("user")); - rtl::OUString OProviderImpl::FactoryArguments::sNodePath(ASCII("nodepath")); - rtl::OUString OProviderImpl::FactoryArguments::sDepth(ASCII("depth")); - rtl::OUString OProviderImpl::FactoryArguments::sLocale(ASCII("locale")); - rtl::OUString OProviderImpl::FactoryArguments::sNoCache(ASCII("nocache")); - rtl::OUString OProviderImpl::FactoryArguments::sLazyWrite(ASCII("lazywrite")); - -#ifdef DBG_UTIL - //----------------------------------------------------------------------------- - bool lookup(const rtl::OUString& rName) + + sal_Char const * const OProviderImpl::FactoryArguments::asciiArgumentNames[] = { - // allowed arguments - static HashSet aArgs; - if (aArgs.empty()) + "nodepath", // ARG_NODEPATH, // requested node path + "depth", // ARG_DEPTH, // depth of the tree + "user", // ARG_USER, // name of the user - only for admin + "locale", // ARG_LOCALE, // desired locale + "nocache", // ARG_NOCACHE, // cache disabling + "lazywrite" // ARG_ASYNC, // lasy write data + }; + + OUString OProviderImpl::FactoryArguments::getArgumentName(Argument _which) SAL_THROW( () ) + { + OSL_ASSERT(sizeof asciiArgumentNames/sizeof 0[asciiArgumentNames] == _arg_count); + OSL_PRECOND(_which < _arg_count, "Illegal argument selector in OProviderImpl::FactoryArguments::getArgumentName"); + + return OUString::createFromAscii(asciiArgumentNames[_which]); + } + + OProviderImpl::FactoryArguments::Argument + OProviderImpl::FactoryArguments::lookupArgument(const rtl::OUString& rName) + SAL_THROW( () ) + { + OUString sCheck( rName.toAsciiLowerCase() ); + + typedef sal_Char const * const * ArgNameIter; + + ArgNameIter const pFirst = asciiArgumentNames; + ArgNameIter const pLast = pFirst + _arg_count; + + ArgNameIter it = pFirst; + + for(; it != pLast; ++it) { - aArgs.insert(OProviderImpl::FactoryArguments::sUser); - aArgs.insert(OProviderImpl::FactoryArguments::sNodePath); - aArgs.insert(OProviderImpl::FactoryArguments::sDepth); - aArgs.insert(OProviderImpl::FactoryArguments::sLocale); - aArgs.insert(OProviderImpl::FactoryArguments::sNoCache); - aArgs.insert(OProviderImpl::FactoryArguments::sLazyWrite); + if (0 == sCheck.compareToAscii(*it)) + { + break; + } } - HashSet::const_iterator it = aArgs.find(rName); - return it != aArgs.end() ? true : false; + OSL_ASSERT( Argument(pLast-pFirst) == ARG_NOT_FOUND ); + return static_cast<Argument>(it - pFirst); } - //----------------------------------------------------------------------------- - bool checkArgs(const uno::Sequence<uno::Any>& _rArgs) throw (lang::IllegalArgumentException) - { - // PRE: a Sequence with some possible arguments - beans::PropertyValue aCurrent; - const uno::Any* pCurrent = _rArgs.getConstArray(); + //----------------------------------------------------------------------------------- - bool bParamOk = false; - for (sal_Int32 i=0; i<_rArgs.getLength(); ++i, ++pCurrent) + bool OProviderImpl::FactoryArguments::extractOneArgument(beans::PropertyValue const& aCurrent, + OUString& /* [out] */ _rNodeAccessor, + sal_Int32& /* [out] */ _nLevels, + vos::ORef<OOptions> /* [in/out] */ _xOptions ) + SAL_THROW( () ) + { + switch ( lookupArgument(aCurrent.Name) ) { - if (*pCurrent >>= aCurrent) + case ARG_NODEPATH: { - if (!lookup(aCurrent.Name)) + OUString sStringVal; + if (aCurrent.Value >>= sStringVal) + _rNodeAccessor = sStringVal; + else + return false; + } + break; + + case ARG_DEPTH: + { + sal_Int32 nIntVal; + if (aCurrent.Value >>= nIntVal) + _nLevels = nIntVal; + else + return false; + } + break; + + case ARG_USER: + { + OUString sStringVal; + if (aCurrent.Value >>= sStringVal) + _xOptions->setUser(sStringVal); + else + return false; + } + break; + + case ARG_LOCALE: + { + OUString sStringVal; + if (aCurrent.Value >>= sStringVal) + { + _xOptions->setLocale(sStringVal); + break; + } + #if 0 + lang::Locale aLocale; + if (aCurrent.Value >>= aLocale) { - rtl::OString aStr = "The argument '"; - aStr += rtl::OUStringToOString(aCurrent.Name,RTL_TEXTENCODING_ASCII_US).getStr(); - aStr += "' could not be extracted."; - OSL_ENSURE(false, aStr.getStr()); + _xOptions->setLocale(aLocale); + break; } + #endif + + return false; } - else if (i > 0 || pCurrent->getValueTypeClass() != uno::TypeClass_STRING) + break; + + case ARG_NOCACHE: { - OSL_ENSURE(false, "operator >>= failed."); + sal_Bool bBoolVal; + if (aCurrent.Value >>= bBoolVal) + _xOptions->setNoCache(!!bBoolVal); + else + return false; } - else + break; + + case ARG_ASYNC: { - OSL_ENSURE(_rArgs.getLength() <= 2, "Too many arguments for legacy parameters."); - break; + sal_Bool bBoolVal; + if (aCurrent.Value >>= bBoolVal) + _xOptions->setLazyWrite(!!bBoolVal); + else + return false; } + break; + + case ARG_NOT_FOUND: + { + rtl::OString sMessage(RTL_CONSTASCII_STRINGPARAM("Unknown argument \"")); + sMessage += rtl::OUStringToOString(aCurrent.Name, RTL_TEXTENCODING_ASCII_US); + sMessage += rtl::OString(RTL_CONSTASCII_STRINGPARAM("\" !\n- Parameter will be ignored -\n")); + CFG_TRACE_WARNING( "provider: %s", sMessage.getStr() ); + #ifdef DBG_UTIL + OSL_ENSURE(false, sMessage.getStr()); + #endif + } + break; + + default: + CFG_TRACE_ERROR( "Known argument is not handled" ); + OSL_ENSURE(false, "Known argument is not handled"); + break; } return true; } -#endif + + //----------------------------------------------------------------------------------- + static + void failIllegal(sal_Int32 _nArg = -1) + SAL_THROW( (lang::IllegalArgumentException) ) + { + OSL_ENSURE( sal_Int16(_nArg) == _nArg, "Argument number out of range. Raising imprecise exception."); + throw lang::IllegalArgumentException( + OUString(RTL_CONSTASCII_USTRINGPARAM("Configuration Provider: Arguments must be PropertyValues.")), + uno::Reference<uno::XInterface>(), + sal_Int16(_nArg) + ); + } + + //----------------------------------------------------------------------------------- + static + bool extractLegacyArguments( const uno::Sequence<uno::Any>& _rArgs, + OUString& /* [out] */ _rNodeAccessor, + sal_Int32& /* [out] */ _nLevels ) + SAL_THROW( () ) + { + // compatibility : formerly, you could specify the node path as first arg and the (optional) depth + // as second arg + switch (_rArgs.getLength() ) + { + default: + return false; + + case 2: + if (! (_rArgs[1] >>= _nLevels) ) + return false; + + // fall thru + case 1: + if (! (_rArgs[0] >>= _rNodeAccessor) ) + return false; + + // fall thru + case 0: + return true; + } + } //----------------------------------------------------------------------------------- - void OProviderImpl::FactoryArguments::extractArgs(const uno::Sequence<uno::Any>& _rArgs, - OUString& /* [out] */ _rNodeAccessor, - OUString& /* [out] */ _rUser, - OUString& /* [out] */ _rLocale, - sal_Int32& /* [out] */ _nLevels, - bool& /* [out] */ _bNoCache, - bool& /* [out] */ _bLazyWrite) - throw (lang::IllegalArgumentException) - { - -#ifdef DBG_UTIL - checkArgs(_rArgs); -#endif - ::rtl::OUString sUser, sPath, sLocale; - sal_Int32 nLevelDepth = ITreeProvider::ALL_LEVELS; - sal_Bool bNoCache = sal_False; - sal_Bool bLazyWrite = sal_False; - - // the args have to be a sequence of property values, currently three property names are recognized - beans::PropertyValue aCurrent; - sal_Bool bAnyPropValue = sal_False; - const uno::Any* pCurrent = _rArgs.getConstArray(); - for (sal_Int32 i=0; i<_rArgs.getLength(); ++i, ++pCurrent) + void OProviderImpl::FactoryArguments::extractArgs( const uno::Sequence<uno::Any>& _rArgs, + OUString& /* [out] */ _rNodeAccessor, + sal_Int32& /* [out] */ _nLevels, + vos::ORef<OOptions> /* [in/out] */ _xOptions ) + SAL_THROW( (lang::IllegalArgumentException) ) + { + _nLevels = ITreeProvider::ALL_LEVELS; // setting a fallback + + // the args have to be a sequence of property values + bool bLegacyFormat = false; + + for (sal_Int32 i=0; i<_rArgs.getLength(); ++i) { - if (*pCurrent >>= aCurrent) + beans::PropertyValue aCurrent; + if (_rArgs[i] >>= aCurrent) { - sal_Bool bExtractSuccess = sal_True; // defaulted to TRUE, so we skip unknown arguments - if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sNodePath)) - bExtractSuccess = (aCurrent.Value >>= sPath); - else if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sUser)) - bExtractSuccess = (aCurrent.Value >>= sUser); - else if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sDepth)) - bExtractSuccess = (aCurrent.Value >>= nLevelDepth); - else if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sLocale)) - bExtractSuccess = (aCurrent.Value >>= sLocale); - else if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sNoCache)) - bExtractSuccess = (aCurrent.Value >>= bNoCache); - else if (aCurrent.Name.equalsIgnoreAsciiCase(OProviderImpl::FactoryArguments::sLazyWrite)) - bExtractSuccess = (aCurrent.Value >>= bLazyWrite); -/* -#ifdef DBG_UTIL - else + if ( !extractOneArgument(aCurrent,_rNodeAccessor,_nLevels,_xOptions) ) { - ::rtl::OString sMessage(RTL_CONSTASCII_STRINGPARAM("OProviderImpl::extractArgs : unknown argument name: ")); - sMessage += ::rtl::OString(aCurrent.Name.getStr(), aCurrent.Name.getLength(), RTL_TEXTENCODING_ASCII_US); - sMessage += ::rtl::OString(RTL_CONSTASCII_STRINGPARAM("!")); - OSL_ENSURE(sal_False, sMessage.getStr()); + OUString sMessage(RTL_CONSTASCII_USTRINGPARAM("The argument ")); + sMessage += aCurrent.Name; + sMessage += OUString(RTL_CONSTASCII_USTRINGPARAM(" has the wrong type.\n- Found type ")); + sMessage += aCurrent.Value.getValueType().getTypeName(); + throw lang::IllegalArgumentException(sMessage,uno::Reference<uno::XInterface>(),sal_Int16(i)); } -#endif -*/ - - if (!bExtractSuccess) - throw lang::IllegalArgumentException( - (OUString(RTL_CONSTASCII_USTRINGPARAM("The argument ")) += aCurrent.Name) += OUString(RTL_CONSTASCII_USTRINGPARAM(" could not be extracted.")), - uno::Reference<uno::XInterface>(), - sal_Int16(i) - ); + } + else + { + if (i == 0)// try compatibility format + if ( extractLegacyArguments(_rArgs,_rNodeAccessor,_nLevels)) + break; - bAnyPropValue = sal_True; + failIllegal(i); + OSL_ASSERT(false); } } - if (!bAnyPropValue) + if (_rNodeAccessor.getLength() == 0) { - // compatibility : formerly, you could specify the node path as first arg and the (optional) depth - // as second arg - if (_rArgs.getLength() > 0) - if (! (_rArgs[0] >>= sPath) ) - throw lang::IllegalArgumentException( - OUString(RTL_CONSTASCII_USTRINGPARAM("The node path specified is invalid.")), - uno::Reference<uno::XInterface>(), - 0 - ); - - if (_rArgs.getLength() > 1) - if (! (_rArgs[1] >>= nLevelDepth) ) - throw lang::IllegalArgumentException( - OUString(RTL_CONSTASCII_USTRINGPARAM("The fetch depth specified is invalid.")), - uno::Reference<uno::XInterface>(), - 1 - ); + OUString sMessage(RTL_CONSTASCII_USTRINGPARAM("Configuration Provider: Missing argument: no nodepath was provided")); + throw lang::IllegalArgumentException(sMessage,uno::Reference<uno::XInterface>(),-1); } - - _rNodeAccessor = sPath; //IConfigSession::composeNodeAccessor(sPath, sUser); - _nLevels = nLevelDepth; - _rLocale = sLocale; - _rUser = sUser; - _bNoCache = (bNoCache != sal_False); - _bLazyWrite = bLazyWrite; } // class OOptions //.......................................................................... diff --git a/configmgr/source/api2/providerimpl.hxx b/configmgr/source/api2/providerimpl.hxx index fec9df9131d7..dc569cac0620 100644 --- a/configmgr/source/api2/providerimpl.hxx +++ b/configmgr/source/api2/providerimpl.hxx @@ -2,9 +2,9 @@ * * $RCSfile: providerimpl.hxx,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: jb $ $Date: 2001-04-19 15:46:27 $ + * last change: $Author: jb $ $Date: 2001-05-28 15:33:37 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -82,12 +82,19 @@ #include "options.hxx" -namespace com { namespace sun { namespace star { namespace uno { - class Any; - class XInterface; - template <class> class Sequence; - template <class> class Reference; -} } } } +namespace com { namespace sun { namespace star { + namespace uno + { + class Any; + class XInterface; + template <class> class Sequence; + template <class> class Reference; + } + namespace beans + { + struct PropertyValue; + } +} } } namespace rtl { class OUString; } @@ -97,6 +104,7 @@ namespace configmgr namespace uno = css::uno; namespace script = css::script; namespace lang = css::lang; + namespace beans = css::beans; using ::rtl::OUString; class ISubtree; @@ -125,21 +133,51 @@ namespace configmgr struct FactoryArguments { /// possible arguments, given only in small letters. - static rtl::OUString sUser; // name of the user - static rtl::OUString sNodePath; // requested node path - static rtl::OUString sDepth; // depth of the tree - static rtl::OUString sLocale; // desired locale - static rtl::OUString sNoCache; // cache disabling - static rtl::OUString sLazyWrite; // lasy write data - + enum Argument + { + ARG_NODEPATH, // requested node path + ARG_DEPTH, // depth of the tree + ARG_USER, // name of the user - only for admin + ARG_LOCALE, // desired locale + ARG_NOCACHE, // cache disabling + ARG_ASYNC, // lasy write data + + _arg_count, + ARG_NOT_FOUND = _arg_count + }; + static sal_Char const * const asciiArgumentNames[]; + + static OUString getArgumentName(Argument _which) SAL_THROW( () ); + static Argument lookupArgument(OUString const& sArgumentName) SAL_THROW( () ); + + static OUString getUserArgumentName() SAL_THROW(()) { return getArgumentName(ARG_USER); } + static OUString getNodePathArgumentName() SAL_THROW(()) { return getArgumentName(ARG_NODEPATH); } + static OUString getDepthArgumentNameArgumentName() SAL_THROW(()) { return getArgumentName(ARG_DEPTH); } + static OUString getLocaleArgumentName() SAL_THROW(()) { return getArgumentName(ARG_LOCALE); } + static OUString getNoCacheArgumentName() SAL_THROW(()) { return getArgumentName(ARG_NOCACHE); } + static OUString getAsyncArgumentName() SAL_THROW(()) { return getArgumentName(ARG_ASYNC); } public: - static void extractArgs(const uno::Sequence<uno::Any>& _rArgs, - ::rtl::OUString& /* [out] */ _rNodeAccessor, - ::rtl::OUString& /* [out] */ _rUser, - ::rtl::OUString& /* [out] */ _rLocale, - sal_Int32& /* [out] */ _nLevels, - bool& /* [out] */ _bNoCache, - bool& /* [out] */ _bLazyWrite) throw (lang::IllegalArgumentException); + /** extracts arguments from the argument sequence into to the parameter variables + + <p>unknown arguments are ignored</p> + + @throws com::sun::star::lang::IllegalArgumentException + if an element of _rArgs had the wrong type or + if the value of a known argument has the wrong type or + if the value of a known argument is out of range (sometimes) + or if no non-empty node path argument could be extracted, + */ + static void extractArgs( const uno::Sequence<uno::Any>& _rArgs, + OUString& /* [out] */ _rNodeAccessor, + sal_Int32& /* [out] */ _nLevels, + vos::ORef<OOptions> /* [in/out] */ xOptions + ) SAL_THROW( (lang::IllegalArgumentException) ); + + static bool extractOneArgument( beans::PropertyValue const& aCurrent, + OUString& /* [out] */ _rNodeAccessor, + sal_Int32& /* [out] */ _nLevels, + vos::ORef<OOptions> /* [in/out] */ _xOptions + ) SAL_THROW( () ); }; |