public static String cleanPath(String path){
  if (path == null) {
    return null;
  }
  String pathToUse=replace(path,WINDOWS_FOLDER_SEPARATOR,FOLDER_SEPARATOR);
  int prefixIndex=pathToUse.indexOf(":");
  String prefix="";
  if (prefixIndex != -1) {
    prefix=pathToUse.substring(0,prefixIndex + 1);
    if (prefix.contains("/")) {
      prefix="";
    }
 else {
      pathToUse=pathToUse.substring(prefixIndex + 1);
    }
  }
  if (pathToUse.startsWith(FOLDER_SEPARATOR)) {
    prefix=prefix + FOLDER_SEPARATOR;
    pathToUse=pathToUse.substring(1);
  }
  String[] pathArray=delimitedListToStringArray(pathToUse,FOLDER_SEPARATOR);
  List<String> pathElements=new LinkedList<String>();
  int tops=0;
  for (int i=pathArray.length - 1; i >= 0; i--) {
    String element=pathArray[i];
    if (CURRENT_PATH.equals(element)) {
    }
 else     if (TOP_PATH.equals(element)) {
      tops++;
    }
 else {
      if (tops > 0) {
        tops--;
      }
 else {
        pathElements.add(0,element);
      }
    }
  }
  for (int i=0; i < tops; i++) {
    pathElements.add(0,TOP_PATH);
  }
  return prefix + collectionToDelimitedString(pathElements,FOLDER_SEPARATOR);
}

public static String cleanPath ( String path ) { if ( path == null ) { return null ; } String pathToUse = replace ( path , WINDOWS_FOLDER_SEPARATOR , FOLDER_SEPARATOR ) ; int prefixIndex = pathToUse . indexOf ( ":" ) ; String prefix = "" ; if ( prefixIndex != - 1 ) { prefix = pathToUse . substring ( 0 , prefixIndex + 1 ) ; if ( prefix . contains ( "/" ) ) { prefix = "" ; } else { pathToUse = pathToUse . substring ( prefixIndex + 1 ) ; } } if ( pathToUse . startsWith ( FOLDER_SEPARATOR ) ) { prefix = prefix + FOLDER_SEPARATOR ; pathToUse = pathToUse . substring ( 1 ) ; } String [ ] pathArray = delimitedListToStringArray ( pathToUse , FOLDER_SEPARATOR ) ; List < String > pathElements = new LinkedList < String > ( ) ; int tops = 0 ; for ( int i = pathArray . length - 1 ; i >= 0 ; i -- ) { String element = pathArray [ i ] ; if ( CURRENT_PATH . equals ( element ) ) { } else if ( TOP_PATH . equals ( element ) ) { tops ++ ; } else { if ( tops > 0 ) { tops -- ; } else { pathElements . add ( 0 , element ) ; } } } for ( int i = 0 ; i < tops ; i ++ ) { pathElements . add ( 0 , TOP_PATH ) ; } return prefix + collectionToDelimitedString ( pathElements , FOLDER_SEPARATOR ) ; }
normalize the path by suppressing sequences like " path / . . " and inner simple dots . < p > the result is convenient for path comparison . for other uses , notice that windows separators ( " \ " ) are replaced by simple slashes .
