Java Path Normalize normalizePath(String path)

Here you can find the source of normalizePath(String path)

Description

Remove relative references and "mistakes" like double slashes from the path.

License

Open Source License

Parameter

Parameter Description
path The path to normalize.

Return

The normalized path.

Declaration

public static String normalizePath(String path) 

Method Source Code

//package com.java2s;
/*//from   w w  w  . j av a  2  s  .c  o m
 * Leech - crawling capabilities for Apache Tika
 * 
 * Copyright (C) 2012 DFKI GmbH, Author: Christian Reuschling
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact us by mail: christian.reuschling@dfki.de
 */

public class Main {
    /**
     * Remove relative references and "mistakes" like double slashes from the path.
     * 
     * @param path The path to normalize.
     * @return The normalized path.
     */
    public static String normalizePath(String path) {
        String result = path;

        // replace all double slashes with a single slash
        result = replace("//", "/", result);

        // replace all references to the current directory with nothing
        result = replace("/./", "/", result);

        // replace all references to the parent directory with nothing
        result = result.replaceAll("/[^/]+/\\.\\./", "/");

        return result;
    }

    /**
     * Substitute String "old" by String "new" in String "text" everywhere.
     * 
     * @param olds The String to be substituted.
     * @param news The String containing the new content.
     * @param text The String in which the substitution is done.
     * @return The result String containing the substitutions; if no substitutions were made, the specified 'text' instance is returned.
     */
    protected static String replace(String olds, String news, String text) {
        if (olds == null || olds.length() == 0) {
            // nothing to substitute.
            return text;
        }
        if (text == null) {
            return null;
        }

        // search for any occurences of 'olds'.
        int oldsIndex = text.indexOf(olds);
        if (oldsIndex == -1) {
            // Nothing to substitute.
            return text;
        }

        // we're going to do some substitutions.
        StringBuilder buffer = new StringBuilder(text.length());
        int prevIndex = 0;

        while (oldsIndex >= 0) {
            // first, add the text between the previous and the current occurence
            buffer.append(text.substring(prevIndex, oldsIndex));

            // then add the substition pattern
            buffer.append(news);

            // remember the index for the next loop
            prevIndex = oldsIndex + olds.length();

            // search for the next occurence
            oldsIndex = text.indexOf(olds, prevIndex);
        }

        // add the part after the last occurence
        buffer.append(text.substring(prevIndex));

        return buffer.toString();
    }
}

Related

  1. normalizePath(String path)
  2. normalizePath(String path)
  3. normalizePath(String path)
  4. normalizePath(String path)
  5. normalizePath(String path)
  6. normalizePath(String path)
  7. normalizePath(String path)
  8. normalizePath(String path)
  9. normalizePath(String path)