--- /dev/null
+/** @file\r
+This file is to define common interfaces for nested element of frameworktasks\r
+\r
+Copyright (c) 2006, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+package org.tianocore.framework.tasks;\r
+\r
+import java.io.File;\r
+import java.util.List;\r
+import java.util.ArrayList;\r
+import java.io.FileReader;\r
+import java.io.BufferedReader;\r
+import java.util.StringTokenizer;\r
+\r
+import org.apache.tools.ant.types.DataType;\r
+import org.apache.tools.ant.types.Path;\r
+import org.apache.tools.ant.BuildException;\r
+\r
+/**\r
+ Interface NestElement is to define common interfaces for nested element\r
+ **/\r
+public class NestElement extends DataType {\r
+ //\r
+ // The name list. All the name strings got from setXXX methods will be put\r
+ // in here.\r
+ //\r
+ protected List<String> nameList = new ArrayList<String>();\r
+\r
+ /**\r
+ Insert content in the newElement into this NestElement\r
+\r
+ @param newElement The new NestElement\r
+ **/\r
+ public void insert(NestElement newElement) {\r
+ this.nameList.addAll(newElement.getNameList());\r
+ }\r
+\r
+ /**\r
+ Handle "name" attribute. No delimiter and special treatment are assumed.\r
+\r
+ @param name A single string value of "name" attribute \r
+ **/\r
+ public void setName(String name) {\r
+ if (name.length() > 0) {\r
+ this.nameList.clear();\r
+ this.nameList.add(name);\r
+ }\r
+ }\r
+\r
+ public void insName(String name) {\r
+ if (name.length() > 0) {\r
+ this.nameList.add(name);\r
+ }\r
+ }\r
+\r
+ /**\r
+ Handle "list" attribute. The value of "list" is assumed as string \r
+ separated by space, tab, comma or semmicolon.\r
+\r
+ @param nameList The value of "list" separated by " \t,;"\r
+ **/\r
+ public void setList(String nameList) {\r
+ if (nameList.length() == 0) {\r
+ return;\r
+ }\r
+\r
+ this.nameList.clear();\r
+ StringTokenizer tokens = new StringTokenizer(nameList, " \t,;", false);\r
+ while (tokens.hasMoreTokens()) {\r
+ String name = tokens.nextToken().trim();\r
+ if (name.length() > 0) {\r
+ this.nameList.add(name);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ Handle "ListFile" attribute. The value of "ListFile" should be the path of\r
+ a file which contains name strings, one name per line.\r
+\r
+ @param listFileName The file path\r
+ **/\r
+ public void setListFile(String listFileName) {\r
+ FileReader fileReader = null;\r
+ BufferedReader in = null;\r
+ String str;\r
+\r
+ //\r
+ // Check if the file exists or not\r
+ // \r
+ File file = new File(listFileName);\r
+ if (!file.exists()) {\r
+ throw new BuildException("The file, " + file + " does not exist!"); \r
+ } \r
+\r
+ try {\r
+ fileReader = new FileReader(file);\r
+ in = new BufferedReader(fileReader);\r
+\r
+ //\r
+ // Read line by line\r
+ // \r
+ nameList.clear();\r
+ while((str = in.readLine()) != null){\r
+ str = str.trim();\r
+ if (str.length() == 0){\r
+ continue;\r
+ }\r
+\r
+ //getProject().replaceProperties(str);\r
+ this.nameList.add(str);\r
+ }\r
+ } catch (Exception e){\r
+ throw new BuildException(e.getMessage()); \r
+ } finally {\r
+ try {\r
+ //\r
+ // close the file\r
+ // \r
+ if (in != null) {\r
+ in.close();\r
+ }\r
+ if (fileReader != null) {\r
+ fileReader.close();\r
+ }\r
+ } catch (Exception e) {\r
+ throw new BuildException(e.getMessage()); \r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ Handle "file" attribute. The value of "file" should be a path.\r
+\r
+ @param file The path name of a file\r
+ **/\r
+ public void setFile(String file) {\r
+ setPath(file);\r
+ }\r
+\r
+ /**\r
+ Add a file or file list into the file list\r
+\r
+ @param file The path of a file\r
+ **/\r
+ public void insFile(String file) {\r
+ insPath(file);\r
+ }\r
+\r
+ /**\r
+ Handle "path" attribute. The value of "path" may contain compound path\r
+ separator (/ or \) which should be cleaned up. Because the "path" string\r
+ will always be passed to external native program which may not handle \r
+ non-native path separator, the clean-up action is a must. And the value\r
+ of "path" may contains several path separated by space, tab, comma or\r
+ semmicolon. We need to split it and put each part in nameList.\r
+\r
+ @param path String value of a file system path\r
+ **/\r
+ public void setPath(String path) {\r
+ this.nameList.clear();\r
+ insPath(path);\r
+ }\r
+\r
+ /**\r
+ Add a path or path list into the path list\r
+\r
+ @param path The path string\r
+ **/\r
+ public void insPath(String path) {\r
+ if (path.length() == 0) {\r
+ return;\r
+ }\r
+\r
+ //\r
+ // split the value of "path" into separated single path\r
+ // \r
+ StringTokenizer tokens = new StringTokenizer(path, " \t,;", false);\r
+ while (tokens.hasMoreTokens()) {\r
+ String pathName = tokens.nextToken().trim();\r
+ if (pathName.length() > 0) {\r
+ //\r
+ // Make clean the path string before storing it\r
+ // \r
+ this.nameList.add(cleanupPath(pathName));\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ Handle "FileName" attribute. The value of "FileName" should be the path\r
+ of a file which contains path strings, one path per line.\r
+\r
+ @param pathFileName\r
+ **/\r
+ public void setPathFile(String pathFileName) {\r
+ FileReader fileReader = null;\r
+ BufferedReader in = null;\r
+ String path;\r
+\r
+ //\r
+ // Check if the file exists or not\r
+ // \r
+ File file = new File(pathFileName);\r
+ if (!file.exists()) {\r
+ throw new BuildException("The file, " + file + " does not exist!"); \r
+ } \r
+\r
+ try {\r
+ fileReader = new FileReader(file);\r
+ in = new BufferedReader(fileReader);\r
+\r
+ //\r
+ // Read the file line by line, skipping empty ones\r
+ // \r
+ nameList.clear();\r
+ while((path = in.readLine()) != null){\r
+ path = path.trim();\r
+ if (path.length() == 0){\r
+ continue;\r
+ }\r
+ //getProject().replaceProperties(path);\r
+\r
+ //\r
+ // Make clean the path string before storing it.\r
+ // \r
+ nameList.add(cleanupPath(path));\r
+ }\r
+ } catch (Exception e){\r
+ throw new BuildException(e.getMessage()); \r
+ } finally {\r
+ try {\r
+ //\r
+ // close the file\r
+ // \r
+ if (in != null) {\r
+ in.close();\r
+ }\r
+ if (fileReader != null) {\r
+ fileReader.close();\r
+ }\r
+ } catch (Exception e) {\r
+ throw new BuildException(e.getMessage()); \r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ Return the name list.\r
+\r
+ @return List<String> The list contains the name(path) strings\r
+ **/\r
+ public List<String> getNameList() {\r
+ return nameList;\r
+ }\r
+\r
+ /**\r
+ Compose and return the the name/path string without any delimiter. The trick\r
+ here is that it's actually used to return the value of nameList which\r
+ has just one name/string.\r
+\r
+ @return String\r
+ **/\r
+ public String toString() {\r
+ return toString("");\r
+ }\r
+\r
+ /**\r
+ Compose and return the name/path string concatenated by leading "prefix".\r
+\r
+ @param prefix The string will be put before each name/string in nameList\r
+ \r
+ @return String The string concatenated with "prefix"\r
+ **/\r
+ public String toString(String prefix) {\r
+ StringBuffer string = new StringBuffer(1024);\r
+ int length = nameList.size();\r
+\r
+ for (int i = 0; i < length; ++i) {\r
+ string.append(prefix);\r
+ string.append(nameList.get(i));\r
+ }\r
+\r
+ return string.toString();\r
+ }\r
+\r
+ /**\r
+ Compose and return the name/path string concatenated by space and\r
+ with only one "prefix".\r
+\r
+ @param prefix The prefix at the beginning of the string\r
+ \r
+ @return String The string with one prefix at the beginning\r
+ **/\r
+ public String toStringWithSinglepPrefix(String prefix) {\r
+ return prefix + toString(" ");\r
+ }\r
+\r
+ /**\r
+ Compose a string list with file names only, separated by spcified string\r
+\r
+ @param separator The separator string\r
+ \r
+ @return String The file list\r
+ **/\r
+ public String toFileList(String separator) {\r
+ StringBuffer string = new StringBuffer(1024);\r
+ int length = nameList.size();\r
+\r
+ for (int i = 0; i < length; ++i) {\r
+ File file = new File(nameList.get(i));\r
+ string.append(file.getName());\r
+ string.append(separator);\r
+ }\r
+\r
+ return string.toString();\r
+ }\r
+\r
+ /**\r
+ Compose a string list with file names only, separated by space\r
+\r
+ @return String The list string\r
+ **/\r
+ public String toFileList() {\r
+ return toFileList(" ");\r
+ }\r
+\r
+ //\r
+ // Remove any duplicated path separator or inconsistent path separator\r
+ //\r
+ private String cleanupPath(String path) {\r
+ String separator = "\\" + File.separator;\r
+ String duplicateSeparator = separator + "{2}";\r
+\r
+ path = Path.translateFile(path);\r
+ path = path.replaceAll(duplicateSeparator, separator);\r
+\r
+ return path;\r
+ }\r
+}\r