1. Update to just keep several line JAVA related msg; 2. Remove file PropertyManager...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / toolchain / ConfigReader.java
CommitLineData
878ddf1f 1/** @file\r
2 ConfigReader class.\r
d2059d05 3 \r
4 ConfigReader is used to read tool chain config file with flat format. \r
5 \r
878ddf1f 6Copyright (c) 2006, Intel Corporation\r
7All rights reserved. This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14**/\r
15package org.tianocore.build.toolchain;\r
16\r
2279b26c 17import org.tianocore.build.exception.GenBuildException;\r
a29c47e0 18\r
878ddf1f 19import java.io.BufferedReader;\r
20import java.io.File;\r
21import java.io.FileReader;\r
a29c47e0 22import java.util.ArrayList;\r
23import java.util.List;\r
878ddf1f 24\r
25/**\r
d2059d05 26 \r
878ddf1f 27 ConfigReader is used to read tool chain config file with flat format. Comments\r
28 is line starting with character '#'.\r
d2059d05 29 \r
878ddf1f 30 @since GenBuild 1.0\r
31**/\r
32public class ConfigReader {\r
33\r
878ddf1f 34 /**\r
d2059d05 35 Parse specified tool chain definition file.\r
36 \r
37 @param filename The config file name with full path\r
ff225cbb 38\r
d2059d05 39 @return String[][] The definition array\r
878ddf1f 40 **/\r
2279b26c 41 public static synchronized String[][] parse(String filename) throws GenBuildException {\r
d2059d05 42 return parse(new File(filename));\r
878ddf1f 43 }\r
44\r
45 /**\r
d2059d05 46 Get all definitions in config file. the config file format is flat\r
47 with "A=B". If line started with '#' looks as comments. \r
48 \r
49 @param configFile The config file\r
50\r
51 @return String[][] The variables defined in the config file\r
52\r
0fdb42ac 53 @throws GenBuildException\r
d2059d05 54 Config file's format is not valid\r
878ddf1f 55 **/\r
2279b26c 56 public static synchronized String[][] parse(File configFile) throws GenBuildException {\r
a29c47e0 57 List<String> keyList = new ArrayList<String>(256);\r
58 List<String> valueList = new ArrayList<String>(256);\r
d2059d05 59 int lines = 0;\r
a29c47e0 60\r
878ddf1f 61 try {\r
d2059d05 62 FileReader reader = new FileReader(configFile);\r
878ddf1f 63 BufferedReader in = new BufferedReader(reader);\r
64 String str;\r
a29c47e0 65\r
878ddf1f 66 while ((str = in.readLine()) != null) {\r
d2059d05 67 ++lines;\r
878ddf1f 68 str = str.trim();\r
69 //\r
d2059d05 70 // skip empty line, comment (start with '#') \r
878ddf1f 71 //\r
d2059d05 72 if (str.length() == 0 || str.startsWith("#")) {\r
878ddf1f 73 continue;\r
74 }\r
d2059d05 75\r
76 //\r
77 // stop if the definition line is not in "name=value" form\r
78 // \r
79 int index;\r
80 if ((index = str.indexOf('=')) <= 0) {\r
0fdb42ac 81 throw new GenBuildException("ERROR Processing file [" \r
82 + configFile.getAbsolutePath() \r
d2059d05 83 + "] (line " + lines + ").\n");\r
84 }\r
85\r
878ddf1f 86 //\r
87 // look as line "A = B"\r
88 //\r
a29c47e0 89 keyList.add(str.substring(0, index).trim());\r
90 valueList.add(str.substring(index + 1).trim());\r
878ddf1f 91 }\r
0fdb42ac 92 } catch (Exception ex) {\r
93 GenBuildException e = new GenBuildException("ERROR Processing file [" \r
94 + configFile.getAbsolutePath() \r
95 + "] (line " + lines + ").\n" + ex.getMessage());\r
96 e.setStackTrace(ex.getStackTrace());\r
97 throw e;\r
878ddf1f 98 }\r
a29c47e0 99\r
100 String[][] definitions = new String[2][keyList.size()];\r
101 definitions[0] = (String[])keyList.toArray(definitions[0]);\r
102 definitions[1] = (String[])valueList.toArray(definitions[1]);\r
103\r
104 return definitions;\r
878ddf1f 105 }\r
878ddf1f 106}\r
a29c47e0 107\r
108\r