]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/GenBuild/org/tianocore/build/toolchain/ConfigReader.java
Added support for macro/property in tools_def.txt. Now you can define a property...
[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
2251a360 17import org.apache.tools.ant.Project;\r
18\r
2279b26c 19import org.tianocore.build.exception.GenBuildException;\r
a29c47e0 20\r
878ddf1f 21import java.io.BufferedReader;\r
22import java.io.File;\r
23import java.io.FileReader;\r
a29c47e0 24import java.util.ArrayList;\r
25import java.util.List;\r
878ddf1f 26\r
27/**\r
d2059d05 28 \r
878ddf1f 29 ConfigReader is used to read tool chain config file with flat format. Comments\r
30 is line starting with character '#'.\r
d2059d05 31 \r
878ddf1f 32 @since GenBuild 1.0\r
33**/\r
34public class ConfigReader {\r
35\r
878ddf1f 36 /**\r
d2059d05 37 Parse specified tool chain definition file.\r
38 \r
39 @param filename The config file name with full path\r
ff225cbb 40\r
d2059d05 41 @return String[][] The definition array\r
878ddf1f 42 **/\r
2251a360 43 public static synchronized String[][] parse(Project prj, String filename) throws GenBuildException {\r
44 return parse(prj, new File(filename));\r
878ddf1f 45 }\r
46\r
47 /**\r
d2059d05 48 Get all definitions in config file. the config file format is flat\r
49 with "A=B". If line started with '#' looks as comments. \r
50 \r
51 @param configFile The config file\r
52\r
53 @return String[][] The variables defined in the config file\r
54\r
0fdb42ac 55 @throws GenBuildException\r
d2059d05 56 Config file's format is not valid\r
878ddf1f 57 **/\r
2251a360 58 public static synchronized String[][] parse(Project prj, File configFile) throws GenBuildException {\r
a29c47e0 59 List<String> keyList = new ArrayList<String>(256);\r
60 List<String> valueList = new ArrayList<String>(256);\r
d2059d05 61 int lines = 0;\r
a29c47e0 62\r
878ddf1f 63 try {\r
d2059d05 64 FileReader reader = new FileReader(configFile);\r
878ddf1f 65 BufferedReader in = new BufferedReader(reader);\r
66 String str;\r
a29c47e0 67\r
878ddf1f 68 while ((str = in.readLine()) != null) {\r
d2059d05 69 ++lines;\r
878ddf1f 70 str = str.trim();\r
71 //\r
d2059d05 72 // skip empty line, comment (start with '#') \r
878ddf1f 73 //\r
d2059d05 74 if (str.length() == 0 || str.startsWith("#")) {\r
878ddf1f 75 continue;\r
76 }\r
d2059d05 77\r
78 //\r
79 // stop if the definition line is not in "name=value" form\r
80 // \r
81 int index;\r
82 if ((index = str.indexOf('=')) <= 0) {\r
0fdb42ac 83 throw new GenBuildException("ERROR Processing file [" \r
84 + configFile.getAbsolutePath() \r
d2059d05 85 + "] (line " + lines + ").\n");\r
86 }\r
87\r
878ddf1f 88 //\r
89 // look as line "A = B"\r
90 //\r
a29c47e0 91 keyList.add(str.substring(0, index).trim());\r
2251a360 92 if (prj != null) {\r
93 valueList.add(prj.replaceProperties(str.substring(index + 1).trim()));\r
94 } else {\r
95 valueList.add(str.substring(index + 1).trim());\r
96 }\r
878ddf1f 97 }\r
0fdb42ac 98 } catch (Exception ex) {\r
99 GenBuildException e = new GenBuildException("ERROR Processing file [" \r
100 + configFile.getAbsolutePath() \r
101 + "] (line " + lines + ").\n" + ex.getMessage());\r
102 e.setStackTrace(ex.getStackTrace());\r
103 throw e;\r
878ddf1f 104 }\r
a29c47e0 105\r
106 String[][] definitions = new String[2][keyList.size()];\r
107 definitions[0] = (String[])keyList.toArray(definitions[0]);\r
108 definitions[1] = (String[])valueList.toArray(definitions[1]);\r
109\r
110 return definitions;\r
878ddf1f 111 }\r
878ddf1f 112}\r
a29c47e0 113\r
114\r