1 18 package org.apache.tools.ant.taskdefs; 19 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.Project; 23 import org.apache.tools.ant.Task; 24 import org.apache.tools.ant.DirectoryScanner; 25 import org.apache.tools.ant.util.FileUtils; 26 import org.apache.tools.ant.types.FileSet; 27 import org.apache.tools.ant.types.Path; 28 29 import java.io.File ; 30 import java.util.List ; 31 import java.util.LinkedList ; 32 import java.util.ListIterator ; 33 34 43 44 public class MakeUrl extends Task { 45 46 49 private String property; 50 51 54 private File file; 55 56 59 private String separator = " "; 60 61 64 private List filesets = new LinkedList (); 65 66 69 private List paths = new LinkedList (); 70 71 74 private boolean validate = true; 75 76 78 public static final String ERROR_MISSING_FILE = "A source file is missing :"; 79 80 public static final String ERROR_NO_PROPERTY = "No property defined"; 81 82 public static final String ERROR_NO_FILES = "No files defined"; 83 84 89 public void setProperty(String property) { 90 this.property = property; 91 } 92 93 98 public void setFile(File file) { 99 this.file = file; 100 } 101 102 108 public void addFileSet(FileSet fileset) { 109 filesets.add(fileset); 110 } 111 112 117 public void setSeparator(String separator) { 118 this.separator = separator; 119 } 120 121 127 public void setValidate(boolean validate) { 128 this.validate = validate; 129 } 130 131 137 public void addPath(Path path) { 138 paths.add(path); 139 } 140 141 146 private String filesetsToURL() { 147 if (filesets.isEmpty()) { 148 return ""; 149 } 150 int count = 0; 151 StringBuffer urls = new StringBuffer (); 152 ListIterator list = filesets.listIterator(); 153 while (list.hasNext()) { 154 FileSet set = (FileSet) list.next(); 155 DirectoryScanner scanner = set.getDirectoryScanner(getProject()); 156 String [] files = scanner.getIncludedFiles(); 157 for (int i = 0; i < files.length; i++) { 158 File f = new File (scanner.getBasedir(), files[i]); 159 validateFile(f); 160 String asUrl = toURL(f); 161 urls.append(asUrl); 162 log(asUrl, Project.MSG_DEBUG); 163 urls.append(separator); 164 count++; 165 } 166 } 167 return stripTrailingSeparator(urls, count); 169 } 170 171 179 private String stripTrailingSeparator(StringBuffer urls, 180 int count) { 181 if (count > 0) { 182 urls.delete(urls.length() - separator.length(), urls.length()); 183 return new String (urls); 184 } else { 185 return ""; 186 } 187 } 188 189 190 195 private String pathsToURL() { 196 if (paths.isEmpty()) { 197 return ""; 198 } 199 int count = 0; 200 StringBuffer urls = new StringBuffer (); 201 ListIterator list = paths.listIterator(); 202 while (list.hasNext()) { 203 Path path = (Path) list.next(); 204 String [] elements = path.list(); 205 for (int i = 0; i < elements.length; i++) { 206 File f = new File (elements[i]); 207 validateFile(f); 208 String asUrl = toURL(f); 209 urls.append(asUrl); 210 log(asUrl, Project.MSG_DEBUG); 211 urls.append(separator); 212 count++; 213 } 214 } 215 return stripTrailingSeparator(urls, count); 217 } 218 219 225 private void validateFile(File fileToCheck) { 226 if (validate && !fileToCheck.exists()) { 227 throw new BuildException(ERROR_MISSING_FILE + fileToCheck.toString()); 228 } 229 } 230 231 237 public void execute() throws BuildException { 238 validate(); 239 if (getProject().getProperty(property) != null) { 241 return; 242 } 243 String url; 244 String filesetURL = filesetsToURL(); 245 if (file != null) { 246 validateFile(file); 247 url = toURL(file); 248 if (filesetURL.length() > 0) { 250 url = url + separator + filesetURL; 251 } 252 } else { 253 url = filesetURL; 254 } 255 String pathURL = pathsToURL(); 257 if (pathURL.length() > 0) { 258 if (url.length() > 0) { 259 url = url + separator + pathURL; 260 } else { 261 url = pathURL; 262 } 263 } 264 log("Setting " + property + " to URL " + url, Project.MSG_VERBOSE); 265 getProject().setNewProperty(property, url); 266 } 267 268 272 private void validate() { 273 if (property == null) { 275 throw new BuildException(ERROR_NO_PROPERTY); 276 } 277 if (file == null && filesets.isEmpty() && paths.isEmpty()) { 278 throw new BuildException(ERROR_NO_FILES); 279 } 280 } 281 282 288 private String toURL(File fileToConvert) { 289 String url; 290 url = FileUtils.getFileUtils().toURI(fileToConvert.getAbsolutePath()); 293 294 return url; 295 } 296 297 } 298 | Popular Tags |