1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.Task; 24 25 52 53 public class Basename extends Task { 54 private File file; 55 private String property; 56 private String suffix; 57 58 62 public void setFile(File file) { 63 this.file = file; 64 } 65 66 70 public void setProperty(String property) { 71 this.property = property; 72 } 73 74 78 public void setSuffix(String suffix) { 79 this.suffix = suffix; 80 } 81 82 87 public void execute() throws BuildException { 88 if (property == null) { 89 throw new BuildException("property attribute required", getLocation()); 90 } 91 if (file == null) { 92 throw new BuildException("file attribute required", getLocation()); 93 } 94 String value = file.getName(); 95 if (suffix != null && value.endsWith(suffix)) { 96 int pos = value.length() - suffix.length(); 100 if (pos > 0 && suffix.charAt(0) != '.' 101 && value.charAt(pos - 1) == '.') { 102 pos--; 103 } 104 value = value.substring(0, pos); 105 } 106 getProject().setNewProperty(property, value); 107 } 108 } 109 110 | Popular Tags |