1 19 20 package org.apache.excalibur.instrument.manager.http.server; 21 22 import java.io.UnsupportedEncodingException ; 23 24 29 public class URLCoder 30 { 31 32 private URLCoder() 33 { 34 } 35 36 39 private static char getDigit( int d ) 40 { 41 char c = Character.forDigit( d, 16 ); 42 if ( Character.isLetter( c ) ) 43 { 44 c = Character.toUpperCase( c ); 45 } 46 return c; 47 } 48 49 53 private static void encodeChars( String value, String encoding, StringBuffer sb ) 54 throws UnsupportedEncodingException 55 { 56 byte[] bytes; 57 if ( encoding == null ) 58 { 59 bytes = value.getBytes(); 60 } 61 else 62 { 63 bytes = value.getBytes( encoding ); 64 } 65 66 for ( int i = 0; i < bytes.length; i++ ) 67 { 68 sb.append( '%' ); 69 int b = (int)bytes[i]; 70 sb.append( getDigit( ( b & 0xf0 ) >> 4 ) ); 71 sb.append( getDigit( b & 0xf ) ); 72 } 73 } 74 75 private static String decodeChars( char[] chars, int start, int count, String encoding ) 76 throws UnsupportedEncodingException 77 { 78 byte[] bytes = new byte[count / 3]; 79 80 int pos = start; 81 int bPos = 0; 82 boolean bad = false; 83 while ( pos < start + count ) 84 { 85 char c = chars[pos]; 86 if ( c != '%' ) 87 { 88 bad = true; 89 break; 90 } 91 pos++; 92 93 int b = 0; 94 95 if ( pos >= chars.length ) 96 { 97 bad = true; 98 break; 99 } 100 c = chars[pos]; 101 if ( ( c >= 'A' ) && ( c <= 'F' ) ) 102 { 103 b = b + 10 + ( c - 'A' ); 104 } 105 else if ( ( c >= '0' ) && ( c <= '9' ) ) 106 { 107 b = b + c - '0'; 108 } 109 else 110 { 111 bad = true; 112 break; 113 } 114 b = b << 4; 115 pos++; 116 117 if ( pos >= chars.length ) 118 { 119 bad = true; 120 break; 121 } 122 c = chars[pos]; 123 if ( ( c >= 'A' ) && ( c <= 'F' ) ) 124 { 125 b = b + 10 + ( c - 'A' ); 126 } 127 else if ( ( c >= '0' ) && ( c <= '9' ) ) 128 { 129 b = b + c - '0'; 130 } 131 else 132 { 133 bad = true; 134 break; 135 } 136 pos++; 137 138 bytes[bPos++] = (byte)( b & 0xff ); 139 } 140 141 if ( bad ) 142 { 143 throw new IllegalArgumentException ( "Unexpected character at position " + pos ); 144 } 145 146 if ( encoding == null ) 147 { 148 return new String ( bytes ); 149 } 150 else 151 { 152 return new String ( bytes, encoding ); 153 } 154 } 155 156 public static String encode( String value, String encoding ) 157 throws UnsupportedEncodingException 158 { 159 boolean changed = false; 160 StringBuffer sb = new StringBuffer (); 161 162 char[] chars = value.toCharArray(); 163 164 int firstEncodeIndex = -1; 165 int encodeCount = 0; 166 StringBuffer encodeSb = new StringBuffer (); 167 for ( int i = 0; i < chars.length; i++ ) 168 { 169 char c = chars[i]; 170 boolean encode; 171 if ( c == ' ' ) 172 { 173 c = '+'; 175 changed = true; 176 encode = false; 177 } 178 else if ( ( ( c >= 'a' ) && ( c <= 'z' ) ) 179 || ( ( c >= 'A' ) && ( c <= 'Z' ) ) 180 || ( ( c >= '0' ) && ( c <= '9' ) ) 181 || ( c == '-' ) || ( c == '_' ) || ( c == '.' ) || ( c == '*' ) ) 182 { 183 encode = false; 185 } 186 else 187 { 188 changed = true; 190 encode = true; 191 } 192 193 if ( encode ) 194 { 195 if ( firstEncodeIndex < 0 ) 197 { 198 firstEncodeIndex = i; 199 } 200 encodeCount++; 201 } 202 else 203 { 204 if ( firstEncodeIndex >= 0 ) 206 { 207 encodeChars( new String ( chars, firstEncodeIndex, encodeCount ), encoding, sb ); 209 firstEncodeIndex = -1; 210 encodeCount = 0; 211 } 212 213 sb.append( c ); 214 } 215 } 216 217 if ( firstEncodeIndex >= 0 ) 219 { 220 encodeChars( new String ( chars, firstEncodeIndex, encodeCount ), encoding, sb ); 222 firstEncodeIndex = -1; 223 encodeCount = 0; 224 } 225 226 if ( changed ) 227 { 228 return sb.toString(); 229 } 230 else 231 { 232 return value; 233 } 234 } 235 236 public static String decode( String value, String encoding ) 237 throws UnsupportedEncodingException 238 { 239 boolean changed = false; 240 StringBuffer sb = new StringBuffer (); 241 242 char[] chars = value.toCharArray(); 243 244 int firstDecodeIndex = -1; 245 int decodeCount = 0; 246 for ( int i = 0; i < chars.length; i++ ) 247 { 248 char c = chars[i]; 249 boolean decode; 250 if ( c == '+' ) 251 { 252 c = ' '; 253 decode = false; 254 changed = true; 255 } 256 else if ( c == '%' ) 257 { 258 decode = true; 259 changed = true; 260 } 261 else 262 { 263 decode = false; 264 } 265 266 if ( decode ) 267 { 268 if ( firstDecodeIndex < 0 ) 270 { 271 firstDecodeIndex = i; 272 } 273 decodeCount += 3; i += 2; 275 } 276 else 277 { 278 if ( firstDecodeIndex >= 0 ) 280 { 281 sb.append( decodeChars( chars, firstDecodeIndex, decodeCount, encoding ) ); 282 firstDecodeIndex = -1; 283 decodeCount = 0; 284 } 285 286 sb.append( c ); 287 } 288 } 289 290 if ( firstDecodeIndex >= 0 ) 292 { 293 sb.append( decodeChars( chars, firstDecodeIndex, decodeCount, encoding ) ); 294 firstDecodeIndex = -1; 295 decodeCount = 0; 296 } 297 298 if ( changed ) 299 { 300 return sb.toString(); 301 } 302 else 303 { 304 return value; 305 } 306 } 307 } | Popular Tags |