summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/NestElement.java
blob: dd36c9127b8428138858290490db5317168009d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/** @file
This file is to define common interfaces for nested element of frameworktasks

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/
package org.tianocore.framework.tasks;

import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;

import org.apache.tools.ant.types.DataType;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.BuildException;

/**
 Interface NestElement is to define common interfaces for nested element
 **/
public class NestElement extends DataType {
    //
    // The name list. All the name strings got from setXXX methods will be put
    // in here.
    //
    protected List<String> nameList = new ArrayList<String>();

    /**
       Insert content in the newElement into this NestElement

       @param newElement    The new NestElement
     **/
    public void insert(NestElement newElement) {
        this.nameList.addAll(newElement.getNameList());
    }

    /**
       Handle "name" attribute. No delimiter and special treatment are assumed.

       @param name  A single string value of "name" attribute 
     **/
    public void setName(String name) {
        if (name.length() > 0) {
            this.nameList.clear();
            this.nameList.add(name);
        }
    }

    public void insName(String name) {
        if (name.length() > 0) {
            this.nameList.add(name);
        }
    }

    /**
       Handle "list" attribute. The value of "list" is assumed as string 
       separated by space, tab, comma or semmicolon.

       @param nameList  The value of "list" separated by " \t,;"
     **/
    public void setList(String nameList) {
        if (nameList.length() == 0) {
            return;
        }

        this.nameList.clear();
        StringTokenizer tokens = new StringTokenizer(nameList, " \t,;", false);
        while (tokens.hasMoreTokens()) {
            String name = tokens.nextToken().trim();
            if (name.length() > 0) {
                this.nameList.add(name);
            }
        }
    }

    /**
       Handle "ListFile" attribute. The value of "ListFile" should be the path of
       a file which contains name strings, one name per line.

       @param listFileName  The file path
     **/
    public void setListFile(String listFileName) {
        FileReader fileReader = null;
        BufferedReader in = null;
        String str;

        //
        // Check if the file exists or not
        // 
        File file = new File(listFileName);
        if (!file.exists()) {
            throw new BuildException("The file, " + file + " does not exist!");           
        } 

        try {
            fileReader = new FileReader(file);
            in = new BufferedReader(fileReader);

            //
            // Read line by line
            // 
            nameList.clear();
            while((str = in.readLine()) != null){
                str = str.trim();
                if (str.length() == 0){
                    continue;
                }

                //getProject().replaceProperties(str);
                this.nameList.add(str);
            }
        } catch (Exception e){
            throw new BuildException(e.getMessage());            
        } finally {
            try {
                //
                // close the file
                // 
                if (in != null) {
                    in.close();
                }
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (Exception e) {
                throw new BuildException(e.getMessage());            
            }
        }
    }

    /**
       Handle "file" attribute. The value of "file" should be a path.

       @param file  The path name of a file
     **/
    public void setFile(String file) {
        setPath(file);
    }

    /**
       Add a file or file list into the file list

       @param file  The path of a file
     **/
    public void insFile(String file) {
        insPath(file);
    }

    /**
       Handle "path" attribute. The value of "path" may contain compound path
       separator (/ or \) which should be cleaned up. Because the "path" string
       will always be passed to external native program which may not handle 
       non-native path separator, the clean-up action is a must. And the value
       of "path" may contains several path separated by space, tab, comma or
       semmicolon. We need to split it and put each part in nameList.

       @param path  String value of a file system path
     **/
    public void setPath(String path) {
        this.nameList.clear();
        insPath(path);
    }

    /**
       Add a path or path list into the path list

       @param path  The path string
     **/
    public void insPath(String path) {
        if (path.length() == 0) {
            return;
        }

        //
        // split the value of "path" into separated single path
        // 
        StringTokenizer tokens = new StringTokenizer(path, " \t,;", false);
        while (tokens.hasMoreTokens()) {
            String pathName = tokens.nextToken().trim();
            if (pathName.length() > 0) {
                //
                // Make clean the path string before storing it
                // 
                this.nameList.add(cleanupPath(pathName));
            }
        }
    }

    /**
       Handle "FileName" attribute. The value of "FileName" should be the path
       of a file which contains path strings, one path per line.

       @param pathFileName
     **/
    public void setPathFile(String pathFileName) {
        FileReader fileReader = null;
        BufferedReader in = null;
        String path;

        //
        // Check if the file exists or not
        // 
        File file = new File(pathFileName);
        if (!file.exists()) {
            throw new BuildException("The file, " + file + " does not exist!");           
        } 

        try {
            fileReader = new FileReader(file);
            in = new BufferedReader(fileReader);

            //
            // Read the file line by line, skipping empty ones
            // 
            nameList.clear();
            while((path = in.readLine()) != null){
                path = path.trim();
                if (path.length() == 0){
                    continue;
                }
                //getProject().replaceProperties(path);

                //
                // Make clean the path string before storing it.
                // 
                nameList.add(cleanupPath(path));
            }
        } catch (Exception e){
            throw new BuildException(e.getMessage());            
        } finally {
            try {
                //
                // close the file
                // 
                if (in != null) {
                    in.close();
                }
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (Exception e) {
                throw new BuildException(e.getMessage());            
            }
        }
    }

    /**
       Return the name list.

       @return List<String> The list contains the name(path) strings
     **/
    public List<String> getNameList() {
        return nameList;
    }

    /**
       Compose and return the the name/path string without any delimiter. The trick
       here is that it's actually used to return the value of nameList which
       has just one name/string.

       @return String
     **/
    public String toString() {
        return toString("");
    }

    /**
       Compose and return the name/path string concatenated by leading "prefix".

       @param prefix    The string will be put before each name/string in nameList
       
       @return String   The string concatenated with "prefix"
     **/
    public String toString(String prefix) {
        StringBuffer string = new StringBuffer(1024);
        int length = nameList.size();

        for (int i = 0; i < length; ++i) {
            string.append(prefix);
            string.append(nameList.get(i));
        }

        return string.toString();
    }

    /**
       Compose and return the name/path string concatenated by space and
       with only one "prefix".

       @param prefix    The prefix at the beginning of the string
       
       @return String   The string with one prefix at the beginning
     **/
    public String toStringWithSinglepPrefix(String prefix) {
        return prefix + toString(" ");
    }

    /**
       Compose a string list with file names only, separated by spcified string

       @param separator     The separator string
       
       @return String       The file list
     **/
    public String toFileList(String separator) {
        StringBuffer string = new StringBuffer(1024);
        int length = nameList.size();

        for (int i = 0; i < length; ++i) {
            File file = new File(nameList.get(i));
            string.append(file.getName());
            string.append(separator);
        }

        return string.toString();
    }

    /**
       Compose a string list with file names only, separated by space

       @return String   The list string
     **/
    public String toFileList() {
        return toFileList(" ");
    }

    /**
       Get the array of names

       @return String[]     The array contains the names
     **/
    public String[] toArray() {
        return nameList.toArray(new String[nameList.size()]);
    }

    /**
       Check if we have any name or not

       @return boolean
     **/
    public boolean isEmpty() {
        return nameList.isEmpty();
    }

    //
    // Remove any duplicated path separator or inconsistent path separator
    //
    private String cleanupPath(String path) {
        String separator = "\\" + File.separator;
        String duplicateSeparator = separator + "{2}";

        path = Path.translateFile(path);
        path = path.replaceAll(duplicateSeparator, separator);

        return path;
    }
}