1 package org.jahia.clipbuilder.html.util; 2 3 import java.net.*; 4 5 import java.io.*; 6 import java.util.*; 7 import org.apache.commons.lang.StringUtils; 8 9 14 public abstract class URLUtilities { 15 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(URLUtilities.class); 16 17 18 24 public static URL getURL(String url) { 25 URL uurl = null; 26 try { 27 uurl = new URL(url); 28 29 } 30 catch (MalformedURLException ex) { 31 logger.error(" [ Url " + url + " malformed ] "); 32 } 33 return uurl; 34 } 35 36 37 40 72 79 public static String getHrefAbsoluteValue(URL baseURL, String url) { 80 81 try { 83 if (isRelatif(url)) { 84 return getAbsolutURL(baseURL, url).toExternalForm(); 85 } 86 else { 87 return url; 88 } 89 } 90 catch (MalformedURLException ex) { 91 logger.error("[ Malformed url ]"); 92 return url; 93 } 94 } 95 96 97 105 public static String getUrlAbsoluteValue(String baseUrl, String url) throws MalformedURLException { 106 logger.debug("[ Get absolute url of: " + url + " ]"); 107 logger.debug("[ Base url: " + baseUrl + " ]"); 108 String res = getAbsolutURL(getURL(baseUrl), url).toExternalForm(); 109 110 logger.debug("[ Result: " + res + " ]"); 111 return res; 112 } 113 114 115 121 public static boolean isRelatif(String url) { 122 try { 123 new URL(url); 124 return false; 125 } 126 catch (MalformedURLException ex) { 127 return true; 128 } 129 130 } 131 132 133 141 public static URL getAbsolutURL(final URL baseURL, String relativeUrl) throws MalformedURLException { 142 if (relativeUrl == null) { 143 logger.error("relative Url is null"); 144 logger.error("Returned value: " + baseURL.toExternalForm()); 145 return baseURL; 146 } 147 if (relativeUrl.indexOf("//") == 0) { 148 return getURL("http:" + relativeUrl); 150 } 151 return com.gargoylesoftware.htmlunit.WebClient.expandUrl(baseURL, relativeUrl); 152 } 153 154 155 161 public static String [] splitQuery(String url) { 162 String [] result = {"", ""}; 163 164 int queryIndex = url.indexOf('?'); 166 if (queryIndex > 1) { 167 result[0] = url.substring(0, queryIndex); 169 result[1] = url.substring(queryIndex + 1); 170 178 } 179 else { 180 result[0] = url; 182 } 183 187 return result; 188 } 189 190 191 197 public static String encode(String url) { 198 url = StringUtils.replaceChars(url, "/", "_"); 199 url = StringUtils.replaceChars(url, "&", "_"); 200 url = StringUtils.replaceChars(url, "+", "_"); 201 url = StringUtils.replaceChars(url, "#", "_"); 202 return url; 203 } 204 205 206 207 214 public static ArrayList parseUrl(String urlValue) throws IOException { 215 ArrayList param = new ArrayList(); 217 URL url = null; 218 try { 219 url = new URL(urlValue); 220 } 221 catch (IOException ex) { 222 ex.printStackTrace(); 223 } 224 225 String uri = url.getFile(); 227 228 String query = StringUtilities.replaceSpecialGraphics(url.getQuery()); 230 231 if (query != null) { 232 String [] queryParam = query.split("&"); 234 for (int i = 0; i < queryParam.length; i++) { 235 String nameAndValue = queryParam[i]; 237 int indexEqual = nameAndValue.indexOf('='); 238 try { 239 String name = URLDecoder.decode(nameAndValue.substring(0, indexEqual), "UTF-8"); 240 String value = URLDecoder.decode(nameAndValue.substring(indexEqual + 241 1), "UTF-8"); 242 String [] p = {name, value}; 243 param.add(p); 244 } 245 catch (UnsupportedEncodingException exc) { 246 exc.printStackTrace(); 247 } 248 } 249 } 250 return param; 251 } 252 253 254 260 private static String getPath(String url) { 261 String path = url; 262 int lastIndexSeparator = url.lastIndexOf('/'); 263 if (lastIndexSeparator > 0) { 264 path = url.substring(0, lastIndexSeparator); 265 } 266 logger.debug("[ Path is: " + path + " ]"); 267 return path; 268 } 269 270 } 271 | Popular Tags |