From 585a096fdba5f7634f7876b6617d9f9f5ac03ff3 Mon Sep 17 00:00:00 2001 From: maroy Date: Wed, 1 Dec 2004 20:07:37 +0000 Subject: [PATCH] added capability to Localized object to load a resource bundle based on information in a configuration file --- .../modules/core/etc/resourceBundle.xml | 11 ++++ .../LiveSupport/Core/LocalizedObject.h | 58 +++++++++++++++++- .../modules/core/src/LocalizedObject.cxx | 59 ++++++++++++++++++- .../modules/core/src/LocalizedObjectTest.cxx | 42 ++++++++++++- .../modules/core/src/LocalizedObjectTest.h | 14 ++++- 5 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 livesupport/modules/core/etc/resourceBundle.xml diff --git a/livesupport/modules/core/etc/resourceBundle.xml b/livesupport/modules/core/etc/resourceBundle.xml new file mode 100644 index 000000000..2af0d220f --- /dev/null +++ b/livesupport/modules/core/etc/resourceBundle.xml @@ -0,0 +1,11 @@ + + + + + +]> + diff --git a/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h b/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h index 3600b97b0..101b46f76 100644 --- a/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h +++ b/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.3 $ + Version : $Revision: 1.4 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h,v $ ------------------------------------------------------------------------------*/ @@ -44,6 +44,7 @@ #include #include +#include #include "LiveSupport/Core/Ptr.h" @@ -63,11 +64,16 @@ namespace Core { * to make localized life easier. * * @author $Author: maroy $ - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class LocalizedObject { private: + /** + * The name of the configuration XML elmenent used by this object. + */ + static const std::string configElementNameStr; + /** * The resource bundle holding the localized resources for this * object. @@ -102,6 +108,54 @@ class LocalizedObject { } + /** + * Return the name of the XML element that is expected + * to be sent to a call to getBundle(). + * + * @return the name of the expected XML configuration element. + * @see #getBundle(const xmlpp::Element &) + */ + static const std::string + getConfigElementName(void) throw () + { + return configElementNameStr; + } + + /** + * Load a resource bundle based on an XML configuration element. + * + * The DTD for the lement to be supplied to this function is: + *

+         *  
+         *  
+         *  
+         *  ]>
+         *  
+ * + * a sample configuration element is as follows: + * + *

