diff --git a/src/main/org/apache/tools/ant/PathTokenizer.java b/src/main/org/apache/tools/ant/PathTokenizer.java index ded0ab4d5..5e59cdd5a 100644 --- a/src/main/org/apache/tools/ant/PathTokenizer.java +++ b/src/main/org/apache/tools/ant/PathTokenizer.java @@ -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 diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index f24fafcb4..61d6a0378 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -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. -

- 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 jon@clearink.com - */ + * Translate a path into its native (platform specific) format. + *

+ * 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(); } /**