ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Antoine Levy-Lambert" <anto...@antbuild.com>
Subject Re: FileUtils#normalize(File)
Date Mon, 28 Jul 2003 10:55:54 GMT
----- Original Message -----
From: "Wannheden, Knut" <knut.wannheden@paranor.ch>
Sent: Monday, July 28, 2003 12:33 PM


> I've noticed that the normalize(File) method in FileUtils requires that
the
> File to normalize is absolute.  I was wondering what the reason is for
this
> reason.  Would it be too complex to normalize a relative path on Windows
> systems?
>
I do not know.
> I think it makes perfectly sense to normalize a path like "./foo" into
"foo"
> or "foo/bar/.." into "foo".  Even a path like "../foo" could be normalized
> using the system property "user.dir", but it could also cause an Exception
> to be thrown as the absolute path "/../foo" would (a Unix system would
even
> normalize that as "/foo").
>
> The reason I'm askin is because I wanted to reuse the code in normalize
> which handles ".." and "." path segments.  But maybe I should copy and
paste
> instead.
The best and most sure option is to factor out the lines of codes that you
are interested in in a separate method, which would be called both by
Normalize and by your specific code for VMS.

I guess these are the lines you are interested in :

these lines could be a method
String resolvePath(String root, String path)

        Stack s = new Stack();
        s.push(root);
        StringTokenizer tok = new StringTokenizer(path, File.separator);
        while (tok.hasMoreTokens()) {
            String thisToken = tok.nextToken();
            if (".".equals(thisToken)) {
                continue;
            } else if ("..".equals(thisToken)) {
                if (s.size() < 2) {
                    throw new BuildException("Cannot resolve path " + orig);
                } else {
                    s.pop();
                }
            } else { // plain component
                s.push(thisToken);
            }
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.size(); i++) {
            if (i > 1) {
                // not before the filesystem root and not after it, since
root
                // already contains one
                sb.append(File.separatorChar);
            }
            sb.append(s.elementAt(i));
        }


        path = sb.toString();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message