1 17 package org.apache.forrest; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileReader ; 23 import java.io.IOException ; 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.types.Parameter; 29 import org.apache.tools.ant.types.selectors.BaseExtendSelector; 30 31 36 public class UncopiedFileSelector extends BaseExtendSelector { 37 38 public final static String CONFIG_KEY = "config"; 39 40 41 private String configFile = null; 42 private boolean configRead = false; 43 44 private Set goodFiles = new HashSet (); 45 46 47 public UncopiedFileSelector() { 48 } 49 50 public String toString() { 51 StringBuffer buf = new StringBuffer ("{uncopiedfileselector config: "); 52 buf.append(configFile); 53 buf.append("}"); 54 return buf.toString(); 55 } 56 57 public void setConfigFile(String conf) { 58 this.configFile = conf; 59 } 60 61 62 private void readConfig() throws BuildException { 63 if (configRead) return; 64 65 verifySettings(); 66 File confFile = new File (this.configFile); 67 try { 68 BufferedReader br = new BufferedReader (new FileReader (confFile)); 69 String line=null; 70 while ( (line = br.readLine()) != null) { 71 if (! (line.charAt(0) == ' ' || line.charAt(0) == '<') ) { 74 if (! goodFiles.contains(line)) { 75 goodFiles.add(line); 76 } 77 } 78 } 79 } catch (FileNotFoundException e) { 80 throw new BuildException("Couldn't find config file "+this.configFile); 81 } catch (IOException ioe) { 82 throw new BuildException("Error reading config file "+this.configFile); 83 } 84 configRead = true; 85 } 86 87 93 public void setParameters(Parameter[] parameters) { 94 super.setParameters(parameters); 95 if (parameters != null) { 96 for (int i = 0; i < parameters.length; i++) { 97 String paramname = parameters[i].getName(); 98 if (CONFIG_KEY.equalsIgnoreCase(paramname)) { 99 setConfigFile(parameters[i].getValue()); 100 } 101 else { 102 setError("Invalid parameter " + paramname); 103 } 104 } 105 } 106 } 107 108 113 public void verifySettings() { 114 if (configFile == null) { 115 setError("The "+UncopiedFileSelector.CONFIG_KEY+" attribute is required"); 116 } 117 } 118 119 129 public boolean isSelected(File basedir, String filename, File file) { 130 validate(); 131 readConfig(); 132 return goodFiles.contains(filename); 133 } 134 } 135 | Popular Tags |