1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.File ; 21 import java.util.List ; 22 import java.util.Vector ; 23 import java.util.ArrayList ; 24 import java.util.StringTokenizer ; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.taskdefs.condition.Os; 29 import org.apache.tools.ant.types.Path; 30 import org.apache.tools.ant.types.Mapper; 31 import org.apache.tools.ant.types.Reference; 32 import org.apache.tools.ant.types.ResourceCollection; 33 import org.apache.tools.ant.types.EnumeratedAttribute; 34 import org.apache.tools.ant.types.resources.Union; 35 import org.apache.tools.ant.util.FileNameMapper; 36 37 44 public class PathConvert extends Task { 45 46 49 private static boolean onWindows = Os.isFamily("dos"); 50 51 55 private Union path = null; 56 59 private Reference refid = null; 60 63 private String targetOS = null; 64 67 private boolean targetWindows = false; 68 71 private boolean setonempty = true; 72 75 private String property = null; 76 79 private Vector prefixMap = new Vector (); 80 83 private String pathSep = null; 84 87 private String dirSep = null; 88 89 90 private Mapper mapper = null; 91 92 95 public PathConvert() { 96 } 97 98 105 public class MapEntry { 106 107 private String from = null; 109 private String to = null; 110 111 118 public void setFrom(String from) { 119 this.from = from; 120 } 121 122 126 public void setTo(String to) { 127 this.to = to; 128 } 129 130 136 public String apply(String elem) { 137 if (from == null || to == null) { 138 throw new BuildException("Both 'from' and 'to' must be set " 139 + "in a map entry"); 140 } 141 String cmpElem = 144 onWindows ? elem.toLowerCase().replace('\\', '/') : elem; 145 String cmpFrom = 146 onWindows ? from.toLowerCase().replace('\\', '/') : from; 147 148 151 return cmpElem.startsWith(cmpFrom) 152 ? to + elem.substring(from.length()) : elem; 153 } 154 } 155 156 160 public static class TargetOs extends EnumeratedAttribute { 161 164 public String [] getValues() { 165 return new String []{"windows", "unix", "netware", "os/2", "tandem"}; 166 } 167 } 168 169 173 public Path createPath() { 174 if (isReference()) { 175 throw noChildrenAllowed(); 176 } 177 Path result = new Path(getProject()); 178 add(result); 179 return result; 180 } 181 182 187 public void add(ResourceCollection rc) { 188 if (isReference()) { 189 throw noChildrenAllowed(); 190 } 191 getPath().add(rc); 192 } 193 194 private synchronized Union getPath() { 195 if (path == null) { 196 path = new Union(); 197 path.setProject(getProject()); 198 } 199 return path; 200 } 201 202 206 public MapEntry createMap() { 207 MapEntry entry = new MapEntry(); 208 prefixMap.addElement(entry); 209 return entry; 210 } 211 212 221 public void setTargetos(String target) { 222 TargetOs to = new TargetOs(); 223 to.setValue(target); 224 setTargetos(to); 225 } 226 227 235 public void setTargetos(TargetOs target) { 236 targetOS = target.getValue(); 237 238 241 245 targetWindows = !targetOS.equals("unix") && !targetOS.equals("tandem"); 246 } 247 248 255 public void setSetonempty(boolean setonempty) { 256 this.setonempty = setonempty; 257 } 258 259 263 public void setProperty(String p) { 264 property = p; 265 } 266 267 271 public void setRefid(Reference r) { 272 if (path != null) { 273 throw noChildrenAllowed(); 274 } 275 refid = r; 276 } 277 278 283 public void setPathSep(String sep) { 284 pathSep = sep; 285 } 286 287 288 293 public void setDirSep(String sep) { 294 dirSep = sep; 295 } 296 297 301 public boolean isReference() { 302 return refid != null; 303 } 304 305 309 public void execute() throws BuildException { 310 Union savedPath = path; 311 String savedPathSep = pathSep; String savedDirSep = dirSep; 314 try { 315 if (isReference()) { 317 Object o = refid.getReferencedObject(getProject()); 318 if (!(o instanceof ResourceCollection)) { 319 throw new BuildException("refid '" + refid.getRefId() 320 + "' does not refer to a resource collection."); 321 } 322 getPath().add((ResourceCollection) o); 323 } 324 validateSetup(); 326 330 String fromDirSep = onWindows ? "\\" : "/"; 335 336 StringBuffer rslt = new StringBuffer (); 337 338 String [] elems = path.list(); 340 341 if (mapper != null) { 342 FileNameMapper impl = mapper.getImplementation(); 343 List ret = new ArrayList (); 344 for (int i = 0; i < elems.length; ++i) { 345 String [] mapped = impl.mapFileName(elems[i]); 346 for (int m = 0; mapped != null && m < mapped.length; ++m) { 347 ret.add(mapped[m]); 348 } 349 } 350 elems = (String []) ret.toArray(new String [ret.size()]); 351 } 352 for (int i = 0; i < elems.length; i++) { 353 String elem = mapElement(elems[i]); 355 358 if (i != 0) { 359 rslt.append(pathSep); 360 } 361 StringTokenizer stDirectory = 362 new StringTokenizer (elem, fromDirSep, true); 363 364 while (stDirectory.hasMoreTokens()) { 365 String token = stDirectory.nextToken(); 366 rslt.append(fromDirSep.equals(token) ? dirSep : token); 367 } 368 } 369 if (setonempty || rslt.length() > 0) { 372 String value = rslt.toString(); 373 if (property == null) { 374 log(value); 375 } else { 376 log("Set property " + property + " = " + value, 377 Project.MSG_VERBOSE); 378 getProject().setNewProperty(property, value); 379 } 380 } 381 } finally { 382 path = savedPath; 383 dirSep = savedDirSep; 384 pathSep = savedPathSep; 385 } 386 } 387 388 396 private String mapElement(String elem) { 397 398 int size = prefixMap.size(); 399 400 if (size != 0) { 401 402 405 for (int i = 0; i < size; i++) { 406 MapEntry entry = (MapEntry) prefixMap.elementAt(i); 407 String newElem = entry.apply(elem); 408 409 412 if (newElem != elem) { 413 elem = newElem; 414 break; } 416 } 417 } 418 return elem; 419 } 420 421 426 public void addMapper(Mapper mapper) { 427 if (this.mapper != null) { 428 throw new BuildException( 429 "Cannot define more than one mapper"); 430 } 431 this.mapper = mapper; 432 } 433 434 439 public void add(FileNameMapper fileNameMapper) { 440 Mapper m = new Mapper(getProject()); 441 m.add(fileNameMapper); 442 addMapper(m); 443 } 444 445 450 private void validateSetup() throws BuildException { 451 452 if (path == null) { 453 throw new BuildException("You must specify a path to convert"); 454 } 455 String dsep = File.separator; 458 String psep = File.pathSeparator; 459 460 if (targetOS != null) { 461 psep = targetWindows ? ";" : ":"; 462 dsep = targetWindows ? "\\" : "/"; 463 } 464 if (pathSep != null) { 465 psep = pathSep; 467 } 468 if (dirSep != null) { 469 dsep = dirSep; 471 } 472 pathSep = psep; 473 dirSep = dsep; 474 } 475 476 481 private BuildException noChildrenAllowed() { 482 return new BuildException("You must not specify nested " 483 + "elements when using the refid attribute."); 484 } 485 486 } 487 488 | Popular Tags |