util/Property.h

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002 Name:      Property.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Class used to read, store & write (java) properties.
00006 -----------------------------------------------------------------------------*/
00007 #ifndef _UTIL_PROPERTY_H
00008 #define _UTIL_PROPERTY_H
00009 
00010 #include <util/xmlBlasterDef.h>
00011 #include <map>
00012 #include <string>
00013 
00014 namespace org { namespace xmlBlaster {
00015 namespace util {
00016 
00036    class Dll_Export Property {
00037       
00038       public: typedef std::map<std::string, std::string, std::less<std::string> > MapType;
00039   
00040    private:
00041       MapType properties_;
00042       
00049       std::string replaceVariable(const std::string &key, const std::string &value, bool env);
00050 
00055       bool setProperty_(const std::string &name, const std::string &value,
00056                        bool overwrite=true);
00057 
00069       void initializeDefaultProperties();
00070 
00071    protected:
00077       bool isComment(const std::string &line) const {
00078          if (line.length() == 0) return false;
00079          return (line.c_str()[0] == '#');
00080       }
00081       
00085       std::string filter(const std::string &line) const {
00086          std::string ret;
00087          for (std::string::size_type i=0; i<line.length(); i++) {
00088             if (line.c_str()[i] != ' ') ret += line.c_str()[i];
00089          }
00090          return ret;
00091       }
00092 
00093       
00100       std::pair<const std::string, std::string> getPair(const std::string &line) const {
00101          std::string::size_type pos = line.find("=");
00102          if ((pos < 2) || (pos >= line.length()) || (isComment(line)) ) {
00103             return std::pair<const std::string, std::string>("","");
00104          }
00105          std::string name, value;
00106          name.assign(line, 0, pos);
00107          value = line.substr(pos+1);
00108          return std::pair<const std::string, std::string>(filter(name), filter(value));
00109       }
00110       
00111       
00112    public:
00113       
00121       Property(int args=0, const char * const argv[]=0);
00122 
00129       Property(MapType propMap);
00130 
00131       /*
00132        * xmlBlaster.properties is searched in this sequence:
00133        * <ul>
00134        *    <li>the command line parameter '-propertyFile', e.g. "-propertyFile /tmp/xy.properties"</li>
00135        *    <li>the environment variable 'propertyFile', e.g. "propertyFile=/tmp/xy.properties"</li>
00136        *    <li>the local directory: ./xmlBlaster.properties</li>
00137        *    <li>in your home directory, HOME/xmlBlaster.properties</li>
00138        *    <li>in the $XMLBLASTER_HOME directory</li>
00139        * </ul>
00140        * Command line properties have precedence<p />
00141        * Environment variables are weakest
00142        */
00143       void loadPropertyFile();
00144       
00145 
00146       ~Property() {
00147          properties_.erase(properties_.begin(), properties_.end());
00148       }
00149 
00154       void replaceVariables(bool env);
00155 
00156       const MapType& getPropertyMap() const {
00157          return properties_;
00158       }
00159 
00169       int readPropertyFile(const std::string &filename, bool overwrite=true);
00170       
00171 
00177       int writePropertyFile(const char *filename) const ;
00178 
00179 
00187       std::string getProperty(const std::string &name, bool env=true);
00188 
00189 
00190       bool propertyExists(const std::string &name, bool env=true);
00191 
00192 
00193       int get(const std::string &name, int def) { return getIntProperty(name, def, false); }
00194       int getIntProperty(const std::string &name, int def, bool env=true);
00195 
00196       long get(const std::string &name, long def) { return getLongProperty(name, def, false); }
00197       long getLongProperty(const std::string &name, long def, bool env=true);
00198 
00199       org::xmlBlaster::util::Timestamp getTimestampProperty(const std::string &name, org::xmlBlaster::util::Timestamp def, bool env=true);
00200 
00201       bool get(const std::string &name, bool def) { return getBoolProperty(name, def, false); }
00202       bool getBoolProperty(const std::string &name, bool def, bool env=true);
00203 
00204 
00205       std::string get(const std::string &name, const char *def) { std::string defS=def; return getStringProperty(name, defS, false); }
00206       std::string get(const std::string &name, const std::string &def) { return getStringProperty(name, def, false); }
00207       std::string getStringProperty(const std::string &name, const std::string &def, 
00208                             bool env=true);
00209 
00214       bool getTypedProperty(const std::string& name, std::string& value, bool env=true);
00215       bool getTypedProperty(const std::string& name, int& value, bool env=true);
00216       bool getTypedProperty(const std::string& name, long& value, bool env=true);
00217       bool getTypedProperty(const std::string& name, bool& value, bool env=true);
00218 #     if __LP64__
00219       // long === long long === 64 bit
00220 #     else
00221       bool getTypedProperty(const std::string& name, org::xmlBlaster::util::Timestamp& value, bool env=true);
00222 #     endif
00223         
00224        bool setProperty(const std::string &name, const std::string &value,
00225                        bool overwrite=true);
00226  
00242       int loadCommandLineProps(int args, const char * const argv[], const std::string &sep="-D", 
00243                     bool javaStyle=true);
00244       
00245 
00251       int findArgument(int args, const char * const argv[], const std::string &name) {
00252          for (int i=1; i < args; i++) {
00253             if (std::string(argv[i]) == name) return i;
00254          }
00255          return -1;
00256       }
00257       
00258       std::string toXml(const std::string& extraOffset="");
00259 
00260    }; // class Property
00261 
00262 }}} // namespace
00263 
00264 #endif // _UTIL_PROPERTY_H
00265 
00266 
00267       
00268