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 Handle "name" attribute. No delimiter and special treatment are assumed.
40 @param name A single string value of "name" attribute
42 public void setName(String name
) {
43 if (name
.length() > 0) {
49 Handle "list" attribute. The value of "list" is assumed as string
50 separated by space, tab, comma or semmicolon.
52 @param nameList The value of "list" separated by " \t,;"
54 public void setList(String nameList
) {
55 if (nameList
.length() == 0) {
59 StringTokenizer tokens
= new StringTokenizer(nameList
, " \t,;", false);
60 while (tokens
.hasMoreTokens()) {
61 String name
= tokens
.nextToken().trim();
62 if (name
.length() > 0) {
63 this.nameList
.add(name
);
69 Handle "ListFile" attribute. The value of "ListFile" should be the path of
70 a file which contains name strings, one name per line.
72 @param listFileName The file path
74 public void setListFile(String listFileName
) {
75 FileReader fileReader
= null;
76 BufferedReader in
= null;
80 // Check if the file exists or not
82 File file
= new File(listFileName
);
84 throw new BuildException("The file, " + file
+ " does not exist!");
88 fileReader
= new FileReader(file
);
89 in
= new BufferedReader(fileReader
);
94 while((str
= in
.readLine()) != null){
96 if (str
.length() == 0){
100 //getProject().replaceProperties(str);
103 } catch (Exception e
){
104 throw new BuildException(e
.getMessage());
113 if (fileReader
!= null) {
116 } catch (Exception e
) {
117 throw new BuildException(e
.getMessage());
123 Handle "file" attribute. The value of "file" should be a path.
125 @param file The path name of a file
127 public void setFile(String file
) {
132 Handle "path" attribute. The value of "path" may contain compound path
133 separator (/ or \) which should be cleaned up. Because the "path" string
134 will always be passed to external native program which may not handle
135 non-native path separator, the clean-up action is a must. And the value
136 of "path" may contains several path separated by space, tab, comma or
137 semmicolon. We need to split it and put each part in nameList.
139 @param path String value of a file system path
141 public void setPath(String path
) {
142 if (path
.length() == 0) {
147 // split the value of "path" into separated single path
149 StringTokenizer tokens
= new StringTokenizer(path
, " \t,;", false);
150 while (tokens
.hasMoreTokens()) {
151 String pathName
= tokens
.nextToken().trim();
152 if (pathName
.length() > 0) {
154 // Make clean the path string before storing it
156 this.nameList
.add(cleanupPath(pathName
));
162 Handle "FileName" attribute. The value of "FileName" should be the path
163 of a file which contains path strings, one path per line.
167 public void setPathFile(String pathFileName
) {
168 FileReader fileReader
= null;
169 BufferedReader in
= null;
173 // Check if the file exists or not
175 File file
= new File(pathFileName
);
176 if (!file
.exists()) {
177 throw new BuildException("The file, " + file
+ " does not exist!");
181 fileReader
= new FileReader(file
);
182 in
= new BufferedReader(fileReader
);
185 // Read the file line by line, skipping empty ones
187 while((path
= in
.readLine()) != null){
189 if (path
.length() == 0){
192 //getProject().replaceProperties(path);
195 // Make clean the path string before storing it.
197 nameList
.add(cleanupPath(path
));
199 } catch (Exception e
){
200 throw new BuildException(e
.getMessage());
209 if (fileReader
!= null) {
212 } catch (Exception e
) {
213 throw new BuildException(e
.getMessage());
219 Return the name list.
221 @return List<String> The list contains the name(path) strings
223 public List
<String
> getNameList() {
228 Compose and return the the name/path string without any delimiter. The trick
229 here is that it's actually used to return the value of nameList which
230 has just one name/string.
234 public String
toString() {
239 Compose and return the name/path string concatenated by leading "prefix".
241 @param prefix The string will be put before each name/string in nameList
243 @return String The string concatenated with "prefix"
245 public String
toString(String prefix
) {
246 StringBuffer string
= new StringBuffer(1024);
247 int length
= nameList
.size();
249 for (int i
= 0; i
< length
; ++i
) {
250 string
.append(prefix
);
251 string
.append(nameList
.get(i
));
254 return string
.toString();
258 // Remove any duplicated path separator or inconsistent path separator
260 private String
cleanupPath(String path
) {
261 String separator
= "\\" + File
.separator
;
262 String duplicateSeparator
= separator
+ "{2}";
264 path
= Path
.translateFile(path
);
265 path
= path
.replaceAll(duplicateSeparator
, separator
);