Browse Source

Change Project.translatePath to use PathTokenizer

I have changed the Project.translatePath method to use the PathTokenizer. This
adds support for paths of the format C:/blah. These used to translate to C;\blah
but this will now be C:\blah.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267749 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 25 years ago
parent
commit
ea7cd6fbfe
2 changed files with 35 additions and 35 deletions
  1. +8
    -1
      src/main/org/apache/tools/ant/PathTokenizer.java
  2. +27
    -34
      src/main/org/apache/tools/ant/Project.java

+ 8
- 1
src/main/org/apache/tools/ant/PathTokenizer.java View File

@@ -77,8 +77,15 @@ public class PathTokenizer {
*/
private String lookahead = null;

/**
* Flag to indicate whether we are running on a platform with a DOS style
* filesystem
*/
private boolean dosStyleFilesystem;

public PathTokenizer(String path) {
tokenizer = new StringTokenizer(path, ":;", false);
dosStyleFilesystem = File.pathSeparatorChar == ';';
}

public boolean hasMoreTokens() {
@@ -100,7 +107,7 @@ public class PathTokenizer {
}
if (token.length() == 1 && Character.isLetter(token.charAt(0))
&& File.pathSeparator.equals(";")
&& dosStyleFilesystem
&& tokenizer.hasMoreTokens()) {
// we are on a dos style system so this path could be a drive
// spec. We look at the next token


+ 27
- 34
src/main/org/apache/tools/ant/Project.java View File

@@ -499,43 +499,36 @@ public class Project {
}

/**
Translate a path into its native (platform specific)
path. This should be extremely fast, code is
borrowed from ECS project.
<p>
All it does is translate the : into ; and / into \
if needed. In other words, it isn't perfect.

@returns translated string or empty string if to_process is null or empty
@author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
*/
* Translate a path into its native (platform specific) format.
* <p>
* This method uses the PathTokenizer class to separate the input path
* into its components. This handles DOS style paths in a relatively
* sensible way. The file separators are then converted to their platform
* specific versions.
*
* @param to_process the path to be converted
*
* @return the native version of to_process or
* an empty string if to_process is null or empty
*/
static public String translatePath(String to_process) {
if ( to_process == null || to_process.length() == 0 ) return "";

StringBuffer bs = new StringBuffer(to_process.length() + 50);
StringCharacterIterator sci = new StringCharacterIterator(to_process);
String path = System.getProperty("path.separator");
String file = System.getProperty("file.separator");
String tmp = null;
for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) {
tmp = String.valueOf(c);

if (tmp.equals(":")) {
// could be a DOS drive or a Unix path separator...
// if followed by a backslash, assume it is a drive
c = sci.next();
tmp = String.valueOf(c);
bs.append( tmp.equals("\\") ? ":" : path );
if (c == CharacterIterator.DONE) break;
}
if ( to_process == null || to_process.length() == 0 ) {
return "";
}

if (tmp.equals(":") || tmp.equals(";"))
tmp = path;
else if (tmp.equals("/") || tmp.equals ("\\"))
tmp = file;
bs.append(tmp);
StringBuffer path = new StringBuffer(to_process.length() + 50);
PathTokenizer tokenizer = new PathTokenizer(to_process);
while (tokenizer.hasMoreTokens()) {
String pathComponent = tokenizer.nextToken();
pathComponent = pathComponent.replace('/', File.separatorChar);
pathComponent = pathComponent.replace('\\', File.separatorChar);
if (path.length() != 0) {
path.append(File.pathSeparatorChar);
}
path.append(pathComponent);
}
return(bs.toString());
return path.toString();
}

/**


Loading…
Cancel
Save