2 This file is to define common interfaces for nested element of frameworktasks
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
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.
14 package org
.tianocore
.framework
.tasks
;
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
;
23 import org
.apache
.tools
.ant
.types
.DataType
;
24 import org
.apache
.tools
.ant
.types
.Path
;
25 import org
.apache
.tools
.ant
.BuildException
;
28 Interface NestElement is to define common interfaces for nested element
30 public class NestElement
extends DataType
{
32 // The name list. All the name strings got from setXXX methods will be put
35 private List
<String
> nameList
= new ArrayList
<String
>();
38 Insert content in the newElement into this NestElement
40 @param newElement The new NestElement
42 public void insert(NestElement newElement
) {
43 this.nameList
.addAll(newElement
.getNameList());
47 Handle "name" attribute. No delimiter and special treatment are assumed.
49 @param name A single string value of "name" attribute
51 public void setName(String name
) {
52 if (name
.length() > 0) {
53 this.nameList
.clear();
54 this.nameList
.add(name
);
58 public void insName(String name
) {
59 if (name
.length() > 0) {
60 this.nameList
.add(name
);
65 Handle "list" attribute. The value of "list" is assumed as string
66 separated by space, tab, comma or semmicolon.
68 @param nameList The value of "list" separated by " \t,;"
70 public void setList(String nameList
) {
71 if (nameList
.length() == 0) {
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
);
86 Handle "ListFile" attribute. The value of "ListFile" should be the path of
87 a file which contains name strings, one name per line.
89 @param listFileName The file path
91 public void setListFile(String listFileName
) {
92 FileReader fileReader
= null;
93 BufferedReader in
= null;
97 // Check if the file exists or not
99 File file
= new File(listFileName
);
100 if (!file
.exists()) {
101 throw new BuildException("The file, " + file
+ " does not exist!");
105 fileReader
= new FileReader(file
);
106 in
= new BufferedReader(fileReader
);
112 while((str
= in
.readLine()) != null){
114 if (str
.length() == 0){
118 //getProject().replaceProperties(str);
119 this.nameList
.add(str
);
121 } catch (Exception e
){
122 throw new BuildException(e
.getMessage());
131 if (fileReader
!= null) {
134 } catch (Exception e
) {
135 throw new BuildException(e
.getMessage());
141 Handle "file" attribute. The value of "file" should be a path.
143 @param file The path name of a file
145 public void setFile(String file
) {
150 Add a file or file list into the file list
152 @param file The path of a file
154 public void insFile(String file
) {
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.
166 @param path String value of a file system path
168 public void setPath(String path
) {
169 this.nameList
.clear();
174 Add a path or path list into the path list
176 @param path The path string
178 public void insPath(String path
) {
179 if (path
.length() == 0) {
184 // split the value of "path" into separated single path
186 StringTokenizer tokens
= new StringTokenizer(path
, " \t,;", false);
187 while (tokens
.hasMoreTokens()) {
188 String pathName
= tokens
.nextToken().trim();
189 if (pathName
.length() > 0) {
191 // Make clean the path string before storing it
193 this.nameList
.add(cleanupPath(pathName
));
199 Handle "FileName" attribute. The value of "FileName" should be the path
200 of a file which contains path strings, one path per line.
204 public void setPathFile(String pathFileName
) {
205 FileReader fileReader
= null;
206 BufferedReader in
= null;
210 // Check if the file exists or not
212 File file
= new File(pathFileName
);
213 if (!file
.exists()) {
214 throw new BuildException("The file, " + file
+ " does not exist!");
218 fileReader
= new FileReader(file
);
219 in
= new BufferedReader(fileReader
);
222 // Read the file line by line, skipping empty ones
225 while((path
= in
.readLine()) != null){
227 if (path
.length() == 0){
230 //getProject().replaceProperties(path);
233 // Make clean the path string before storing it.
235 nameList
.add(cleanupPath(path
));
237 } catch (Exception e
){
238 throw new BuildException(e
.getMessage());
247 if (fileReader
!= null) {
250 } catch (Exception e
) {
251 throw new BuildException(e
.getMessage());
257 Return the name list.
259 @return List<String> The list contains the name(path) strings
261 public List
<String
> getNameList() {
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.
272 public String
toString() {
277 Compose and return the name/path string concatenated by leading "prefix".
279 @param prefix The string will be put before each name/string in nameList
281 @return String The string concatenated with "prefix"
283 public String
toString(String prefix
) {
284 StringBuffer string
= new StringBuffer(1024);
285 int length
= nameList
.size();
287 for (int i
= 0; i
< length
; ++i
) {
288 string
.append(prefix
);
289 string
.append(nameList
.get(i
));
292 return string
.toString();
296 Compose and return the name/path string concatenated by space and
297 with only one "prefix".
299 @param prefix The prefix at the beginning of the string
301 @return String The string with one prefix at the beginning
303 public String
toStringWithSinglepPrefix(String prefix
) {
304 return prefix
+ toString(" ");
308 Compose a string list with file names only, separated by spcified string
310 @param separator The separator string
312 @return String The file list
314 public String
toFileList(String separator
) {
315 StringBuffer string
= new StringBuffer(1024);
316 int length
= nameList
.size();
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
);
324 return string
.toString();
328 Compose a string list with file names only, separated by space
330 @return String The list string
332 public String
toFileList() {
333 return toFileList(" ");
337 // Remove any duplicated path separator or inconsistent path separator
339 private String
cleanupPath(String path
) {
340 String separator
= "\\" + File
.separator
;
341 String duplicateSeparator
= separator
+ "{2}";
343 path
= Path
.translateFile(path
);
344 path
= path
.replaceAll(duplicateSeparator
, separator
);