+         *  
+         *  
+ * + * for an overview of resource bundle parameters, see the ICU + * documentation on + * resource management + * + * @param element the XML configuration element + * @return the resource bundle, based on this element. + * @exception std::invalid_argument if the supplied element is not + * a proper resource bundle configuration element. + * @see http://oss.software.ibm.com/icu/userguide/ResourceManagement.html + */ + static Ptr::Ref + getBundle(const xmlpp::Element & element) + throw (std::invalid_argument); + /** * Get the resource bundle for this object. * diff --git a/livesupport/modules/core/src/LocalizedObject.cxx b/livesupport/modules/core/src/LocalizedObject.cxx index b97c1d9bb..456770d85 100644 --- a/livesupport/modules/core/src/LocalizedObject.cxx +++ b/livesupport/modules/core/src/LocalizedObject.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.3 $ + Version : $Revision: 1.4 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObject.cxx,v $ ------------------------------------------------------------------------------*/ @@ -45,12 +45,69 @@ using namespace LiveSupport::Core; /* ================================================ local constants & macros */ +/*------------------------------------------------------------------------------ + * The name of the config element for this class + *----------------------------------------------------------------------------*/ +const std::string LocalizedObject::configElementNameStr = "resourceBundle"; + +/** + * The name of the attribute to get the path of the resource bundle. + */ +static const std::string pathAttrName = "path"; + +/** + * The name of the attribute to get the locale of the resource bundle. + */ +static const std::string localeAttrName = "locale"; + /* =============================================== local function prototypes */ /* ============================================================= module code */ +/*------------------------------------------------------------------------------ + * Load a resource bunlde based on an XML configuration element. + *----------------------------------------------------------------------------*/ +Ptr::Ref +LocalizedObject :: getBundle(const xmlpp::Element & element) + throw (std::invalid_argument) +{ + if (element.get_name() != configElementNameStr) { + std::string eMsg = "Bad configuration element "; + eMsg += element.get_name(); + throw std::invalid_argument(eMsg); + } + + const xmlpp::Attribute * attribute; + + if (!(attribute = element.get_attribute(pathAttrName))) { + std::string eMsg = "Missing attribute "; + eMsg += pathAttrName; + throw std::invalid_argument(eMsg); + } + std::string path = attribute->get_value(); + + if (!(attribute = element.get_attribute(localeAttrName))) { + std::string eMsg = "Missing attribute "; + eMsg += localeAttrName; + throw std::invalid_argument(eMsg); + } + std::string locale = attribute->get_value(); + + UErrorCode status = U_ZERO_ERROR; + Ptr::Ref resourceBundle( + new ResourceBundle(path.c_str(), + locale.c_str(), + status)); + if (!U_SUCCESS(status)) { + throw std::invalid_argument("opening resource bundle a failure"); + } + + return resourceBundle; +} + + /*------------------------------------------------------------------------------ * Get a resource bundle by the specified key *----------------------------------------------------------------------------*/ diff --git a/livesupport/modules/core/src/LocalizedObjectTest.cxx b/livesupport/modules/core/src/LocalizedObjectTest.cxx index 3d8516974..2fb5b7585 100644 --- a/livesupport/modules/core/src/LocalizedObjectTest.cxx +++ b/livesupport/modules/core/src/LocalizedObjectTest.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObjectTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -50,6 +50,11 @@ using namespace LiveSupport::Core; CPPUNIT_TEST_SUITE_REGISTRATION(LocalizedObjectTest); +/** + * The name of the configuration file for the resource bundle. + */ +static const std::string configFileName = "etc/resourceBundle.xml"; + /* =============================================== local function prototypes */ @@ -225,3 +230,38 @@ LocalizedObjectTest :: formatMessageTest(void) } +/*------------------------------------------------------------------------------ + * Test to see if resource bundle can be loaded based on a config file + *----------------------------------------------------------------------------*/ +void +LocalizedObjectTest :: loadFromConfig(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref bundle; + + try { + Ptr::Ref parser( + new xmlpp::DomParser(configFileName, true)); + const xmlpp::Document * document = parser->get_document(); + const xmlpp::Element * root = document->get_root_node(); + + bundle = LocalizedObject::getBundle(*root); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL("semantic error in configuration file"); + } catch (std::exception &e) { + CPPUNIT_FAIL(e.what()); + } + CPPUNIT_ASSERT(bundle.get()); + + // now, see if this really is the en bundle + try { + Ptr::Ref locObj(new LocalizedObject(bundle)); + Ptr::Ref section1(new LocalizedObject( + locObj->getBundle("section1"))); + Ptr::Ref foo = section1->getResourceString("foo"); + CPPUNIT_ASSERT(foo->compare("fou") == 0); + } catch (std::invalid_argument &e) { + CPPUNIT_FAIL(e.what()); + } +} + diff --git a/livesupport/modules/core/src/LocalizedObjectTest.h b/livesupport/modules/core/src/LocalizedObjectTest.h index 868a2dce9..ce074e017 100644 --- a/livesupport/modules/core/src/LocalizedObjectTest.h +++ b/livesupport/modules/core/src/LocalizedObjectTest.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/LocalizedObjectTest.h,v $ ------------------------------------------------------------------------------*/ @@ -58,7 +58,7 @@ namespace Core { * Unit test for the LocalizedObject class. * * @author $Author: maroy $ - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * @see LocalizedObject */ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture @@ -68,6 +68,7 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(fallbackTest); CPPUNIT_TEST(unicodeTest); CPPUNIT_TEST(formatMessageTest); + CPPUNIT_TEST(loadFromConfig); CPPUNIT_TEST_SUITE_END(); protected: @@ -105,6 +106,15 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture void formatMessageTest(void) throw (CPPUNIT_NS::Exception); + /** + * A test to see if a resource bundle can be loaded based on a + * configuration file + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + loadFromConfig(void) throw (CPPUNIT_NS::Exception); + public: