1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import java.io.File ; 22 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.types.EnumeratedAttribute; 25 import org.apache.tools.ant.types.Mapper; 26 import org.apache.tools.ant.util.FileNameMapper; 27 import org.apache.tools.ant.util.IdentityMapper; 28 29 37 public class PresentSelector extends BaseSelector { 38 39 private File targetdir = null; 40 private Mapper mapperElement = null; 41 private FileNameMapper map = null; 42 private boolean destmustexist = true; 43 44 48 public PresentSelector() { 49 } 50 51 54 public String toString() { 55 StringBuffer buf = new StringBuffer ("{presentselector targetdir: "); 56 if (targetdir == null) { 57 buf.append("NOT YET SET"); 58 } else { 59 buf.append(targetdir.getName()); 60 } 61 buf.append(" present: "); 62 if (destmustexist) { 63 buf.append("both"); 64 } else { 65 buf.append("srconly"); 66 } 67 if (map != null) { 68 buf.append(map.toString()); 69 } else if (mapperElement != null) { 70 buf.append(mapperElement.toString()); 71 } 72 buf.append("}"); 73 return buf.toString(); 74 } 75 76 82 public void setTargetdir(File targetdir) { 83 this.targetdir = targetdir; 84 } 85 86 91 public Mapper createMapper() throws BuildException { 92 if (mapperElement != null) { 93 throw new BuildException("Cannot define more than one mapper"); 94 } 95 mapperElement = new Mapper(getProject()); 96 return mapperElement; 97 } 98 99 100 112 public void setPresent(FilePresence fp) { 113 if (fp.getIndex() == 0) { 114 destmustexist = false; 115 } 116 } 117 118 122 public void verifySettings() { 123 if (targetdir == null) { 124 setError("The targetdir attribute is required."); 125 } 126 if (mapperElement == null) { 127 map = new IdentityMapper(); 128 } else { 129 map = mapperElement.getImplementation(); 130 } 131 if (map == null) { 132 setError("Could not set <mapper> element."); 133 } 134 } 135 136 145 public boolean isSelected(File basedir, String filename, File file) { 146 147 validate(); 149 150 String [] destfiles = map.mapFileName(filename); 152 if (destfiles == null) { 155 return false; 156 } 157 if (destfiles.length != 1 || destfiles[0] == null) { 159 throw new BuildException("Invalid destination file results for " 160 + targetdir + " with filename " + filename); 161 } 162 String destname = destfiles[0]; 163 File destfile = new File (targetdir, destname); 164 return destfile.exists() == destmustexist; 165 } 166 167 171 public static class FilePresence extends EnumeratedAttribute { 172 175 public String [] getValues() { 176 return new String []{"srconly", "both"}; 177 } 178 } 179 180 } 181 182 | Popular Tags |