\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 just to define common interfaces for nested element\r
+ Interface NestElement is to define common interfaces for nested element\r
**/\r
-public interface NestElement {\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
+ private List<String> nameList = new ArrayList<String>();\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
+ 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
+ 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
+ while((str = in.readLine()) != null){\r
+ str = str.trim();\r
+ if (str.length() == 0){\r
+ continue;\r
+ }\r
+\r
+ //getProject().replaceProperties(str);\r
+ 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
+ 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
+ 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
+ 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
- nested element Interface for up-casting \r
+ Return the name list.\r
+\r
+ @return List<String> The list contains the name(path) strings\r
**/\r
- \r
- public String getName();\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
- public void setName(String name);\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
- public String toString();\r
+ for (int i = 0; i < length; ++i) {\r
+ string.append(prefix);\r
+ string.append(nameList.get(i));\r
+ }\r
\r
- public File getFile();\r
+ return string.toString();\r
+ }\r
\r
- public void setFile(File file);\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
- public void setList(String fileNameList);\r
+ path = Path.translateFile(path);\r
+ path = path.replaceAll(duplicateSeparator, separator);\r
\r
- public List<String> getList();\r
+ return path;\r
+ }\r
}\r