001: /*
002: * Created on 02-Feb-2006
003: */
004: package uk.org.ponder.stringutil;
005:
006: import java.io.UnsupportedEncodingException;
007: import java.net.URLDecoder;
008: import java.util.Map;
009: import java.util.StringTokenizer;
010:
011: import uk.org.ponder.util.Logger;
012: import uk.org.ponder.util.UniversalRuntimeException;
013:
014: /** Utilities for operating on URLs */
015:
016: public class URLUtil {
017:
018: public static boolean isAbsolute(String url) {
019: if (url.startsWith("javascript:") || url.startsWith("mailto:")
020: || url.startsWith("file:"))
021: return true;
022: int protpos = url.indexOf("://");
023: if (protpos == -1)
024: return false;
025: for (int i = 0; i < protpos; ++i) {
026: if (!Character.isLetterOrDigit(url.charAt(i)))
027: return false;
028: }
029: return true;
030: }
031:
032: /** Append the supplied name/value pair to the end of the supplied URL,
033: * after URLencoding name and value.
034: */
035:
036: public static String appendAttribute(String url, String name,
037: String value) {
038: CharWrap togo = new CharWrap(url);
039: int qpos = url.indexOf('?');
040: appendAttribute(togo, qpos == -1, name, value);
041: return togo.toString();
042: }
043:
044: /** Append the supplied name/value pair to the end of the supplied URL,
045: * after URLencoding name and value. The attribute will use the ? or &
046: * character according to whether <code>isfirst</code> is true or false.
047: */
048:
049: public static void appendAttribute(CharWrap togo, boolean isfirst,
050: String name, String value) {
051: togo.append(isfirst ? '?' : '&');
052: togo.append(URLEncoder.encode(name));
053: togo.append("=");
054: togo.append(URLEncoder.encode(value));
055: }
056:
057: /** Convert list of URL-form name/value pairs into a Map representation */
058: // TODO: backport vector values code
059: public static Map paramsToMap(String extraparams, Map target) {
060: if (Logger.log.isDebugEnabled()) {
061: Logger.log
062: .debug("Action link requires extra parameters from "
063: + extraparams);
064: }
065: StringTokenizer st = new StringTokenizer(extraparams, "&");
066: while (st.hasMoreTokens()) {
067: String token = st.nextToken();
068: int eqpos = token.indexOf("=");
069: String key = decodeURL(token.substring(0, eqpos));
070: String value = decodeURL(token.substring(eqpos + 1));
071: target.put(key, value);
072: }
073: return target;
074: }
075:
076: public static String[] splitPathInfo(String pathinfo) {
077: return pathinfo.split("/");
078: }
079:
080: /** Decodes a URL assuming UTF-8, and wrapping any tedious exceptions */
081: public static String decodeURL(String url) {
082: try {
083: return URLDecoder.decode(url, "UTF-8");
084: } catch (UnsupportedEncodingException e) {
085: throw UniversalRuntimeException.accumulate(e,
086: "Error decoding URL " + url + " using UTF-8");
087: }
088: }
089:
090: /** URL-encodes only the whitespace characters in a URL (necessary for some
091: * faulty incomplete encodings).
092: */
093:
094: public static String deSpace(String URL) {
095: CharWrap togo = new CharWrap(URL.length());
096: for (int i = 0; i < URL.length(); ++i) {
097: char c = URL.charAt(i);
098: if (Character.isWhitespace(c)) {
099: URLEncoder.appendURLHex(c, togo);
100: } else
101: togo.append(c);
102: }
103: return togo.toString();
104: }
105:
106: }
|