]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - 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
1/** @file\r
2 ConfigReader class.\r
3 \r
4 ConfigReader is used to read tool chain config file with flat format. \r
5 \r
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
17import org.apache.tools.ant.Project;\r
18\r
19import org.tianocore.build.exception.GenBuildException;\r
20\r
21import java.io.BufferedReader;\r
22import java.io.File;\r
23import java.io.FileReader;\r
24import java.util.ArrayList;\r
25import java.util.List;\r
26\r
27/**\r
28 \r
29 ConfigReader is used to read tool chain config file with flat format. Comments\r
30 is line starting with character '#'.\r
31 \r
32 @since GenBuild 1.0\r
33**/\r
34public class ConfigReader {\r
35\r
36 /**\r
37 Parse specified tool chain definition file.\r
38 \r
39 @param filename The config file name with full path\r
40\r
41 @return String[][] The definition array\r
42 **/\r
43 public static synchronized String[][] parse(Project prj, String filename) throws GenBuildException {\r
44 return parse(prj, new File(filename));\r
45 }\r
46\r
47 /**\r
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
55 @throws GenBuildException\r
56 Config file's format is not valid\r
57 **/\r
58 public static synchronized String[][] parse(Project prj, File configFile) throws GenBuildException {\r
59 List<String> keyList = new ArrayList<String>(256);\r
60 List<String> valueList = new ArrayList<String>(256);\r
61 int lines = 0;\r
62\r
63 try {\r
64 FileReader reader = new FileReader(configFile);\r
65 BufferedReader in = new BufferedReader(reader);\r
66 String str;\r
67\r
68 while ((str = in.readLine()) != null) {\r
69 ++lines;\r
70 str = str.trim();\r
71 //\r
72 // skip empty line, comment (start with '#') \r
73 //\r
74 if (str.length() == 0 || str.startsWith("#")) {\r
75 continue;\r
76 }\r
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
83 throw new GenBuildException("ERROR Processing file [" \r
84 + configFile.getAbsolutePath() \r
85 + "] (line " + lines + ").\n");\r
86 }\r
87\r
88 //\r
89 // look as line "A = B"\r
90 //\r
91 keyList.add(str.substring(0, index).trim());\r
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
97 }\r
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
104 }\r
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
111 }\r
112}\r
113\r
114\r