📄 pathconvert.java
字号:
targetWindows = !targetOS.equals("unix") && !targetOS.equals("tandem"); } /** * Set whether the specified property will be set if the result * is the empty string. * @param setonempty true or false. * * @since Ant 1.5 */ public void setSetonempty(boolean setonempty) { this.setonempty = setonempty; } /** * Set the name of the property into which the converted path will be placed. * @param p the property name. */ public void setProperty(String p) { property = p; } /** * Add a reference to a Path, FileSet, DirSet, or FileList defined elsewhere. * @param r the reference to a path, fileset, dirset or filelist. */ public void setRefid(Reference r) { if (path != null) { throw noChildrenAllowed(); } refid = r; } /** * Set the default path separator string; defaults to current JVM * {@link java.io.File#pathSeparator File.pathSeparator}. * @param sep path separator string. */ public void setPathSep(String sep) { pathSep = sep; } /** * Set the default directory separator string; * defaults to current JVM {@link java.io.File#separator File.separator}. * @param sep directory separator string. */ public void setDirSep(String sep) { dirSep = sep; } /** * Learn whether the refid attribute of this element been set. * @return true if refid is valid. */ public boolean isReference() { return refid != null; } /** * Do the execution. * @throws BuildException if something is invalid. */ public void execute() throws BuildException { Union savedPath = path; String savedPathSep = pathSep; // may be altered in validateSetup String savedDirSep = dirSep; // may be altered in validateSetup try { // If we are a reference, create a Path from the reference if (isReference()) { Object o = refid.getReferencedObject(getProject()); if (!(o instanceof ResourceCollection)) { throw new BuildException("refid '" + refid.getRefId() + "' does not refer to a resource collection."); } getPath().add((ResourceCollection) o); } validateSetup(); // validate our setup // Currently, we deal with only two path formats: Unix and Windows // And Unix is everything that is not Windows // (with the exception for NetWare and OS/2 below) // for NetWare and OS/2, piggy-back on Windows, since here and // in the apply code, the same assumptions can be made as with // windows - that \\ is an OK separator, and do comparisons // case-insensitive. String fromDirSep = onWindows ? "\\" : "/"; StringBuffer rslt = new StringBuffer(); // Get the list of path components in canonical form String[] elems = path.list(); if (mapper != null) { FileNameMapper impl = mapper.getImplementation(); List ret = new ArrayList(); for (int i = 0; i < elems.length; ++i) { String[] mapped = impl.mapFileName(elems[i]); for (int m = 0; mapped != null && m < mapped.length; ++m) { ret.add(mapped[m]); } } elems = (String[]) ret.toArray(new String[ret.size()]); } for (int i = 0; i < elems.length; i++) { String elem = mapElement(elems[i]); // Apply the path prefix map // Now convert the path and file separator characters from the // current os to the target os. if (i != 0) { rslt.append(pathSep); } StringTokenizer stDirectory = new StringTokenizer(elem, fromDirSep, true); while (stDirectory.hasMoreTokens()) { String token = stDirectory.nextToken(); rslt.append(fromDirSep.equals(token) ? dirSep : token); } } // Place the result into the specified property, // unless setonempty == false if (setonempty || rslt.length() > 0) { String value = rslt.toString(); if (property == null) { log(value); } else { log("Set property " + property + " = " + value, Project.MSG_VERBOSE); getProject().setNewProperty(property, value); } } } finally { path = savedPath; dirSep = savedDirSep; pathSep = savedPathSep; } } /** * Apply the configured map to a path element. The map is used to convert * between Windows drive letters and Unix paths. If no map is configured, * then the input string is returned unchanged. * * @param elem The path element to apply the map to. * @return String Updated element. */ private String mapElement(String elem) { int size = prefixMap.size(); if (size != 0) { // Iterate over the map entries and apply each one. // Stop when one of the entries actually changes the element. for (int i = 0; i < size; i++) { MapEntry entry = (MapEntry) prefixMap.elementAt(i); String newElem = entry.apply(elem); // Note I'm using "!=" to see if we got a new object back from // the apply method. if (newElem != elem) { elem = newElem; break; // We applied one, so we're done } } } return elem; } /** * Add a mapper to convert the file names. * * @param mapper a <code>Mapper</code> value. */ public void addMapper(Mapper mapper) { if (this.mapper != null) { throw new BuildException( "Cannot define more than one mapper"); } this.mapper = mapper; } /** * Add a nested filenamemapper. * @param fileNameMapper the mapper to add. * @since Ant 1.6.3 */ public void add(FileNameMapper fileNameMapper) { Mapper m = new Mapper(getProject()); m.add(fileNameMapper); addMapper(m); } /** * Validate that all our parameters have been properly initialized. * * @throws BuildException if something is not set up properly. */ private void validateSetup() throws BuildException { if (path == null) { throw new BuildException("You must specify a path to convert"); } // Determine the separator strings. The dirsep and pathsep attributes // override the targetOS settings. String dsep = File.separator; String psep = File.pathSeparator; if (targetOS != null) { psep = targetWindows ? ";" : ":"; dsep = targetWindows ? "\\" : "/"; } if (pathSep != null) { // override with pathsep= psep = pathSep; } if (dirSep != null) { // override with dirsep= dsep = dirSep; } pathSep = psep; dirSep = dsep; } /** * Creates an exception that indicates that this XML element must not have * child elements if the refid attribute is set. * @return BuildException. */ private BuildException noChildrenAllowed() { return new BuildException("You must not specify nested " + "elements when using the refid attribute."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -