Urls { public static String resolveUrlExpr(String expression, URI uri){ int i = 0; StringBuilder source = new StringBuilder(expression); StringBuilder builder = new StringBuilder(); while (source.length()>0){ char c = source.charAt(i); source.deleteCharAt(i); if(c == '@'){ if(source.charAt(i) == '{'){ source.deleteCharAt(i); int index = source.indexOf("}"); if(-1 == index){ throw new IllegalArgumentException("error server url expression:"+expression); } String exp = source.substring(0,index); source.delete(0,index+1); builder.append(getExpressionValue(exp,uri)); } }else { if(c != '/' || builder.length() == 0){ builder.append(c); }else if(builder.charAt(builder.length()-1)!='/'){ builder.append(c); } } } return builder.toString(); } protected  Urls(); static String encode(String url); static String encode(String url, String charset); static String decode(String s); static String decode(String s, String charset); static boolean hasProtocolPrefix(String location); static boolean isClasspath(String resourceLocation); static String getQueryString(String uri); static String appendQueryString(String url, String queryString); static String appendQueryParams(String url, String name, String value); static String removeQueryString(String url); static boolean isUrl(String resourceLocation); static boolean isFileUrl(URL url); static boolean isJarUrl(URL url); static URI toURI(URL url); static URI toURI(String location); static URL getJarFileURL(URL jarUrl); static void setUseCachesIfNecessary(URLConnection con); static String getQueryString(Map<String, String> map); static StringBuilder getQueryStringBuilder(Map<String, String> map); static Map<String, String> queryStringToMap(String queryString); static String resolveUrlExpr(String expression, URI uri); static final String CLASSPATH_ONE_URL_PREFIX; static final String CLASSPATH_ALL_URL_PREFIX; static final String FILE_URL_PREFIX; static final String JAR_URL_SEPARATOR; static final String PROTOCOL_SEPARATOR; static final String PROTOCOL_FILE; static final String PROTOCOL_JAR; static final String PROTOCOL_ZIP; static final String PROTOCOL_VFSZIP; static final String PROTOCOL_VFS; static final String PROTOCOL_WSJAR; static final String PROTOCOL_CODE_SOURCE; }
@Test public void testResolveUrlExpr(){ URI uri1 = URI.create("http: URI uri2 = URI.create("http: String str1 = Urls.resolveUrlExpr("@{~/}path1",uri1); String str2 = Urls.resolveUrlExpr("@{~}path1",uri1); String str3 = Urls.resolveUrlExpr("@{/}path1",uri1); String str4 = Urls.resolveUrlExpr("@{^/}path1",uri1); String str5 = Urls.resolveUrlExpr("@{^}path1",uri1); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("/ctx/path1",str3); Assert.assertEquals("/path1",str4); Assert.assertEquals("path1",str5); str1 = Urls.resolveUrlExpr("@{~/}path1",uri2); str2 = Urls.resolveUrlExpr("@{~}path1",uri2); str3 = Urls.resolveUrlExpr("@{/}path1",uri2); str4 = Urls.resolveUrlExpr("@{^/}path1",uri2); str5 = Urls.resolveUrlExpr("@{^}path1",uri2); Assert.assertEquals("http: Assert.assertEquals("http: Assert.assertEquals("/ctx/path1",str3); Assert.assertEquals("/path1",str4); Assert.assertEquals("path1",str5); }