test/jdk/tools/jpackage/helpers/jdk/jpackage/test/CfgFile.java
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
equal deleted inserted replaced
58647:2c43b89b1679 58648:3bf53ffa9ae7
       
     1 /*
       
     2  * Copyright (c) 2019, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package jdk.jpackage.test;
       
    24 
       
    25 import java.io.IOException;
       
    26 import java.nio.file.Files;
       
    27 import java.nio.file.Path;
       
    28 import java.util.Collections;
       
    29 import java.util.HashMap;
       
    30 import java.util.Map;
       
    31 import java.util.Objects;
       
    32 import java.util.regex.Matcher;
       
    33 import java.util.regex.Pattern;
       
    34 
       
    35 
       
    36 public final class CfgFile {
       
    37     public String getValue(String section, String key) {
       
    38         Objects.requireNonNull(section);
       
    39         Objects.requireNonNull(key);
       
    40 
       
    41         Map<String, String> entries = data.get(section);
       
    42         TKit.assertTrue(entries != null, String.format(
       
    43                 "Check section [%s] is found in [%s] cfg file", section, id));
       
    44 
       
    45         String value = entries.get(key);
       
    46         TKit.assertNotNull(value, String.format(
       
    47                 "Check key [%s] is found in [%s] section of [%s] cfg file", key,
       
    48                 section, id));
       
    49 
       
    50         return value;
       
    51     }
       
    52 
       
    53     private CfgFile(Map<String, Map<String, String>> data, String id) {
       
    54         this.data = data;
       
    55         this.id = id;
       
    56     }
       
    57 
       
    58     public static CfgFile readFromFile(Path path) throws IOException {
       
    59         TKit.trace(String.format("Read [%s] jpackage cfg file", path));
       
    60 
       
    61         final Pattern sectionBeginRegex = Pattern.compile( "\\s*\\[([^]]*)\\]\\s*");
       
    62         final Pattern keyRegex = Pattern.compile( "\\s*([^=]*)=(.*)" );
       
    63 
       
    64         Map<String, Map<String, String>> result = new HashMap<>();
       
    65 
       
    66         String currentSectionName = null;
       
    67         Map<String, String> currentSection = new HashMap<>();
       
    68         for (String line : Files.readAllLines(path)) {
       
    69             Matcher matcher = sectionBeginRegex.matcher(line);
       
    70             if (matcher.find()) {
       
    71                 if (currentSectionName != null) {
       
    72                     result.put(currentSectionName, Collections.unmodifiableMap(
       
    73                             new HashMap<>(currentSection)));
       
    74                 }
       
    75                 currentSectionName = matcher.group(1);
       
    76                 currentSection.clear();
       
    77                 continue;
       
    78             }
       
    79 
       
    80             matcher = keyRegex.matcher(line);
       
    81             if (matcher.find()) {
       
    82                 currentSection.put(matcher.group(1), matcher.group(2));
       
    83                 continue;
       
    84             }
       
    85         }
       
    86 
       
    87         if (!currentSection.isEmpty()) {
       
    88             result.put("", Collections.unmodifiableMap(currentSection));
       
    89         }
       
    90 
       
    91         return new CfgFile(Collections.unmodifiableMap(result), path.toString());
       
    92     }
       
    93 
       
    94     private final Map<String, Map<String, String>> data;
       
    95     private final String id;
       
    96 }