]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/NestElement.java
1) Changed ToolArg class to abstract generic arguments of a tool
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / NestElement.java
CommitLineData
878ddf1f 1/** @file\r
2This file is to define common interfaces for nested element of frameworktasks\r
3\r
4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14package org.tianocore.framework.tasks;\r
15\r
16import java.io.File;\r
17import java.util.List;\r
82810f3b 18import java.util.ArrayList;\r
19import java.io.FileReader;\r
20import java.io.BufferedReader;\r
21import java.util.StringTokenizer;\r
22\r
23import org.apache.tools.ant.types.DataType;\r
24import org.apache.tools.ant.types.Path;\r
25import org.apache.tools.ant.BuildException;\r
878ddf1f 26\r
27/**\r
82810f3b 28 Interface NestElement is to define common interfaces for nested element\r
878ddf1f 29 **/\r
82810f3b 30public class NestElement extends DataType {\r
31 //\r
32 // The name list. All the name strings got from setXXX methods will be put\r
33 // in here.\r
34 //\r
35 private List<String> nameList = new ArrayList<String>();\r
36\r
93f5dd0a 37 /**\r
38 Insert content in the newElement into this NestElement\r
39\r
40 @param newElement The new NestElement\r
41 **/\r
42 public void insert(NestElement newElement) {\r
43 this.nameList.addAll(newElement.getNameList());\r
44 }\r
45\r
82810f3b 46 /**\r
47 Handle "name" attribute. No delimiter and special treatment are assumed.\r
48\r
49 @param name A single string value of "name" attribute \r
50 **/\r
51 public void setName(String name) {\r
52 if (name.length() > 0) {\r
93f5dd0a 53 this.nameList.clear();\r
54 this.nameList.add(name);\r
55 }\r
56 }\r
57\r
58 public void insName(String name) {\r
59 if (name.length() > 0) {\r
60 this.nameList.add(name);\r
82810f3b 61 }\r
62 }\r
63\r
64 /**\r
65 Handle "list" attribute. The value of "list" is assumed as string \r
66 separated by space, tab, comma or semmicolon.\r
67\r
68 @param nameList The value of "list" separated by " \t,;"\r
69 **/\r
70 public void setList(String nameList) {\r
71 if (nameList.length() == 0) {\r
72 return;\r
73 }\r
74\r
93f5dd0a 75 this.nameList.clear();\r
82810f3b 76 StringTokenizer tokens = new StringTokenizer(nameList, " \t,;", false);\r
77 while (tokens.hasMoreTokens()) {\r
78 String name = tokens.nextToken().trim();\r
79 if (name.length() > 0) {\r
80 this.nameList.add(name);\r
81 }\r
82 }\r
83 }\r
84\r
85 /**\r
86 Handle "ListFile" attribute. The value of "ListFile" should be the path of\r
87 a file which contains name strings, one name per line.\r
88\r
89 @param listFileName The file path\r
90 **/\r
91 public void setListFile(String listFileName) {\r
92 FileReader fileReader = null;\r
93 BufferedReader in = null;\r
94 String str;\r
95\r
96 //\r
97 // Check if the file exists or not\r
98 // \r
99 File file = new File(listFileName);\r
100 if (!file.exists()) {\r
101 throw new BuildException("The file, " + file + " does not exist!"); \r
102 } \r
103\r
104 try {\r
105 fileReader = new FileReader(file);\r
106 in = new BufferedReader(fileReader);\r
107\r
108 //\r
109 // Read line by line\r
110 // \r
93f5dd0a 111 nameList.clear();\r
82810f3b 112 while((str = in.readLine()) != null){\r
113 str = str.trim();\r
114 if (str.length() == 0){\r
115 continue;\r
116 }\r
117\r
118 //getProject().replaceProperties(str);\r
93f5dd0a 119 this.nameList.add(str);\r
82810f3b 120 }\r
121 } catch (Exception e){\r
122 throw new BuildException(e.getMessage()); \r
123 } finally {\r
124 try {\r
125 //\r
126 // close the file\r
127 // \r
128 if (in != null) {\r
129 in.close();\r
130 }\r
131 if (fileReader != null) {\r
132 fileReader.close();\r
133 }\r
134 } catch (Exception e) {\r
135 throw new BuildException(e.getMessage()); \r
136 }\r
137 }\r
138 }\r
139\r
140 /**\r
141 Handle "file" attribute. The value of "file" should be a path.\r
142\r
143 @param file The path name of a file\r
144 **/\r
145 public void setFile(String file) {\r
146 setPath(file);\r
147 }\r
148\r
93f5dd0a 149 /**\r
150 Add a file or file list into the file list\r
151\r
152 @param file The path of a file\r
153 **/\r
154 public void insFile(String file) {\r
155 insPath(file);\r
156 }\r
157\r
82810f3b 158 /**\r
159 Handle "path" attribute. The value of "path" may contain compound path\r
160 separator (/ or \) which should be cleaned up. Because the "path" string\r
161 will always be passed to external native program which may not handle \r
162 non-native path separator, the clean-up action is a must. And the value\r
163 of "path" may contains several path separated by space, tab, comma or\r
164 semmicolon. We need to split it and put each part in nameList.\r
165\r
166 @param path String value of a file system path\r
167 **/\r
168 public void setPath(String path) {\r
93f5dd0a 169 this.nameList.clear();\r
170 insPath(path);\r
171 }\r
172\r
173 /**\r
174 Add a path or path list into the path list\r
175\r
176 @param path The path string\r
177 **/\r
178 public void insPath(String path) {\r
82810f3b 179 if (path.length() == 0) {\r
180 return;\r
181 }\r
182\r
183 //\r
184 // split the value of "path" into separated single path\r
185 // \r
186 StringTokenizer tokens = new StringTokenizer(path, " \t,;", false);\r
187 while (tokens.hasMoreTokens()) {\r
188 String pathName = tokens.nextToken().trim();\r
189 if (pathName.length() > 0) {\r
190 //\r
191 // Make clean the path string before storing it\r
192 // \r
193 this.nameList.add(cleanupPath(pathName));\r
194 }\r
195 }\r
196 }\r
197\r
198 /**\r
199 Handle "FileName" attribute. The value of "FileName" should be the path\r
200 of a file which contains path strings, one path per line.\r
201\r
202 @param pathFileName\r
203 **/\r
204 public void setPathFile(String pathFileName) {\r
205 FileReader fileReader = null;\r
206 BufferedReader in = null;\r
207 String path;\r
208\r
209 //\r
210 // Check if the file exists or not\r
211 // \r
212 File file = new File(pathFileName);\r
213 if (!file.exists()) {\r
214 throw new BuildException("The file, " + file + " does not exist!"); \r
215 } \r
216\r
217 try {\r
218 fileReader = new FileReader(file);\r
219 in = new BufferedReader(fileReader);\r
220\r
221 //\r
222 // Read the file line by line, skipping empty ones\r
223 // \r
93f5dd0a 224 nameList.clear();\r
82810f3b 225 while((path = in.readLine()) != null){\r
226 path = path.trim();\r
227 if (path.length() == 0){\r
228 continue;\r
229 }\r
230 //getProject().replaceProperties(path);\r
231\r
232 //\r
233 // Make clean the path string before storing it.\r
234 // \r
235 nameList.add(cleanupPath(path));\r
236 }\r
237 } catch (Exception e){\r
238 throw new BuildException(e.getMessage()); \r
239 } finally {\r
240 try {\r
241 //\r
242 // close the file\r
243 // \r
244 if (in != null) {\r
245 in.close();\r
246 }\r
247 if (fileReader != null) {\r
248 fileReader.close();\r
249 }\r
250 } catch (Exception e) {\r
251 throw new BuildException(e.getMessage()); \r
252 }\r
253 }\r
254 }\r
255\r
878ddf1f 256 /**\r
82810f3b 257 Return the name list.\r
258\r
259 @return List<String> The list contains the name(path) strings\r
878ddf1f 260 **/\r
82810f3b 261 public List<String> getNameList() {\r
262 return nameList;\r
263 }\r
264\r
265 /**\r
266 Compose and return the the name/path string without any delimiter. The trick\r
267 here is that it's actually used to return the value of nameList which\r
268 has just one name/string.\r
878ddf1f 269\r
82810f3b 270 @return String\r
271 **/\r
272 public String toString() {\r
273 return toString("");\r
274 }\r
275\r
276 /**\r
277 Compose and return the name/path string concatenated by leading "prefix".\r
278\r
279 @param prefix The string will be put before each name/string in nameList\r
280 \r
281 @return String The string concatenated with "prefix"\r
282 **/\r
283 public String toString(String prefix) {\r
284 StringBuffer string = new StringBuffer(1024);\r
285 int length = nameList.size();\r
878ddf1f 286\r
82810f3b 287 for (int i = 0; i < length; ++i) {\r
288 string.append(prefix);\r
289 string.append(nameList.get(i));\r
290 }\r
878ddf1f 291\r
82810f3b 292 return string.toString();\r
293 }\r
878ddf1f 294\r
93f5dd0a 295 /**\r
296 Compose and return the name/path string concatenated by space and\r
297 with only one "prefix".\r
298\r
299 @param prefix The prefix at the beginning of the string\r
300 \r
301 @return String The string with one prefix at the beginning\r
302 **/\r
303 public String toStringWithSinglepPrefix(String prefix) {\r
304 return prefix + toString(" ");\r
305 }\r
306\r
307 /**\r
308 Compose a string list with file names only, separated by spcified string\r
309\r
310 @param separator The separator string\r
311 \r
312 @return String The file list\r
313 **/\r
314 public String toFileList(String separator) {\r
315 StringBuffer string = new StringBuffer(1024);\r
316 int length = nameList.size();\r
317\r
318 for (int i = 0; i < length; ++i) {\r
319 File file = new File(nameList.get(i));\r
320 string.append(file.getName());\r
321 string.append(separator);\r
322 }\r
323\r
324 return string.toString();\r
325 }\r
326\r
327 /**\r
328 Compose a string list with file names only, separated by space\r
329\r
330 @return String The list string\r
331 **/\r
332 public String toFileList() {\r
333 return toFileList(" ");\r
334 }\r
335\r
82810f3b 336 //\r
337 // Remove any duplicated path separator or inconsistent path separator\r
338 //\r
339 private String cleanupPath(String path) {\r
340 String separator = "\\" + File.separator;\r
341 String duplicateSeparator = separator + "{2}";\r
878ddf1f 342\r
82810f3b 343 path = Path.translateFile(path);\r
344 path = path.replaceAll(duplicateSeparator, separator);\r
878ddf1f 345\r
82810f3b 346 return path;\r
347 }\r
878ddf1f 348}\r