public String abs2rel ( final String basePath , final String absPath ) { if ( ! isAbsolutePath ( absPath ) ) { return absPath ; } if ( isWindowsDrive ( absPath ) && isWindowsDrive ( basePath ) && absPath . charAt ( 0 ) != basePath . charAt ( 0 ) ) { return absPath ; } StringBuilder result = new StringBuilder ( ) ; String [ ] baseParts = getParts ( basePath ) ; String [ ] absParts = getParts ( absPath ) ; // extract common prefix int start = 0 ; for ( int i = 0 ; i < Math . min ( baseParts . length , absParts . length ) ; ++ i ) { if ( baseParts [ i ] . equals ( absParts [ i ] ) ) { start = i + 1 ; } } for ( int i = start ; i < baseParts . length ; ++ i ) { if ( result . length ( ) > 0 ) { result . append ( File . separator ) ; } result . append ( ".." ) ; } for ( int i = start ; i < absParts . length ; ++ i ) { if ( result . length ( ) > 0 ) { result . append ( File . separator ) ; } result . append ( absParts [ i ] ) ; } return result . toString ( ) ; }
Converts an absolute path into a relative one .