>
> It would be very easy to just use the standard Ant separator for entries
> (namely a ",") and have ant parse it into the OS dependant separator when
> required... not problem, instant solution.
>

I don't think "," is the standard ant separator. Where does it say this?

> In fact, when I first tried to use it, that's exactly what I assumed would
> happen... needless to say I was disappointed to find that I had to use a
> native separator :(

Its actually better than you realise. As it stands ant will accept a path
specification with either Windows or Unix standard path separators and
translate to the native platform's representation. This makes ant very
accepting. In general I try to stick to the Unix style separators in my
build files even though I work mostly on NT.

BTW, I wrote a class to tokenize paths into path components and I then
rewrote Project.translatePath to use it. This actually addresses a problem
raised by Phil Hanna to support absolute paths of the form C:/blah. It does
this without affecting the ability for single letter Unix paths to be
supported. The tokenizer is reusable in other places in the ant code where
you need to iterate over the components of a path.

I have attached it for comment.

Conor

Attachment: PathTokenizer.java
Description: Binary data

Index: Project.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.20
diff -u -r1.20 Project.java
--- Project.java        2000/05/27 22:21:09     1.20
+++ Project.java        2000/05/31 14:52:26
@@ -488,6 +488,43 @@
     }

     /**
+     * Translate a path according to the given path and file separators.
+     *
+     * This method is very accomodating, accepting either ':' or ';' as
+     * path separators and replacing with the given path separator.
+     * Similarly '/' and '\' are accepted as file separators and replaced
+     * with the given file spearator.
+     *
+     * DOS style drive specs are accomodated and passed thrugh unscathed.
+     */
+    private String translatePath(String to_process, String pathSeparator,
+                                 String fileSeparator) {
+        if ( to_process == null || to_process.length() == 0 ) return "";
+
+        StringBuffer path = new StringBuffer(to_process.length() + 50);
+        PathTokenizer tokenizer = new PathTokenizer(to_process);
+        while (tokenizer.hasMoreTokens()) {
+            String pathComponent = tokenizer.nextToken();
+            StringBuffer bs = new StringBuffer(pathComponent.length() + 10);
+            StringCharacterIterator sci = new 
StringCharacterIterator(pathComponent);
+            String tmp = null;
+            for (char c = sci.first(); c != CharacterIterator.DONE; c = 
sci.next()) {
+                tmp = String.valueOf(c);
+                if (tmp.equals("/") || tmp.equals ("\\"))
+                    tmp = fileSeparator;
+                bs.append(tmp);
+            }
+            if (path.length() != 0) {
+                path.append(pathSeparator);
+            }
+            path.append(bs);
+        }
+
+        return path.toString();
+    }
+
+
+    /**
         Translate a path into its native (platform specific)
         path. This should be extremely fast, code is
         borrowed from ECS project.
@@ -499,32 +536,18 @@
         @author Jon S. Stevens <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
     */
     public String translatePath(String to_process) {
-        if ( to_process == null || to_process.length() == 0 ) return "";
+        return translatePath(to_process, System.getProperty("path.separator"),
+                                         System.getProperty("file.separator"));
+    }

-        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 (tmp.equals(":") || tmp.equals(";"))
-                tmp = path;
-            else if (tmp.equals("/") || tmp.equals ("\\"))
-                tmp = file;
-            bs.append(tmp);
-        }
-        return(bs.toString());
+    /**
+     * Translate a path from the native platform into the internal platform
+     * independent representation. The internal representation uses the ':'
+     * character as the path separator and the '/' character as the file
+     * separator. This code handles DOS style drive specifications.
+     */
+    public String translatePathInternal(String to_process) {
+        return translatePath(to_process, ":","/");
     }

     /**

Reply via email to