src/jdk.packager/share/native/library/common/Helpers.cpp
branchJDK-8200758-branch
changeset 57017 1b08af362a30
parent 57016 f63f13da91c0
child 57018 9d782e357916
equal deleted inserted replaced
57016:f63f13da91c0 57017:1b08af362a30
     1 /*
       
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 #include "Helpers.h"
       
    27 #include "PlatformString.h"
       
    28 #include "PropertyFile.h"
       
    29 
       
    30 
       
    31 bool Helpers::SplitOptionIntoNameValue(
       
    32         TString option, TString& Name, TString& Value) {
       
    33     bool hasValue = false;
       
    34     Name = _T("");
       
    35     Value = _T("");
       
    36     unsigned int index = 0;
       
    37 
       
    38     for (; index < option.length(); index++) {
       
    39         TCHAR c = option[index];
       
    40 
       
    41         switch (c) {
       
    42             case '=': {
       
    43                 index++;
       
    44                 hasValue = true;
       
    45                 break;
       
    46             }
       
    47 
       
    48             case '\\': {
       
    49                 if (index + 1 < option.length()) {
       
    50                     c = option[index + 1];
       
    51 
       
    52                     switch (c) {
       
    53                         case '\\': {
       
    54                             index++;
       
    55                             Name += '\\';
       
    56                             break;
       
    57                         }
       
    58 
       
    59                         case '=': {
       
    60                             index++;
       
    61                             Name += '=';
       
    62                             break;
       
    63                         }
       
    64                     }
       
    65 
       
    66                 }
       
    67 
       
    68                 continue;
       
    69             }
       
    70 
       
    71             default: {
       
    72                 Name += c;
       
    73                 continue;
       
    74             }
       
    75         }
       
    76 
       
    77         break;
       
    78     }
       
    79 
       
    80     if (hasValue) {
       
    81         Value = option.substr(index, index - option.length());
       
    82     }
       
    83 
       
    84     return (option.length() > 0);
       
    85 }
       
    86 
       
    87 
       
    88 TString Helpers::ReplaceString(TString subject, const TString& search,
       
    89                             const TString& replace) {
       
    90     size_t pos = 0;
       
    91     while((pos = subject.find(search, pos)) != TString::npos) {
       
    92             subject.replace(pos, search.length(), replace);
       
    93             pos += replace.length();
       
    94     }
       
    95     return subject;
       
    96 }
       
    97 
       
    98 TString Helpers::ConvertIdToFilePath(TString Value) {
       
    99     TString search;
       
   100     search = '.';
       
   101     TString replace;
       
   102     replace = '/';
       
   103     TString result = ReplaceString(Value, search, replace);
       
   104     return result;
       
   105 }
       
   106 
       
   107 TString Helpers::ConvertIdToJavaPath(TString Value) {
       
   108     TString search;
       
   109     search = '.';
       
   110     TString replace;
       
   111     replace = '/';
       
   112     TString result = ReplaceString(Value, search, replace);
       
   113     search = '\\';
       
   114     result = ReplaceString(result, search, replace);
       
   115     return result;
       
   116 }
       
   117 
       
   118 TString Helpers::ConvertJavaPathToId(TString Value) {
       
   119     TString search;
       
   120     search = '/';
       
   121     TString replace;
       
   122     replace = '.';
       
   123     TString result = ReplaceString(Value, search, replace);
       
   124     return result;
       
   125 }
       
   126 
       
   127 OrderedMap<TString, TString>
       
   128         Helpers::GetJVMArgsFromConfig(IPropertyContainer* config) {
       
   129     OrderedMap<TString, TString> result;
       
   130 
       
   131     for (unsigned int index = 0; index < config->GetCount(); index++) {
       
   132         TString argname =
       
   133                 TString(_T("jvmarg.")) + PlatformString(index + 1).toString();
       
   134         TString argvalue;
       
   135 
       
   136         if (config->GetValue(argname, argvalue) == false) {
       
   137             break;
       
   138         }
       
   139         else if (argvalue.empty() == false) {
       
   140             TString name;
       
   141             TString value;
       
   142             if (Helpers::SplitOptionIntoNameValue(argvalue, name, value)) {
       
   143                 result.Append(name, value);
       
   144             }
       
   145         }
       
   146     }
       
   147 
       
   148     return result;
       
   149 }
       
   150 
       
   151 std::list<TString> Helpers::GetArgsFromConfig(IPropertyContainer* config) {
       
   152     std::list<TString> result;
       
   153 
       
   154     for (unsigned int index = 0; index < config->GetCount(); index++) {
       
   155         TString argname = TString(_T("arg."))
       
   156                 + PlatformString(index + 1).toString();
       
   157         TString argvalue;
       
   158 
       
   159         if (config->GetValue(argname, argvalue) == false) {
       
   160             break;
       
   161         }
       
   162         else if (argvalue.empty() == false) {
       
   163             result.push_back((argvalue));
       
   164         }
       
   165     }
       
   166 
       
   167     return result;
       
   168 }
       
   169 
       
   170 void AppendToIni(PropertyFile &Source, IniFile* Destination, TString Key) {
       
   171     TString value;
       
   172 
       
   173     if (Source.GetValue(Key, value) == true) {
       
   174         Platform& platform = Platform::GetInstance();
       
   175         std::map<TString, TString> keys = platform.GetKeys();
       
   176         Destination->Append(keys[CONFIG_SECTION_APPLICATION], Key, value);
       
   177     }
       
   178 }
       
   179 
       
   180 void Helpers::LoadOldConfigFile(TString FileName, IniFile* Container) {
       
   181     PropertyFile propertyFile;
       
   182 
       
   183     if (propertyFile.LoadFromFile(FileName) == true) {
       
   184         Platform& platform = Platform::GetInstance();
       
   185 
       
   186         std::map<TString, TString> keys = platform.GetKeys();
       
   187 
       
   188         // Application Section
       
   189         AppendToIni(propertyFile, Container, keys[CONFIG_MAINJAR_KEY]);
       
   190         AppendToIni(propertyFile, Container, keys[CONFIG_MAINMODULE_KEY]);
       
   191         AppendToIni(propertyFile, Container, keys[CONFIG_MAINCLASSNAME_KEY]);
       
   192         AppendToIni(propertyFile, Container, keys[CONFIG_CLASSPATH_KEY]);
       
   193         AppendToIni(propertyFile, Container, keys[APP_NAME_KEY]);
       
   194         AppendToIni(propertyFile, Container, keys[CONFIG_APP_ID_KEY]);
       
   195         AppendToIni(propertyFile, Container, keys[JVM_RUNTIME_KEY]);
       
   196         AppendToIni(propertyFile, Container, keys[PACKAGER_APP_DATA_DIR]);
       
   197 
       
   198         AppendToIni(propertyFile, Container, keys[CONFIG_APP_MEMORY]);
       
   199         AppendToIni(propertyFile, Container, keys[CONFIG_SPLASH_KEY]);
       
   200 
       
   201         // JVMOptions Section
       
   202         OrderedMap<TString, TString> JVMArgs =
       
   203                 Helpers::GetJVMArgsFromConfig(&propertyFile);
       
   204         Container->AppendSection(keys[CONFIG_SECTION_JVMOPTIONS], JVMArgs);
       
   205 
       
   206         // ArgOptions Section
       
   207         std::list<TString> args = Helpers::GetArgsFromConfig(&propertyFile);
       
   208         OrderedMap<TString, TString> convertedArgs;
       
   209 
       
   210         for (std::list<TString>::iterator iterator = args.begin();
       
   211                 iterator != args.end(); iterator++) {
       
   212             TString arg = *iterator;
       
   213             TString name;
       
   214             TString value;
       
   215 
       
   216             if (Helpers::SplitOptionIntoNameValue(arg, name, value) == true) {
       
   217                 convertedArgs.Append(name, value);
       
   218             }
       
   219         }
       
   220 
       
   221         Container->AppendSection(keys[CONFIG_SECTION_ARGOPTIONS],
       
   222                 convertedArgs);
       
   223     }
       
   224 }
       
   225 
       
   226 std::list<TString>
       
   227         Helpers::MapToNameValueList(OrderedMap<TString, TString> Map) {
       
   228     std::list<TString> result;
       
   229     std::vector<TString> keys = Map.GetKeys();
       
   230 
       
   231     for (OrderedMap<TString, TString>::const_iterator iterator = Map.begin();
       
   232             iterator != Map.end(); iterator++) {
       
   233        pair<TString, TString> *item = *iterator;
       
   234        TString key = item->first;
       
   235        TString value = item->second;
       
   236 
       
   237        if (value.length() == 0) {
       
   238            result.push_back(key);
       
   239        } else {
       
   240            result.push_back(key + _T('=') + value);
       
   241         }
       
   242     }
       
   243 
       
   244     return result;
       
   245 }
       
   246 
       
   247 TString Helpers::NameValueToString(TString name, TString value) {
       
   248     TString result;
       
   249 
       
   250     if (value.empty() == true) {
       
   251         result = name;
       
   252     }
       
   253     else {
       
   254         result = name + TString(_T("=")) + value;
       
   255     }
       
   256 
       
   257     return result;
       
   258 }
       
   259 
       
   260 std::list<TString> Helpers::StringToArray(TString Value) {
       
   261     std::list<TString> result;
       
   262     TString line;
       
   263 
       
   264     for (unsigned int index = 0; index < Value.length(); index++) {
       
   265         TCHAR c = Value[index];
       
   266 
       
   267         switch (c) {
       
   268             case '\n': {
       
   269                 result.push_back(line);
       
   270                 line = _T("");
       
   271                 break;
       
   272             }
       
   273 
       
   274             case '\r': {
       
   275                 result.push_back(line);
       
   276                 line = _T("");
       
   277 
       
   278                 if (Value[index + 1] == '\n')
       
   279                     index++;
       
   280 
       
   281                 break;
       
   282             }
       
   283 
       
   284             default: {
       
   285                 line += c;
       
   286             }
       
   287         }
       
   288     }
       
   289 
       
   290     // The buffer may not have ended with a Carriage Return/Line Feed.
       
   291     if (line.length() > 0) {
       
   292         result.push_back(line);
       
   293     }
       
   294 
       
   295     return result;
       
   296 }