878ddf1f |
1 | /** @file\r |
2 | This file is to define common interfaces for nested element of frameworktasks\r |
3 | \r |
4 | Copyright (c) 2006, Intel Corporation\r |
5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r |
8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
12 | \r |
13 | **/\r |
14 | package org.tianocore.framework.tasks;\r |
15 | \r |
16 | import java.io.File;\r |
17 | import java.util.List;\r |
82810f3b |
18 | import java.util.ArrayList;\r |
19 | import java.io.FileReader;\r |
20 | import java.io.BufferedReader;\r |
21 | import java.util.StringTokenizer;\r |
22 | \r |
23 | import org.apache.tools.ant.types.DataType;\r |
24 | import org.apache.tools.ant.types.Path;\r |
25 | import 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 |
30 | public 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 |
37 | /**\r |
38 | Handle "name" attribute. No delimiter and special treatment are assumed.\r |
39 | \r |
40 | @param name A single string value of "name" attribute \r |
41 | **/\r |
42 | public void setName(String name) {\r |
43 | if (name.length() > 0) {\r |
44 | nameList.add(name);\r |
45 | }\r |
46 | }\r |
47 | \r |
48 | /**\r |
49 | Handle "list" attribute. The value of "list" is assumed as string \r |
50 | separated by space, tab, comma or semmicolon.\r |
51 | \r |
52 | @param nameList The value of "list" separated by " \t,;"\r |
53 | **/\r |
54 | public void setList(String nameList) {\r |
55 | if (nameList.length() == 0) {\r |
56 | return;\r |
57 | }\r |
58 | \r |
59 | StringTokenizer tokens = new StringTokenizer(nameList, " \t,;", false);\r |
60 | while (tokens.hasMoreTokens()) {\r |
61 | String name = tokens.nextToken().trim();\r |
62 | if (name.length() > 0) {\r |
63 | this.nameList.add(name);\r |
64 | }\r |
65 | }\r |
66 | }\r |
67 | \r |
68 | /**\r |
69 | Handle "ListFile" attribute. The value of "ListFile" should be the path of\r |
70 | a file which contains name strings, one name per line.\r |
71 | \r |
72 | @param listFileName The file path\r |
73 | **/\r |
74 | public void setListFile(String listFileName) {\r |
75 | FileReader fileReader = null;\r |
76 | BufferedReader in = null;\r |
77 | String str;\r |
78 | \r |
79 | //\r |
80 | // Check if the file exists or not\r |
81 | // \r |
82 | File file = new File(listFileName);\r |
83 | if (!file.exists()) {\r |
84 | throw new BuildException("The file, " + file + " does not exist!"); \r |
85 | } \r |
86 | \r |
87 | try {\r |
88 | fileReader = new FileReader(file);\r |
89 | in = new BufferedReader(fileReader);\r |
90 | \r |
91 | //\r |
92 | // Read line by line\r |
93 | // \r |
94 | while((str = in.readLine()) != null){\r |
95 | str = str.trim();\r |
96 | if (str.length() == 0){\r |
97 | continue;\r |
98 | }\r |
99 | \r |
100 | //getProject().replaceProperties(str);\r |
101 | nameList.add(str);\r |
102 | }\r |
103 | } catch (Exception e){\r |
104 | throw new BuildException(e.getMessage()); \r |
105 | } finally {\r |
106 | try {\r |
107 | //\r |
108 | // close the file\r |
109 | // \r |
110 | if (in != null) {\r |
111 | in.close();\r |
112 | }\r |
113 | if (fileReader != null) {\r |
114 | fileReader.close();\r |
115 | }\r |
116 | } catch (Exception e) {\r |
117 | throw new BuildException(e.getMessage()); \r |
118 | }\r |
119 | }\r |
120 | }\r |
121 | \r |
122 | /**\r |
123 | Handle "file" attribute. The value of "file" should be a path.\r |
124 | \r |
125 | @param file The path name of a file\r |
126 | **/\r |
127 | public void setFile(String file) {\r |
128 | setPath(file);\r |
129 | }\r |
130 | \r |
131 | /**\r |
132 | Handle "path" attribute. The value of "path" may contain compound path\r |
133 | separator (/ or \) which should be cleaned up. Because the "path" string\r |
134 | will always be passed to external native program which may not handle \r |
135 | non-native path separator, the clean-up action is a must. And the value\r |
136 | of "path" may contains several path separated by space, tab, comma or\r |
137 | semmicolon. We need to split it and put each part in nameList.\r |
138 | \r |
139 | @param path String value of a file system path\r |
140 | **/\r |
141 | public void setPath(String path) {\r |
142 | if (path.length() == 0) {\r |
143 | return;\r |
144 | }\r |
145 | \r |
146 | //\r |
147 | // split the value of "path" into separated single path\r |
148 | // \r |
149 | StringTokenizer tokens = new StringTokenizer(path, " \t,;", false);\r |
150 | while (tokens.hasMoreTokens()) {\r |
151 | String pathName = tokens.nextToken().trim();\r |
152 | if (pathName.length() > 0) {\r |
153 | //\r |
154 | // Make clean the path string before storing it\r |
155 | // \r |
156 | this.nameList.add(cleanupPath(pathName));\r |
157 | }\r |
158 | }\r |
159 | }\r |
160 | \r |
161 | /**\r |
162 | Handle "FileName" attribute. The value of "FileName" should be the path\r |
163 | of a file which contains path strings, one path per line.\r |
164 | \r |
165 | @param pathFileName\r |
166 | **/\r |
167 | public void setPathFile(String pathFileName) {\r |
168 | FileReader fileReader = null;\r |
169 | BufferedReader in = null;\r |
170 | String path;\r |
171 | \r |
172 | //\r |
173 | // Check if the file exists or not\r |
174 | // \r |
175 | File file = new File(pathFileName);\r |
176 | if (!file.exists()) {\r |
177 | throw new BuildException("The file, " + file + " does not exist!"); \r |
178 | } \r |
179 | \r |
180 | try {\r |
181 | fileReader = new FileReader(file);\r |
182 | in = new BufferedReader(fileReader);\r |
183 | \r |
184 | //\r |
185 | // Read the file line by line, skipping empty ones\r |
186 | // \r |
187 | while((path = in.readLine()) != null){\r |
188 | path = path.trim();\r |
189 | if (path.length() == 0){\r |
190 | continue;\r |
191 | }\r |
192 | //getProject().replaceProperties(path);\r |
193 | \r |
194 | //\r |
195 | // Make clean the path string before storing it.\r |
196 | // \r |
197 | nameList.add(cleanupPath(path));\r |
198 | }\r |
199 | } catch (Exception e){\r |
200 | throw new BuildException(e.getMessage()); \r |
201 | } finally {\r |
202 | try {\r |
203 | //\r |
204 | // close the file\r |
205 | // \r |
206 | if (in != null) {\r |
207 | in.close();\r |
208 | }\r |
209 | if (fileReader != null) {\r |
210 | fileReader.close();\r |
211 | }\r |
212 | } catch (Exception e) {\r |
213 | throw new BuildException(e.getMessage()); \r |
214 | }\r |
215 | }\r |
216 | }\r |
217 | \r |
878ddf1f |
218 | /**\r |
82810f3b |
219 | Return the name list.\r |
220 | \r |
221 | @return List<String> The list contains the name(path) strings\r |
878ddf1f |
222 | **/\r |
82810f3b |
223 | public List<String> getNameList() {\r |
224 | return nameList;\r |
225 | }\r |
226 | \r |
227 | /**\r |
228 | Compose and return the the name/path string without any delimiter. The trick\r |
229 | here is that it's actually used to return the value of nameList which\r |
230 | has just one name/string.\r |
878ddf1f |
231 | \r |
82810f3b |
232 | @return String\r |
233 | **/\r |
234 | public String toString() {\r |
235 | return toString("");\r |
236 | }\r |
237 | \r |
238 | /**\r |
239 | Compose and return the name/path string concatenated by leading "prefix".\r |
240 | \r |
241 | @param prefix The string will be put before each name/string in nameList\r |
242 | \r |
243 | @return String The string concatenated with "prefix"\r |
244 | **/\r |
245 | public String toString(String prefix) {\r |
246 | StringBuffer string = new StringBuffer(1024);\r |
247 | int length = nameList.size();\r |
878ddf1f |
248 | \r |
82810f3b |
249 | for (int i = 0; i < length; ++i) {\r |
250 | string.append(prefix);\r |
251 | string.append(nameList.get(i));\r |
252 | }\r |
878ddf1f |
253 | \r |
82810f3b |
254 | return string.toString();\r |
255 | }\r |
878ddf1f |
256 | \r |
82810f3b |
257 | //\r |
258 | // Remove any duplicated path separator or inconsistent path separator\r |
259 | //\r |
260 | private String cleanupPath(String path) {\r |
261 | String separator = "\\" + File.separator;\r |
262 | String duplicateSeparator = separator + "{2}";\r |
878ddf1f |
263 | \r |
82810f3b |
264 | path = Path.translateFile(path);\r |
265 | path = path.replaceAll(duplicateSeparator, separator);\r |
878ddf1f |
266 | \r |
82810f3b |
267 | return path;\r |
268 | }\r |
878ddf1f |
269 | }\r |