ResourceBundle Class Reference

#include <ResourceBundle.h>

Inheritance diagram for ResourceBundle:

fennel::FennelResource List of all members.

Public Member Functions

const set< string > getKeys () const
const string & getMessage (const string &key) const
bool hasMessage (const string &key) const
const string & getBaseName () const
const LocalegetLocale () const

Static Public Member Functions

static void setGlobalResourceFileLocation (const string &location)
static RecursiveMutexgetMutex ()

Protected Member Functions

 ResourceBundle (const string &baseName, const Locale &locale, const string &location)
virtual ~ResourceBundle ()
void setParent (ResourceBundle *bundle)

Private Member Functions

void loadMessages ()

Private Attributes

const string _baseName
const Locale _locale
const string _location
ResourceBundle_parent
map< string, string > _messages

Static Private Attributes

static RecursiveMutex mutex

Detailed Description

Definition at line 38 of file ResourceBundle.h.


Constructor & Destructor Documentation

ResourceBundle::ResourceBundle ( const string &  baseName,
const Locale locale,
const string &  location 
) [explicit, protected]

Definition at line 55 of file ResourceBundle.cpp.

References loadMessages().

00059     : _baseName(baseName),
00060       _locale(locale),
00061       _location(location),
00062       _parent(NULL)
00063 {
00064     loadMessages();
00065 }

ResourceBundle::~ResourceBundle (  )  [protected, virtual]

Definition at line 67 of file ResourceBundle.cpp.

00068 {
00069 }


Member Function Documentation

void ResourceBundle::setParent ( ResourceBundle bundle  )  [protected]

Definition at line 71 of file ResourceBundle.cpp.

References _parent.

00072 {
00073     _parent = bundle;
00074 }

const set< string > ResourceBundle::getKeys (  )  const

Definition at line 76 of file ResourceBundle.cpp.

References _messages, _parent, and getKeys().

Referenced by getKeys().

00077 {
00078     set<string> keys;
00079 
00080     map<string, string>::const_iterator iter = _messages.begin(),
00081         end = _messages.end();
00082 
00083     while (iter != end) {
00084         keys.insert((*iter).first);
00085         iter++;
00086     }
00087 
00088     if (_parent) {
00089         set<string> parentKeys = _parent->getKeys();
00090 
00091         keys.insert(parentKeys.begin(), parentKeys.end());
00092     }
00093 
00094     return keys;
00095 }

const string & ResourceBundle::getMessage ( const string &  key  )  const

Definition at line 99 of file ResourceBundle.cpp.

References _messages, _parent, and MISSING_KEY().

Referenced by ResourceDefinition::format(), and ResourceDefinition::prepareFormatter().

00100 {
00101     map<string, string>::const_iterator iter;
00102     iter = _messages.find(key);
00103     if (iter == _messages.end()) {
00104         if (_parent) {
00105             return _parent->getMessage(key);
00106         }
00107 
00108         return MISSING_KEY;
00109     }
00110 
00111     return (*iter).second;
00112 }

bool ResourceBundle::hasMessage ( const string &  key  )  const

Definition at line 115 of file ResourceBundle.cpp.

References _messages, _parent, and hasMessage().

Referenced by ResourceDefinition::format(), hasMessage(), and ResourceDefinition::prepareFormatter().

00116 {
00117     return
00118         _messages.find(key) != _messages.end()
00119         || (_parent && _parent->hasMessage(key));
00120 }

const string & ResourceBundle::getBaseName (  )  const

Definition at line 128 of file ResourceBundle.cpp.

References _baseName.

Referenced by ResourceDefinition::format(), and ResourceDefinition::prepareFormatter().

00129 {
00130     return _baseName;
00131 }

const Locale & ResourceBundle::getLocale (  )  const

Definition at line 122 of file ResourceBundle.cpp.

References _locale.

Referenced by ResourceDefinition::format(), and ResourceDefinition::prepareFormatter().

00123 {
00124     return _locale;
00125 }

void ResourceBundle::setGlobalResourceFileLocation ( const string &  location  )  [static]

