modules/fxpackager/src/main/native/library/common/Helpers.cpp

Print this page




  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #include "Helpers.h"
  35 #include "PlatformString.h"

  36 
  37 
  38 bool Helpers::SplitOptionIntoNameValue(TString option, TString& Name, TString& Value) {
  39     bool result = false;
  40     size_t position = option.find('=');


  41 
  42     if (position != TString::npos) {
  43         Name = option.substr(0, position);
  44         Value = option.substr(position + 1, option.length() - position + 1);



  45         result = true;

  46     }
  47     else {
  48         Name = option;








  49     }
  50 
  51     return result;

























  52 }
  53 

  54 TString Helpers::ReplaceString(TString subject, const TString& search,
  55                             const TString& replace) {
  56     size_t pos = 0;
  57     while((pos = subject.find(search, pos)) != TString::npos) {
  58             subject.replace(pos, search.length(), replace);
  59             pos += replace.length();
  60     }
  61     return subject;
  62 }
  63 
  64 TString Helpers::ConvertIdToFilePath(TString Value) {
  65     TString search;
  66     search = '.';
  67     TString replace;
  68     replace = '/';
  69     TString result = ReplaceString(Value, search, replace);
  70     return result;
  71 }
  72 
  73 TString Helpers::ConvertIdToJavaPath(TString Value) {
  74     TString search;
  75     search = '.';
  76     TString replace;
  77     replace = '/';
  78     TString result = ReplaceString(Value, search, replace);
  79     search = '\\';
  80     result = ReplaceString(result, search, replace);
  81     return result;
  82 }
  83 
  84 TString Helpers::ConvertPathToId(TString Value) {
  85     TString search;
  86     search = TRAILING_SLASH;
  87     TString replace;
  88     replace = '.';
  89     TString result = ReplaceString(Value, search, replace);
  90     return result;
  91 }
  92 
  93 TOrderedMap Helpers::GetJVMArgsFromConfig(PropertyContainer* config) {
  94     TOrderedMap result;
  95 
  96     for (unsigned int index = 0; index < config->GetCount(); index++) {
  97         TString argname = TString(_T("jvmarg.")) + PlatformString(index + 1).toString();
  98         TString argvalue;
  99 
 100         if (config->GetValue(argname, argvalue) == false) {
 101             break;
 102         }
 103         else if (argvalue.empty() == false) {
 104             TString name;
 105             TValueIndex value;
 106             Helpers::SplitOptionIntoNameValue(argvalue, name, value.value);
 107             result.insert(TOrderedMap::value_type(name, value));
 108         }
 109     }
 110 
 111     return result;
 112 }
 113 
 114 TOrderedMap Helpers::GetJVMUserArgsFromConfig(PropertyContainer* config) {
 115     TOrderedMap result;
 116 
 117     for (unsigned int index = 0; index < config->GetCount(); index++) {
 118         TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString();
 119         TString argname = prefix + _T(".name");
 120         TString argvalue = prefix + _T(".value");
 121         TString name;
 122         TValueIndex value;
 123 
 124         if ((config->GetValue(argname, name) == false) || (config->GetValue(argvalue, value.value) == false)) {
 125             break;
 126         }
 127         else if ((name.empty() == false) && (value.value.empty() == false)) {
 128             result.insert(TOrderedMap::value_type(name, value));
 129         }
 130     }
 131 
 132     return result;
 133 }
 134 
 135 std::map<TString, TString> Helpers::GetConfigFromJVMUserArgs(TOrderedMap OrderedMap) {
 136     std::map<TString, TString> result;
 137     size_t index = 0;




 138     
 139     for (TOrderedMap::iterator iterator = OrderedMap.begin();
 140          iterator != OrderedMap.end();
 141          iterator++) {
 142         TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString();
 143         TString argname = prefix + _T(".name");
 144         TString argvalue = prefix + _T(".value");
 145         TString name = iterator->first;
 146         TString value = iterator->second.value;
 147         
 148         result.insert(std::map<TString, TString>::value_type(argname, name));
 149         result.insert(std::map<TString, TString>::value_type(argvalue, value));
 150         index++;
 151     }
 152     
 153     return result;
 154 }
 155 
 156 std::list<TString> Helpers::GetArgsFromConfig(PropertyContainer* config) {
 157     std::list<TString> result;
 158 
 159     for (unsigned int index = 0; index < config->GetCount(); index++) {
 160         TString argname = TString(_T("arg.")) + PlatformString(index + 1).toString();
 161         TString argvalue;
 162 
 163         if (config->GetValue(argname, argvalue) == false) {
 164             break;
 165         }
 166         else if (argvalue.empty() == false) {
 167             result.push_back((argvalue));
 168         }
 169     }
 170 
 171     return result;
 172 }
 173 
 174 bool comp(const TValueIndex& a, const TValueIndex& b) {
 175     return a.index < b.index;




































































 176 }
 177 
 178 std::list<TString> Helpers::GetOrderedKeysFromMap(TOrderedMap OrderedMap) {
 179     std::list<TString> result;
 180     std::list<TValueIndex> indexedList;
 181     
 182     for (TOrderedMap::iterator iterator = OrderedMap.begin();
 183          iterator != OrderedMap.end();
 184          iterator++) {
 185         TValueIndex item;
 186         item.value = iterator->first;
 187         item.index = iterator->second.index;
 188         indexedList.push_back(item);
 189     }
 190     
 191     indexedList.sort(comp);
 192     
 193     for (std::list<TValueIndex>::const_iterator iterator = indexedList.begin(); iterator != indexedList.end(); iterator++) {
 194         TString name = iterator->value;
 195         result.push_back(name);
 196     }
 197     
 198     return result;
 199 }
 200 

 201 TString Helpers::NameValueToString(TString name, TString value) {
 202     TString result;
 203     
 204     if (value.empty() == true) {
 205         result = name;
 206     }
 207     else {
 208         result = name + TString(_T("=")) + value;
 209     }
 210     
 211     return result;
 212 }


  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #include "Helpers.h"
  35 #include "PlatformString.h"
  36 #include "PropertyFile.h"
  37 
  38 
  39 bool Helpers::SplitOptionIntoNameValue(TString option, TString& Name, TString& Value) {
  40     bool result = false;
  41     Name = _T("");
  42     Value = _T("");
  43     unsigned int index = 0;
  44 
  45     for (; index < option.length(); index++) {
  46         TCHAR c = option[index];
  47 
  48         switch (c) {
  49             case '=': {
  50                 index++;
  51                 result = true;
  52                 break;
  53             }
  54 
  55             case '\\': {
  56                 if (index + 1 < option.length()) {
  57                     c = option[index + 1];
  58 
  59                     switch (c) {
  60                         case '\\': {
  61                             index++;
  62                             Name += '\\';
  63                             break;
  64                         }
  65 
  66                         case '=': {
  67                             index++;
  68                             Name += '=';
  69                             break;
  70                         }
  71                     }
  72 
  73                 }
  74 
  75                 continue;
  76             }
  77 
  78             default: {
  79                 Name += c;
  80                 continue;
  81             }
  82         }
  83 
  84         break;
  85     }
  86 
  87     if (result) {
  88         Value = option.substr(index, index - option.length());
  89     }
  90 
  91     return true;
  92 }
  93 
  94 
  95 TString Helpers::ReplaceString(TString subject, const TString& search,
  96                             const TString& replace) {
  97     size_t pos = 0;
  98     while((pos = subject.find(search, pos)) != TString::npos) {
  99             subject.replace(pos, search.length(), replace);
 100             pos += replace.length();
 101     }
 102     return subject;
 103 }
 104 
 105 TString Helpers::ConvertIdToFilePath(TString Value) {
 106     TString search;
 107     search = '.';
 108     TString replace;
 109     replace = '/';
 110     TString result = ReplaceString(Value, search, replace);
 111     return result;
 112 }
 113 
 114 TString Helpers::ConvertIdToJavaPath(TString Value) {
 115     TString search;
 116     search = '.';
 117     TString replace;
 118     replace = '/';
 119     TString result = ReplaceString(Value, search, replace);
 120     search = '\\';
 121     result = ReplaceString(result, search, replace);
 122     return result;
 123 }
 124 
 125 TString Helpers::ConvertPathToId(TString Value) {
 126     TString search;
 127     search = TRAILING_PATHSEPARATOR;
 128     TString replace;
 129     replace = '.';
 130     TString result = ReplaceString(Value, search, replace);
 131     return result;
 132 }
 133 
 134 OrderedMap<TString, TString> Helpers::GetJVMArgsFromConfig(IPropertyContainer* config) {
 135     OrderedMap<TString, TString> result;
 136 
 137     for (unsigned int index = 0; index < config->GetCount(); index++) {
 138         TString argname = TString(_T("jvmarg.")) + PlatformString(index + 1).toString();
 139         TString argvalue;
 140 
 141         if (config->GetValue(argname, argvalue) == false) {
 142             break;
 143         }
 144         else if (argvalue.empty() == false) {
 145             TString name;
 146             TString value;
 147             Helpers::SplitOptionIntoNameValue(argvalue, name, value);
 148             result.Append(name, value);
 149         }
 150     }
 151 
 152     return result;
 153 }
 154 
 155 OrderedMap<TString, TString> Helpers::GetJVMUserArgsFromConfig(IPropertyContainer* config) {
 156     OrderedMap<TString, TString> result;
 157 
 158     for (unsigned int index = 0; index < config->GetCount(); index++) {
 159         TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString();
 160         TString argname = prefix + _T(".name");
 161         TString argvalue = prefix + _T(".value");
 162         TString name;
 163         TString value;
 164 
 165         if ((config->GetValue(argname, name) == false) || (config->GetValue(argvalue, value) == false)) {
 166             break;
 167         }
 168         else if ((name.empty() == false) && (value.empty() == false)) {
 169             result.Append(name, value);
 170         }
 171     }
 172 
 173     return result;
 174 }
 175 
 176 /*OrderedMap<TString, TString> Helpers::GetConfigFromJVMUserArgs(OrderedMap<TString, TString> Map) {
 177     OrderedMap<TString, TString> result;
 178     std::vector<TString> keys = Map.GetKeys();
 179 
 180     for (unsigned int index = 0; index < keys.size(); index++) {
 181         TString key = keys[index];
 182         TString value;
 183 
 184         if (Map.GetValue(key, value) == true) {


 185             TString prefix = TString(_T("jvmuserarg.")) + PlatformString(index + 1).toString();
 186             TString argname = prefix + _T(".name");
 187             TString argvalue = prefix + _T(".value");


 188 
 189             result.Append(argname, key);
 190             result.Append(argvalue, value);
 191         }
 192     }
 193 
 194     return result;
 195 }*/
 196 
 197 std::list<TString> Helpers::GetArgsFromConfig(IPropertyContainer* config) {
 198     std::list<TString> result;
 199 
 200     for (unsigned int index = 0; index < config->GetCount(); index++) {
 201         TString argname = TString(_T("arg.")) + PlatformString(index + 1).toString();
 202         TString argvalue;
 203 
 204         if (config->GetValue(argname, argvalue) == false) {
 205             break;
 206         }
 207         else if (argvalue.empty() == false) {
 208             result.push_back((argvalue));
 209         }
 210     }
 211 
 212     return result;
 213 }
 214 
 215 void AppendToIni(PropertyFile &Source, IniFile* Destination, TString Key) {
 216     TString value;
 217 
 218     if (Source.GetValue(Key, value) == true) {
 219         Platform& platform = Platform::GetInstance();
 220         std::map<TString, TString> keys = platform.GetKeys();
 221         Destination->Append(keys[CONFIG_SECTION_APPLICATION], Key, value);
 222     }
 223 }
 224 
 225 void Helpers::LoadOldConfigFile(TString FileName, IniFile* Container) {
 226     PropertyFile propertyFile;
 227 
 228     if (propertyFile.LoadFromFile(FileName) == true) {
 229         Platform& platform = Platform::GetInstance();
 230 
 231         std::map<TString, TString> keys = platform.GetKeys();
 232 
 233         // Application Section
 234         AppendToIni(propertyFile, Container, keys[CONFIG_MAINJAR_KEY]);
 235         AppendToIni(propertyFile, Container, keys[CONFIG_MAINCLASSNAME_KEY]);
 236         AppendToIni(propertyFile, Container, keys[CONFIG_CLASSPATH_KEY]);
 237         AppendToIni(propertyFile, Container, keys[APP_NAME_KEY]);
 238         AppendToIni(propertyFile, Container, keys[CONFIG_APP_ID_KEY]);
 239         AppendToIni(propertyFile, Container, keys[JVM_RUNTIME_KEY]);
 240         AppendToIni(propertyFile, Container, keys[PACKAGER_APP_DATA_DIR]);
 241 
 242         AppendToIni(propertyFile, Container, keys[CONFIG_APP_MEMORY]);
 243         AppendToIni(propertyFile, Container, keys[CONFIG_SPLASH_KEY]);
 244 
 245         // JVMOptions Section
 246         OrderedMap<TString, TString> JVMArgs = Helpers::GetJVMArgsFromConfig(&propertyFile);
 247         Container->AppendSection(keys[CONFIG_SECTION_JVMOPTIONS], JVMArgs);
 248 
 249         // JVMUserOptions Section
 250         OrderedMap<TString, TString> defaultJVMUserArgs = Helpers::GetJVMUserArgsFromConfig(&propertyFile);
 251         Container->AppendSection(keys[CONFIG_SECTION_JVMUSEROPTIONS], defaultJVMUserArgs);
 252 
 253         // ArgOptions Section
 254         std::list<TString> args = Helpers::GetArgsFromConfig(&propertyFile);
 255         OrderedMap<TString, TString> convertedArgs;
 256 
 257         for (std::list<TString>::iterator iterator = args.begin(); iterator != args.end(); iterator++) {
 258             TString arg = *iterator;
 259             TString name;
 260             TString value;
 261 
 262             if (Helpers::SplitOptionIntoNameValue(arg, name, value) == true) {
 263                 convertedArgs.Append(name, value);
 264             }
 265         }
 266 
 267         Container->AppendSection(keys[CONFIG_SECTION_ARGOPTIONS], convertedArgs);
 268     }
 269 }
 270 
 271 void Helpers::LoadOldUserConfigFile(TString FileName, IniFile* Container) {
 272     PropertyFile propertyFile;
 273     Container = NULL;
 274 
 275     if (propertyFile.LoadFromFile(FileName) == true) {
 276         Container = new IniFile();
 277         Platform& platform = Platform::GetInstance();
 278 
 279         std::map<TString, TString> keys = platform.GetKeys();
 280 
 281         // JVMUserOverridesOptions Section
 282         OrderedMap<TString, TString> defaultJVMUserArgs = Helpers::GetJVMUserArgsFromConfig(&propertyFile);
 283         Container->AppendSection(keys[CONFIG_SECTION_JVMUSEROVERRIDESOPTIONS], defaultJVMUserArgs);
 284     }
 285 }
 286 
 287 std::list<TString> Helpers::MapToNameValueList(OrderedMap<TString, TString> Map) {
 288     std::list<TString> result;
 289     std::vector<TString> keys = Map.GetKeys();
 290 
 291     for (size_t index = 0; index < keys.size(); index++) {
 292         TString key = keys[index];
 293         TString value;
 294 
 295         if (Map.GetValue(key, value) == true) {
 296             result.push_back(key + _T('=') + value);
 297         }







 298     }
 299 
 300     return result;
 301 }
 302 
 303 
 304 TString Helpers::NameValueToString(TString name, TString value) {
 305     TString result;
 306 
 307     if (value.empty() == true) {
 308         result = name;
 309     }
 310     else {
 311         result = name + TString(_T("=")) + value;
 312     }
 313 
 314     return result;
 315 }