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