private String convertToRelative ( String srcFile , String base ) { String slash = SystemProperties . getProperty ( "file.separator" ) ; if ( FILE_IGNORE_CASE ) { srcFile = srcFile . toLowerCase ( ) ; base = base . toLowerCase ( ) ; } if ( base . equals ( srcFile ) ) { return "." ; } if ( ! base . endsWith ( slash ) ) { base = base + slash ; } if ( base . length ( ) <= srcFile . length ( ) ) { String root = srcFile . substring ( 0 , base . length ( ) ) ; if ( root . equals ( base ) ) { // Strip off the base directory, make relative return "." + SystemProperties . getProperty ( "file.separator" ) + srcFile . substring ( base . length ( ) ) ; } } // See if we can build a relative path above the base using .. notation int slashPos = srcFile . indexOf ( slash ) ; int branchPoint ; if ( slashPos >= 0 ) { String subPath = srcFile . substring ( 0 , slashPos ) ; if ( ( subPath . length ( ) == 0 ) || base . startsWith ( subPath ) ) { branchPoint = slashPos + 1 ; slashPos = srcFile . indexOf ( slash , branchPoint ) ; while ( slashPos >= 0 ) { subPath = srcFile . substring ( 0 , slashPos ) ; if ( base . startsWith ( subPath ) ) { branchPoint = slashPos + 1 ; } else { break ; } slashPos = srcFile . indexOf ( slash , branchPoint ) ; } int slashCount = 0 ; slashPos = base . indexOf ( slash , branchPoint ) ; while ( slashPos >= 0 ) { slashCount ++ ; slashPos = base . indexOf ( slash , slashPos + 1 ) ; } StringBuilder path = new StringBuilder ( ) ; String upDir = ".." + slash ; for ( int i = 0 ; i < slashCount ; i ++ ) { path . append ( upDir ) ; } path . append ( srcFile . substring ( branchPoint ) ) ; return path . toString ( ) ; } } return srcFile ; }
Converts a full path to a relative path if possible