1 10 package org.mmbase.util; 11 import org.mmbase.util.logging.*; 12 13 23 public class URLParamEscape { 24 25 private static Logger log = Logging.getLoggerInstance(URLParamEscape.class.getName()); 27 28 32 static boolean isacceptable[] = 33 { 34 false, false, false, false, false, false, false, false, false, false, true, false, true, true, true, false, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, false }; 47 48 51 static char hex[] = { 52 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 53 }; 54 55 58 static char HEX_ESCAPE='%'; 59 60 67 public static String escapeurl(String str) { 68 StringBuffer esc = new StringBuffer (); 69 byte buf[]; 70 try { 71 buf = str.getBytes("UTF-8"); 72 } catch (java.io.UnsupportedEncodingException uee) { 73 return str; } 75 for (int i = 0; i<buf.length;i++) { 76 int a = (int)buf[i] & 0xff; 77 if (a>=32 && a<128 && isacceptable[a-32]) { 78 esc.append((char)a); 79 } else { 80 esc.append(HEX_ESCAPE); 81 esc.append(hex[a >> 4]); 82 esc.append(hex[a & 15]); 83 } 84 } 85 return esc.toString(); 86 } 87 88 94 private static char from_hex(char c) { 95 return (char)(c >= '0' && c <= '9' ? c - '0' 96 : c >= 'A' && c <= 'F' ? c - 'A' + 10 97 : c - 'a' + 10); 98 } 99 100 107 public static String unescapeurl(String str) { 108 int i; 109 char j,t; 110 StringBuffer esc=new StringBuffer (); 111 112 if (str!=null) { 113 for (i=0;i<str.length();i++) { 114 t=str.charAt(i); 115 if (t==HEX_ESCAPE) { 116 t=str.charAt(++i); 117 j=(char)(from_hex(t)*16); 118 t=str.charAt(++i); 119 j+=from_hex(t); 120 esc.append(j); 121 } else { 122 esc.append(t); 123 } 124 } 125 } else { 126 log.warn("Unescapeurl -> Bogus parameter"); 127 } 128 return esc.toString(); 129 } 130 131 134 public static void main(String args[]) { 135 for (int i=0;i<args.length;i++) { 136 log.info("Original : '"+args[i]+"'"); 137 log.info("Escaped : '"+escapeurl(args[i])+"'"); 138 log.info("Unescaped again : '"+unescapeurl(escapeurl(args[i]))+"'"); 139 } 140 141 } 142 } 143 | Popular Tags |