Definition at line 50 of file ResourceBundle.cpp.

References globalResourceLocation().

00051 {
00052     globalResourceLocation = location;
00053 }

RecursiveMutex & ResourceBundle::getMutex (  )  [static]

Definition at line 45 of file ResourceBundle.cpp.

References mutex.

Referenced by makeInstance().

00046 {
00047     return mutex;
00048 }

void ResourceBundle::loadMessages (  )  [private]

Definition at line 245 of file ResourceBundle.cpp.

References _baseName, _locale, _location, _messages, convertPropertyToBoost(), Locale::getDisplayName(), and globalResourceLocation().

Referenced by ResourceBundle().

00246 {
00247     fstream in;
00248 
00249     // e.g. GeneratedResourceBundle_en_US.resources
00250     string fileName;
00251 
00252     if (_locale == Locale("")) {
00253         fileName.assign(_baseName
00254                         + ".properties");
00255     } else {
00256         fileName.assign(_baseName
00257                         + "_"
00258                         + _locale.getDisplayName()
00259                         + ".properties");
00260     }
00261 
00262     // look in _location first
00263     bool tryGlobalLocation = true;
00264     if (!_location.empty()) {
00265         string path = _location + "/" + fileName;
00266         in.open(path.c_str(), ios::in);
00267         if (in.good()) {
00268             tryGlobalLocation = false;
00269         }
00270     }
00271 
00272     // failing that, try the gobal location, if any
00273     if (tryGlobalLocation) {
00274         bool tryEnvVar = true;
00275 
00276         // TODO jvs 18-Feb-2004: once Fennel starts using Boost's
00277         // platform-independent filesystem library, use it here too.
00278         if (!globalResourceLocation.empty()) {
00279             string path = globalResourceLocation + "/" + fileName;
00280             in.open(path.c_str(), ios::in);
00281             if (in.good()) {
00282                 tryEnvVar = false;
00283             }
00284         }
00285 
00286         if (tryEnvVar) {
00287             const char *fennelHome = getenv("FENNEL_HOME");
00288             if (fennelHome == NULL) {
00289                 return; // give up
00290             }
00291 
00292             string path = string(fennelHome) + "/common/" + fileName;
00293             in.open(path.c_str(), ios::in);
00294             if (!in.good()) {
00295                 return; // give up
00296             }
00297         }
00298     }
00299 
00300     string line, key, message;
00301     while (in.good()) {
00302         getline(in, line);
00303 
00304         if (line.length() == 0 || line[0] == '#') {
00305             // ignore blank lines and comments
00306             continue;
00307         }
00308 
00309         string::size_type pos = line.find('=');
00310         if (pos == string::npos) {
00311             // bad message format?
00312             continue;
00313         }
00314 
00315         key = line.substr(0, pos);
00316         message = line.substr(pos + 1);
00317 
00318         _messages[key] = convertPropertyToBoost(message);
00319     }
00320 
00321     in.close();
00322 }


Member Data Documentation

const string ResourceBundle::_baseName [private]

Definition at line 64 of file ResourceBundle.h.

Referenced by getBaseName(), and loadMessages().

const Locale ResourceBundle::_locale [private]

Definition at line 65 of file ResourceBundle.h.

Referenced by getLocale(), and loadMessages().

const string ResourceBundle::_location [private]

Definition at line 66 of file ResourceBundle.h.

Referenced by loadMessages().

ResourceBundle* ResourceBundle::_parent [private]

Definition at line 68 of file ResourceBundle.h.

Referenced by getKeys(), getMessage(), hasMessage(), and setParent().

map<string, string> ResourceBundle::_messages [private]

Definition at line 70 of file ResourceBundle.h.

Referenced by getKeys(), getMessage(), hasMessage(), and loadMessages().

RecursiveMutex ResourceBundle::mutex [static, private]

Definition at line 72 of file ResourceBundle.h.

Referenced by getMutex().


The documentation for this class was generated from the following files:
Generated on Mon Jun 22 04:00:45 2009 for Fennel by  doxygen 1.5.1