/** * Returns <code>true</code> if url <code>a</code> is a direct prefix of url <code>b</code>. For example, * <code>/news</code> is the parent of <code>/news/today</code>. * <p> * Note that <code>a</code> is also an extended prefix of <code>b</code> if <code>a</code> and <code>b</code> are * equal. * * @param a * the first url * @param b * the second url * @return <code>true</code> if <code>a</code> is the direct prefix of <code>b</code> */ public static boolean isPrefix(String a, String b) { if (isExtendedPrefix(a, b)) { if (a.length() < b.length()) { String bRest = b.substring(a.length() + 1); if (bRest.endsWith("/")) bRest = bRest.substring(0, bRest.length() - 2); return bRest.indexOf("/", 1) < 0; } else { return true; } } return false; }
/** * Returns the url extension that <code>url</code> defines over <code>prefix</code>. For example, the extension of url * <code>/news/today</code> and prefix <code>/news</code> is <code>today</code>. * <p> * If <code>prefix</code> is not a prefix of <code>url</code>, this method returns <code>null</code>, if * <code>url</code> and <code>prefix</code> match, the empty string is returned. * * @param prefix * the url prefix * @param url * the url * @return the url extension over the prefix */ public static String getExtension(String prefix, String url) { prefix = prefix.trim(); if (isExtendedPrefix(prefix, url)) { if (url.length() > prefix.length()) { String extension = url.substring(prefix.length() + 1); if (extension.endsWith("/")) { extension = extension.substring(0, extension.length() - 1); } return extension; } else return ""; } return null; }