@Test public void runTests() throws IOException { for ( int x = 0; x < PARENTS.length; x++ ) { File parent = new File(PARENTS[x]).getCanonicalFile(); File child = new File(CHILDREN[x]).getCanonicalFile(); String answer = ANSWER[x]; String r = RelativizePath.convertToRelativePath( parent, child ); assertEquals(String.format("parent %s child %s", parent.toString(), child.toString()), answer, r ); } }
static String convertToRelativePath( File thing, File relativeTo ) { StringBuilder relativePath; if ( thing.getParentFile().equals( relativeTo.getParentFile() ) ) { return thing.getName(); } List<String> thingDirectories = RelativizePath.parentDirs( thing ); List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo ); int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size(); int lastCommonRoot = -1; int index; for ( index = 0; index < length; index++ ) { if ( thingDirectories.get( index ).equals( relativeToDirectories.get( index ) ) ) { lastCommonRoot = index; } else { break; } } if ( lastCommonRoot != -1 ) { relativePath = new StringBuilder(); for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) { relativePath.append( "../" ); } for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) { relativePath.append( thingDirectories.get( index ) ).append( '/' ); } relativePath.append( thing.getName() ); return relativePath.toString(); } return null; }
RelativizePath { static String convertToRelativePath( File thing, File relativeTo ) { StringBuilder relativePath; if ( thing.getParentFile().equals( relativeTo.getParentFile() ) ) { return thing.getName(); } List<String> thingDirectories = RelativizePath.parentDirs( thing ); List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo ); int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size(); int lastCommonRoot = -1; int index; for ( index = 0; index < length; index++ ) { if ( thingDirectories.get( index ).equals( relativeToDirectories.get( index ) ) ) { lastCommonRoot = index; } else { break; } } if ( lastCommonRoot != -1 ) { relativePath = new StringBuilder(); for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) { relativePath.append( "../" ); } for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) { relativePath.append( thingDirectories.get( index ) ).append( '/' ); } relativePath.append( thing.getName() ); return relativePath.toString(); } return null; } }
RelativizePath { static String convertToRelativePath( File thing, File relativeTo ) { StringBuilder relativePath; if ( thing.getParentFile().equals( relativeTo.getParentFile() ) ) { return thing.getName(); } List<String> thingDirectories = RelativizePath.parentDirs( thing ); List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo ); int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size(); int lastCommonRoot = -1; int index; for ( index = 0; index < length; index++ ) { if ( thingDirectories.get( index ).equals( relativeToDirectories.get( index ) ) ) { lastCommonRoot = index; } else { break; } } if ( lastCommonRoot != -1 ) { relativePath = new StringBuilder(); for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) { relativePath.append( "../" ); } for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) { relativePath.append( thingDirectories.get( index ) ).append( '/' ); } relativePath.append( thing.getName() ); return relativePath.toString(); } return null; } private  RelativizePath(); }
RelativizePath { static String convertToRelativePath( File thing, File relativeTo ) { StringBuilder relativePath; if ( thing.getParentFile().equals( relativeTo.getParentFile() ) ) { return thing.getName(); } List<String> thingDirectories = RelativizePath.parentDirs( thing ); List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo ); int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size(); int lastCommonRoot = -1; int index; for ( index = 0; index < length; index++ ) { if ( thingDirectories.get( index ).equals( relativeToDirectories.get( index ) ) ) { lastCommonRoot = index; } else { break; } } if ( lastCommonRoot != -1 ) { relativePath = new StringBuilder(); for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) { relativePath.append( "../" ); } for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) { relativePath.append( thingDirectories.get( index ) ).append( '/' ); } relativePath.append( thing.getName() ); return relativePath.toString(); } return null; } private  RelativizePath();  }
RelativizePath { static String convertToRelativePath( File thing, File relativeTo ) { StringBuilder relativePath; if ( thing.getParentFile().equals( relativeTo.getParentFile() ) ) { return thing.getName(); } List<String> thingDirectories = RelativizePath.parentDirs( thing ); List<String> relativeToDirectories = RelativizePath.parentDirs( relativeTo ); int length = thingDirectories.size() < relativeToDirectories.size() ? thingDirectories.size() : relativeToDirectories.size(); int lastCommonRoot = -1; int index; for ( index = 0; index < length; index++ ) { if ( thingDirectories.get( index ).equals( relativeToDirectories.get( index ) ) ) { lastCommonRoot = index; } else { break; } } if ( lastCommonRoot != -1 ) { relativePath = new StringBuilder(); for ( index = lastCommonRoot + 1; index < relativeToDirectories.size(); index++ ) { relativePath.append( "../" ); } for ( index = lastCommonRoot + 1; index < thingDirectories.size(); index++ ) { relativePath.append( thingDirectories.get( index ) ).append( '/' ); } relativePath.append( thing.getName() ); return relativePath.toString(); } return null; } private  RelativizePath();   }