]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkTasks/org/tianocore/framework/tasks/NestElement.java
a402392349a3b4a1c2a77f18ce5ef91b6ce80f6b
[mirror_edk2.git] / Tools / 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 private List<String> nameList = new ArrayList<String>();
36
37 /**
38 Handle "name" attribute. No delimiter and special treatment are assumed.
39
40 @param name A single string value of "name" attribute
41 **/
42 public void setName(String name) {
43 if (name.length() > 0) {
44 nameList.add(name);
45 }
46 }
47
48 /**
49 Handle "list" attribute. The value of "list" is assumed as string
50 separated by space, tab, comma or semmicolon.
51
52 @param nameList The value of "list" separated by " \t,;"
53 **/
54 public void setList(String nameList) {
55 if (nameList.length() == 0) {
56 return;
57 }
58
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);
64 }
65 }
66 }
67
68 /**
69 Handle "ListFile" attribute. The value of "ListFile" should be the path of
70 a file which contains name strings, one name per line.
71
72 @param listFileName The file path
73 **/
74 public void setListFile(String listFileName) {
75 FileReader fileReader = null;
76 BufferedReader in = null;
77 String str;
78
79 //
80 // Check if the file exists or not
81 //
82 File file = new File(listFileName);
83 if (!file.exists()) {
84 throw new BuildException("The file, " + file + " does not exist!");
85 }
86
87 try {
88 fileReader = new FileReader(file);
89 in = new BufferedReader(fileReader);
90
91 //
92 // Read line by line
93 //
94 while((str = in.readLine()) != null){
95 str = str.trim();
96 if (str.length() == 0){
97 continue;
98 }
99
100 //getProject().replaceProperties(str);
101 nameList.add(str);
102 }
103 } catch (Exception e){
104 throw new BuildException(e.getMessage());
105 } finally {
106 try {
107 //
108 // close the file
109 //
110 if (in != null) {
111 in.close();
112 }
113 if (fileReader != null) {
114 fileReader.close();
115 }
116 } catch (Exception e) {
117 throw new BuildException(e.getMessage());
118 }
119 }
120 }
121
122 /**
123 Handle "file" attribute. The value of "file" should be a path.
124
125 @param file The path name of a file
126 **/
127 public void setFile(String file) {
128 setPath(file);
129 }
130
131 /**
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.
138
139 @param path String value of a file system path
140 **/
141 public void setPath(String path) {
142 if (path.length() == 0) {
143 return;
144 }
145
146 //
147 // split the value of "path" into separated single path
148 //
149 StringTokenizer tokens = new StringTokenizer(path, " \t,;", false);
150 while (tokens.hasMoreTokens()) {
151 String pathName = tokens.nextToken().trim();
152 if (pathName.length() > 0) {
153 //
154 // Make clean the path string before storing it
155 //
156 this.nameList.add(cleanupPath(pathName));
157 }
158 }
159 }
160
161 /**
162 Handle "FileName" attribute. The value of "FileName" should be the path
163 of a file which contains path strings, one path per line.
164
165 @param pathFileName
166 **/
167 public void setPathFile(String pathFileName) {
168 FileReader fileReader = null;
169 BufferedReader in = null;
170 String path;
171
172 //
173 // Check if the file exists or not
174 //
175 File file = new File(pathFileName);
176 if (!file.exists()) {
177 throw new BuildException("The file, " + file + " does not exist!");
178 }
179
180 try {
181 fileReader = new FileReader(file);
182 in = new BufferedReader(fileReader);
183
184 //
185 // Read the file line by line, skipping empty ones
186 //
187 while((path = in.readLine()) != null){
188 path = path.trim();
189 if (path.length() == 0){
190 continue;
191 }
192 //getProject().replaceProperties(path);
193
194 //
195 // Make clean the path string before storing it.
196 //
197 nameList.add(cleanupPath(path));
198 }
199 } catch (Exception e){
200 throw new BuildException(e.getMessage());
201 } finally {
202 try {
203 //
204 // close the file
205 //
206 if (in != null) {
207 in.close();
208 }
209 if (fileReader != null) {
210 fileReader.close();
211 }
212 } catch (Exception e) {
213 throw new BuildException(e.getMessage());
214 }
215 }
216 }
217
218 /**
219 Return the name list.
220
221 @return List<String> The list contains the name(path) strings
222 **/
223 public List<String> getNameList() {
224 return nameList;
225 }
226
227 /**
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.
231
232 @return String
233 **/
234 public String toString() {
235 return toString("");
236 }
237
238 /**
239 Compose and return the name/path string concatenated by leading "prefix".
240
241 @param prefix The string will be put before each name/string in nameList
242
243 @return String The string concatenated with "prefix"
244 **/
245 public String toString(String prefix) {
246 StringBuffer string = new StringBuffer(1024);
247 int length = nameList.size();
248
249 for (int i = 0; i < length; ++i) {
250 string.append(prefix);
251 string.append(nameList.get(i));
252 }
253
254 return string.toString();
255 }
256
257 //
258 // Remove any duplicated path separator or inconsistent path separator
259 //
260 private String cleanupPath(String path) {
261 String separator = "\\" + File.separator;
262 String duplicateSeparator = separator + "{2}";
263
264 path = Path.translateFile(path);
265 path = path.replaceAll(duplicateSeparator, separator);
266
267 return path;
268 }
269 }