Tools { public static String joinRelativeUrl(String baseUrl, String url) { if (baseUrl.indexOf('?') > 0) { baseUrl = baseUrl.substring(0, baseUrl.indexOf('?')); } boolean isWindowsUrl = baseUrl.indexOf('\\') >= 0; boolean isUsedInUnix = File.separatorChar == '/'; if (isUsedInUnix && isWindowsUrl) { baseUrl = baseUrl.replace('\\', '/'); url = url.replace('\\', '/'); } boolean isFile = baseUrl.startsWith("file:"); int ix = baseUrl.lastIndexOf('\\'); if (ix == -1) { ix = baseUrl.lastIndexOf('/'); } if (url.startsWith("/") && !isFile) { ix = baseUrl.indexOf("/", baseUrl.indexOf(" return baseUrl.substring(0, ix) + url; } while (url.startsWith(".\\") || url.startsWith("./")) { url = url.substring(2); } while (url.startsWith("../") || url.startsWith("..\\")) { int ix2 = baseUrl.lastIndexOf('\\', ix - 1); if (ix2 == -1) { ix2 = baseUrl.lastIndexOf('/', ix - 1); } if (ix2 == -1) { break; } baseUrl = baseUrl.substring(0, ix2 + 1); ix = ix2; url = url.substring(3); } while (url.contains("/./") || url.contains("\\.\\")) { int ix2 = url.indexOf("/./"); if (ix2 == -1) { ix2 = url.indexOf("\\.\\"); } url = url.substring(0, ix2) + url.substring(ix2 + 2); } while (url.contains("/../") || url.contains("\\..\\")) { int ix2 = -1; int ix3 = url.indexOf("/../"); if (ix3 == -1) { ix3 = url.indexOf("\\..\\"); ix2 = url.lastIndexOf('\\', ix3 - 1); } else { ix2 = url.lastIndexOf('/', ix3 - 1); } if (ix2 == -1) { break; } url = url.substring(0, ix2) + url.substring(ix3 + 3); } String result = baseUrl.substring(0, ix + 1) + url; if (isFile) { result = result.replace('/', File.separatorChar); } return result; }  static String[] tokenizeArgs(String args); static String convertToHtml(String str); static String getFilename(String filePath); static String getDir(String filePath); static String ensureDir(String dir, String basedir); static String ensureFileDir(String file, String basedir); static String ensureDir(String outputDir); static String modifyUrl(final String url, Integer mods); static void openURL(String url); static ByteArrayOutputStream readAll(InputStream instream, long maxSize); static void readAndWrite(InputStream instream, long maxSize, OutputStream outstream); static int copyFile(File source, File target, boolean overwrite); static String joinRelativeUrl(String baseUrl, String url); static boolean isEmpty(String str); static long writeAll(OutputStream out, InputStream in); static String expandProperties(StringToStringMap values, String content, boolean leaveMissing); static String replaceHost(String endpoint, String host); static String getEndpointFromUrl(URL baseUrl); static String getContentTypeFromFilename(String fileName); static String getExtensionForContentType(String contentType); static String getFileContent(String path); static String getTitleProperty(String path); static String normalizeFileSeparators(String input); static void assertSimilar(String expected, String real, char wildcard); static boolean isSimilar(String expected, String real, char wildcard); static void main(String[] args); static File createTemporaryDirectory(); static void deleteDirectoryOnExit(final File directory); static boolean isPropertyExpansion(@Nullable String value); static String removePropertyExpansions(String definitionUrl, String definition); static final int COPY_BUFFER_SIZE; static final long READ_ALL; }
@Test public void joinsUrlsCorrectly() throws Exception { assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("http: Tools.joinRelativeUrl("http: assertEquals("file:c:" + File.separator + "bil" + File.separator + "xsd" + File.separator + "test.xsd", Tools.joinRelativeUrl("file:c:\\bil\\test.wsdl", "./xsd/test.xsd")); assertEquals("file:c:" + File.separator + "bil" + File.separator + "xsd" + File.separator + "test.xsd", Tools.joinRelativeUrl("file:c:\\bil\\test\\test\\test.wsdl", "..\\..\\xsd\\test.xsd")); }