1 18 19 package org.apache.tools.ant.types.selectors; 20 21 import org.apache.tools.ant.types.Mapper; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.util.IdentityMapper; 24 import org.apache.tools.ant.util.FileNameMapper; 25 import org.apache.tools.ant.util.FileUtils; 26 27 import java.io.File ; 28 29 33 public abstract class MappingSelector extends BaseSelector { 34 35 36 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 37 38 40 protected File targetdir = null; 41 protected Mapper mapperElement = null; 42 protected FileNameMapper map = null; 43 protected int granularity = 0; 44 45 47 51 public MappingSelector() { 52 granularity = (int) FILE_UTILS.getFileTimestampGranularity(); 53 } 54 55 56 62 public void setTargetdir(File targetdir) { 63 this.targetdir = targetdir; 64 } 65 66 71 public Mapper createMapper() throws BuildException { 72 if (mapperElement != null) { 73 throw new BuildException("Cannot define more than one mapper"); 74 } 75 mapperElement = new Mapper(getProject()); 76 return mapperElement; 77 } 78 79 83 public void verifySettings() { 84 if (targetdir == null) { 85 setError("The targetdir attribute is required."); 86 } 87 if (mapperElement == null) { 88 map = new IdentityMapper(); 89 } else { 90 map = mapperElement.getImplementation(); 91 } 92 if (map == null) { 93 setError("Could not set <mapper> element."); 94 } 95 } 96 97 106 public boolean isSelected(File basedir, String filename, File file) { 107 108 validate(); 110 111 String [] destfiles = map.mapFileName(filename); 113 if (destfiles == null) { 116 return false; 117 } 118 if (destfiles.length != 1 || destfiles[0] == null) { 120 throw new BuildException("Invalid destination file results for " 121 + targetdir.getName() + " with filename " + filename); 122 } 123 String destname = destfiles[0]; 124 File destfile = new File (targetdir, destname); 125 126 boolean selected = selectionTest(file, destfile); 127 return selected; 128 } 129 130 136 protected abstract boolean selectionTest(File srcfile, File destfile); 137 138 144 public void setGranularity(int granularity) { 145 this.granularity = granularity; 146 } 147 148 } 149 | Popular Tags |