@Test public void relativization() { List<String> cases= ListBuilder. <String>builder(). add("http: add("http: add("http: add("http: add("http: add(" add(" add(" add(" add(" add(""). add("/"). add("/file"). add("/directory/subdirectory/"). add("/directory/subdirectory/file"). add("file"). add("directory/subdirectory/"). add("directory/subdirectory/file"). add("urn:opaque:path"). build(); for(String rawBase:cases) { URI base=URI.create(rawBase); for(String rawTarget:cases) { URI target=URI.create(rawTarget); LOGGER.debug("<{}>.relativize(<{}>)=\n\t- JDK: <{}>\n\t- Custom: <{}>",base,target,base.relativize(target),URIUtils.relativize(base, target)); } } }
public static URI relativize(URI base, URI target) { Objects.requireNonNull(base,BASE_URI_CANNOT_BE_NULL); Objects.requireNonNull(target,TARGET_URI_CANNOT_BE_NULL); URI nTarget = target.normalize(); if(areRelativizable(base,target)) { URI nBase=base.normalize(); if(nBase.equals(nTarget)) { nTarget=URI.create(EMPTY); } else { URI walkthrough = absoluteRelativization(nBase,nTarget); if(!(walkthrough.getPath().startsWith(PARENT) && nTarget.getPath().isEmpty())) { nTarget=walkthrough; } } } return nTarget; }
URIUtils { public static URI relativize(URI base, URI target) { Objects.requireNonNull(base,BASE_URI_CANNOT_BE_NULL); Objects.requireNonNull(target,TARGET_URI_CANNOT_BE_NULL); URI nTarget = target.normalize(); if(areRelativizable(base,target)) { URI nBase=base.normalize(); if(nBase.equals(nTarget)) { nTarget=URI.create(EMPTY); } else { URI walkthrough = absoluteRelativization(nBase,nTarget); if(!(walkthrough.getPath().startsWith(PARENT) && nTarget.getPath().isEmpty())) { nTarget=walkthrough; } } } return nTarget; } }
URIUtils { public static URI relativize(URI base, URI target) { Objects.requireNonNull(base,BASE_URI_CANNOT_BE_NULL); Objects.requireNonNull(target,TARGET_URI_CANNOT_BE_NULL); URI nTarget = target.normalize(); if(areRelativizable(base,target)) { URI nBase=base.normalize(); if(nBase.equals(nTarget)) { nTarget=URI.create(EMPTY); } else { URI walkthrough = absoluteRelativization(nBase,nTarget); if(!(walkthrough.getPath().startsWith(PARENT) && nTarget.getPath().isEmpty())) { nTarget=walkthrough; } } } return nTarget; } private  URIUtils(); }
URIUtils { public static URI relativize(URI base, URI target) { Objects.requireNonNull(base,BASE_URI_CANNOT_BE_NULL); Objects.requireNonNull(target,TARGET_URI_CANNOT_BE_NULL); URI nTarget = target.normalize(); if(areRelativizable(base,target)) { URI nBase=base.normalize(); if(nBase.equals(nTarget)) { nTarget=URI.create(EMPTY); } else { URI walkthrough = absoluteRelativization(nBase,nTarget); if(!(walkthrough.getPath().startsWith(PARENT) && nTarget.getPath().isEmpty())) { nTarget=walkthrough; } } } return nTarget; } private  URIUtils(); static URL toURL(URI uri); static URI relativize(URI base, URI target); static URI resolve(URI base, URI target); }
URIUtils { public static URI relativize(URI base, URI target) { Objects.requireNonNull(base,BASE_URI_CANNOT_BE_NULL); Objects.requireNonNull(target,TARGET_URI_CANNOT_BE_NULL); URI nTarget = target.normalize(); if(areRelativizable(base,target)) { URI nBase=base.normalize(); if(nBase.equals(nTarget)) { nTarget=URI.create(EMPTY); } else { URI walkthrough = absoluteRelativization(nBase,nTarget); if(!(walkthrough.getPath().startsWith(PARENT) && nTarget.getPath().isEmpty())) { nTarget=walkthrough; } } } return nTarget; } private  URIUtils(); static URL toURL(URI uri); static URI relativize(URI base, URI target); static URI resolve(URI base, URI target);  }