1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ZORBA_PROPERTIES_BASE_H
17 #define ZORBA_PROPERTIES_BASE_H
18 
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <vector>
23 #include <cctype>
24 #include <zorba/config.h>
25 
26 namespace zorba {
27 
28 /***************************************************************************//**
29 
30 ********************************************************************************/
31 class ZORBA_DLL_PUBLIC PropertiesBase
32 {
33 protected:
34   std::vector<std::string> thePositionalArgs;
35 
36 public:
~PropertiesBase()37   virtual ~PropertiesBase() {}
38 
load_all(const char * cfgFilename,const std::string & env_pfx,int argc,const char ** argv)39   std::string load_all(
40         const char* cfgFilename,
41         const std::string& env_pfx,
42         int argc,
43         const char **argv)
44   {
45     std::string result;
46 
47     if (! (result = load_env(env_pfx)).empty())
48       return result;
49 
50     if (! (result = load_file(cfgFilename)).empty())
51       return result;
52 
53     return load_argv(argc, argv);
54   }
55 
load_env(const std::string & env_pfx)56   std::string load_env(const std::string& env_pfx)
57   {
58     return load_env(env_pfx, get_all_options());
59   }
60 
61   std::string load_env(const std::string& env_pfx, const char** options);
62 
63   std::string load_file(const char* fname);
64 
65   virtual std::string load_argv(int argc, const char **argv) = 0;
66 
67   virtual const char** get_all_options() const = 0;
68 
check_args()69   virtual std::string check_args() { return ""; }
70 
getPositionalArgs()71   const std::vector<std::string>& getPositionalArgs() const
72   {
73     return thePositionalArgs;
74   }
75 
copy_args(const char ** argv)76   void copy_args (const char** argv)
77   {
78     for (; *argv != NULL; ++argv)
79     {
80       thePositionalArgs.push_back(*argv);
81     }
82   }
83 
84   template<class T> void init_val(const char* str, T& val, unsigned delta = 0)
85   {
86     std::istringstream is(str + delta);
87     is >> val;
88   }
89 
90 };
91 
92 
93 template<> ZORBA_DLL_PUBLIC void PropertiesBase::init_val(
94     const char* str,
95     std::string& val,
96     unsigned delta);
97 
98 
99 template<> ZORBA_DLL_PUBLIC void PropertiesBase::init_val(
100     const char* str,
101     std::vector<std::string>& val,
102     unsigned delta);
103 
104 /**
105  * \brief This class provides access to global properties.
106  *
107  * This class provides access to global properties set for Zorba in environment
108  * and configuration file.
109  * It is available using Zorba.getProperties() method.
110  * \see { Zorba::getProperties() }
111  */
112 class ZORBA_DLL_PUBLIC PropertiesGlobal : public PropertiesBase
113 {
114 public:
~PropertiesGlobal()115   virtual ~PropertiesGlobal() {}
116 
117   /**
118    * \brief Get global JVM classpath property.
119    *
120    * Before the JVM is started this will return the classpath set by
121    * command line option, the CLASSPATH environment variable and in Zorba
122    * config file.
123    *
124    * After the JVM is started this will contain in addition the paths to jars
125    * used by modules that make use of the JVM.
126    */
getJVMClassPath(std::string & jvmClasspath)127   virtual void getJVMClassPath(std::string & jvmClasspath) {}
128 
129   /**
130    * \brief Set global JVM classpath property.
131    *
132    * This method should be used to set additional JVM classpath for modules
133    * that make use of JVM. This will overide the classpath set by CLASSPATH
134    * environment variable or Zorba config file.
135    *
136    * Once the JVM is started this method doesn't have any effect.
137    */
setJVMClassPath(const std::string & jvmClasspath)138   virtual void setJVMClassPath(const std::string & jvmClasspath) {}
139 };
140 
141 }
142 #endif  // ZORBA_PROPERTIES_BASE_H
143 /*
144  * Local variables:
145  * mode: c++
146  * End:
147  */
148 /* vim:set et sw=2 ts=2: */
149