added capability to Localized object to load a resource bundle based on
information in a configuration file
This commit is contained in:
parent
75d4ff9502
commit
585a096fdb
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<!DOCTYPE resourceBundle [
|
||||||
|
|
||||||
|
<!ELEMENT resourceBundle EMPTY >
|
||||||
|
<!ATTLIST resourceBundle path CDATA #REQUIRED >
|
||||||
|
<!ATTLIST resourceBundle locale CDATA #REQUIRED >
|
||||||
|
|
||||||
|
]>
|
||||||
|
<resourceBundle path = "./tmp/Core"
|
||||||
|
locale = "en"
|
||||||
|
/>
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
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 $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/LocalizedObject.h,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -44,6 +44,7 @@
|
||||||
|
|
||||||
#include <unicode/resbund.h>
|
#include <unicode/resbund.h>
|
||||||
#include <unicode/fmtable.h>
|
#include <unicode/fmtable.h>
|
||||||
|
#include <libxml++/libxml++.h>
|
||||||
|
|
||||||
#include "LiveSupport/Core/Ptr.h"
|
#include "LiveSupport/Core/Ptr.h"
|
||||||
|
|
||||||
|
@ -63,11 +64,16 @@ namespace Core {
|
||||||
* to make localized life easier.
|
* to make localized life easier.
|
||||||
*
|
*
|
||||||
* @author $Author: maroy $
|
* @author $Author: maroy $
|
||||||
* @version $Revision: 1.3 $
|
* @version $Revision: 1.4 $
|
||||||
*/
|
*/
|
||||||
class LocalizedObject
|
class LocalizedObject
|
||||||
{
|
{
|
||||||
private:
|
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
|
* The resource bundle holding the localized resources for this
|
||||||
* object.
|
* 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:
|
||||||
|
* <pre><code>
|
||||||
|
* <!DOCTYPE resourceBundle [
|
||||||
|
* <!ELEMENT resourceBundle EMPTY >
|
||||||
|
* <!ATTLIST resourceBundle path CDATA #REQUIRED >
|
||||||
|
* <!ATTLIST resourceBundle locale CDATA #REQUIRED >
|
||||||
|
* ]>
|
||||||
|
* </code></pre>
|
||||||
|
*
|
||||||
|
* a sample configuration element is as follows:
|
||||||
|
*
|
||||||
|
* <pre><code>
|
||||||
|
* <resourceBundle path = "./tmp/Core"
|
||||||
|
* locale = "en"
|
||||||
|
* />
|
||||||
|
* </code></pre>
|
||||||
|
*
|
||||||
|
* for an overview of resource bundle parameters, see the ICU
|
||||||
|
* documentation on <a
|
||||||
|
* href=http://oss.software.ibm.com/icu/userguide/ResourceManagement.html>
|
||||||
|
* resource management</a>
|
||||||
|
*
|
||||||
|
* @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<ResourceBundle>::Ref
|
||||||
|
getBundle(const xmlpp::Element & element)
|
||||||
|
throw (std::invalid_argument);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the resource bundle for this object.
|
* Get the resource bundle for this object.
|
||||||
*
|
*
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
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 $
|
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 */
|
/* ================================================ 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 */
|
/* =============================================== local function prototypes */
|
||||||
|
|
||||||
|
|
||||||
/* ============================================================= module code */
|
/* ============================================================= module code */
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------
|
||||||
|
* Load a resource bunlde based on an XML configuration element.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
Ptr<ResourceBundle>::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<ResourceBundle>::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
|
* Get a resource bundle by the specified key
|
||||||
*----------------------------------------------------------------------------*/
|
*----------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
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 $
|
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);
|
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 */
|
/* =============================================== 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<ResourceBundle>::Ref bundle;
|
||||||
|
|
||||||
|
try {
|
||||||
|
Ptr<xmlpp::DomParser>::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<LocalizedObject>::Ref locObj(new LocalizedObject(bundle));
|
||||||
|
Ptr<LocalizedObject>::Ref section1(new LocalizedObject(
|
||||||
|
locObj->getBundle("section1")));
|
||||||
|
Ptr<UnicodeString>::Ref foo = section1->getResourceString("foo");
|
||||||
|
CPPUNIT_ASSERT(foo->compare("fou") == 0);
|
||||||
|
} catch (std::invalid_argument &e) {
|
||||||
|
CPPUNIT_FAIL(e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
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 $
|
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.
|
* Unit test for the LocalizedObject class.
|
||||||
*
|
*
|
||||||
* @author $Author: maroy $
|
* @author $Author: maroy $
|
||||||
* @version $Revision: 1.2 $
|
* @version $Revision: 1.3 $
|
||||||
* @see LocalizedObject
|
* @see LocalizedObject
|
||||||
*/
|
*/
|
||||||
class LocalizedObjectTest : public CPPUNIT_NS::TestFixture
|
class LocalizedObjectTest : public CPPUNIT_NS::TestFixture
|
||||||
|
@ -68,6 +68,7 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture
|
||||||
CPPUNIT_TEST(fallbackTest);
|
CPPUNIT_TEST(fallbackTest);
|
||||||
CPPUNIT_TEST(unicodeTest);
|
CPPUNIT_TEST(unicodeTest);
|
||||||
CPPUNIT_TEST(formatMessageTest);
|
CPPUNIT_TEST(formatMessageTest);
|
||||||
|
CPPUNIT_TEST(loadFromConfig);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -105,6 +106,15 @@ class LocalizedObjectTest : public CPPUNIT_NS::TestFixture
|
||||||
void
|
void
|
||||||
formatMessageTest(void) throw (CPPUNIT_NS::Exception);
|
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:
|
public:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue