> > so i'm willing to submit my code for review, and (maybe) replacement.
> > what do i do ? post it right here ?
> 
> That would be great. If you just want to send the relevent method(s) then that 
> would be great. Alternatively you could send a unified patch (-u option) and 
> someone can apply that. Sending a patch is the preferred method and is simple 
> as
> 
> cvs diff -u FileUtil.java >> patchfile.txt

ok, so here's a file with the method. it's not a patch though.
also, it's been designed with unix & windows in mind.
i don't know the specificities of other file systems.
    /**
     * normalize a path.
     * that means : changes to unix style if under windows, removes occurences of "./" 
and process "../" when necessary.
     * <br><br>
     * if path is absolute (starts with '/') and there are too many occurences of 
"../" (would then have some kind
     * of 'negative' path) returns null.<br>
     * if path is relative, the exceeding ../ are kept at the begining of the 
path.<br><br>
     * please note that this method has been tested with unix and windows only.
     *
     *
     * @param path the path to be normalized.
     *
     * @return the normalized path or null.
     * @throws NullPointerException if path is null.
     */
    public static final String normalize (String path)
    {
        if (path.length () < 2)
            return path;

        StringBuffer buff = new StringBuffer (path);

        int length = path.length ();

         // this whole prefix thing is for windows compatibility only.
        String prefix = null;

        if (length > 2 && buff.charAt (1)==':')
        {
            prefix = path.substring (0,2);
            buff.delete (0,2);
            path = path.substring (2);
            length -= 2;
        }

        boolean startsWithSlash = length > 0 && (buff.charAt (0) == '/' || buff.charAt 
(0) == '\\');

        boolean expStart = true;
        int ptCount = 0;
        int lastPoint = -1;
        int lastSlash = length+1;
        int upLevel = 0;

        for (int i=length-1;i>=0;i--)
            switch (path.charAt (i))
            {
                case '\\':
                    buff.setCharAt (i,'/');
                case '/':
                    if (lastSlash == i+1)
                        buff.deleteCharAt (i);

                    switch (ptCount)
                    {
                        case 1:
                            buff.delete (i,lastSlash);
                            break;

                        case 2:
                            upLevel ++;
                            break;

                        default:
                            if (upLevel > 0 && lastSlash != i+1)
                            {
                                buff.delete (i,lastSlash+3);
                                upLevel --;
                            }
                            break;
                    }

                    ptCount = 0;
                    expStart = true;
                    lastSlash = i;
                    break;

                case '.':
                    if (expStart)
                        ptCount ++;

                    lastPoint = i;
                    break;

                default:
                    ptCount = 0;
                    expStart = false;
                    break;
            }

        switch (ptCount)
        {
            case 1:
                buff.delete (0,lastSlash);
                break;

            case 2:
                break;

            default:
                if (upLevel > 0)
                {
                    if (startsWithSlash)
                        return null;
                    else
                        upLevel = 1;
                }

                while (upLevel > 0)
                {
                    buff.delete (0,lastSlash+3);
                    upLevel --;
                }
                break;
        }

        length = buff.length ();
        boolean isLengthNull = length == 0;
        char firstChar = isLengthNull?0:buff.charAt (0);

        if (!startsWithSlash && !isLengthNull && firstChar == '/')
            buff.deleteCharAt (0);
        else if (startsWithSlash && (isLengthNull || (!isLengthNull && firstChar != 
'/')))
            buff.insert (0,'/');

        if (prefix != null)
            buff.insert (0,prefix);

        return buff.toString ();
    }

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to