1 3 package jodd.servlet; 4 5 import javax.servlet.http.HttpServletRequest ; 6 import javax.servlet.http.HttpServletResponse ; 7 import javax.servlet.jsp.PageContext ; 8 import java.io.UnsupportedEncodingException ; 9 import java.net.URLEncoder ; 10 11 15 public class HtmlEncode { 16 17 protected static String EMPTY_STRING = ""; 18 19 protected static final char[][] TEXT = new char[64][]; 20 protected static final char[][] BLOCK = new char[64][]; 21 protected static final char[][] URL = new char[256][]; 22 23 26 static { 27 for (int i = 0; i < 64; i++) { 28 TEXT[i] = new char[] {(char) i}; 29 } 30 for (char c = 0; c < 256; c++) { 31 try { 32 URL[c] = URLEncoder.encode(String.valueOf(c), "ISO-8859-1").toCharArray(); 33 } catch (UnsupportedEncodingException ueex) { 34 ueex.printStackTrace(); 35 } 36 } 37 38 TEXT['\''] = "'".toCharArray(); TEXT['"'] = """.toCharArray(); TEXT['&'] = "&".toCharArray(); TEXT['<'] = "<".toCharArray(); TEXT['>'] = ">".toCharArray(); 45 System.arraycopy(TEXT, 0, BLOCK, 0, 64); 47 BLOCK['\n'] = "<br>".toCharArray(); BLOCK['\r'] = "<br>".toCharArray(); } 50 51 53 public static String text(Object object) { 54 if (object == null) { 55 return EMPTY_STRING; 56 } 57 return text(object.toString()); 58 } 59 60 71 public static String text(String text) { 72 int len; 73 if ((text == null) || ((len = text.length()) == 0)) { 74 return EMPTY_STRING; 75 } 76 StringBuffer buffer = new StringBuffer (len + (len >> 2)); 77 for (int i = 0; i < len; i++) { 78 char c = text.charAt(i); 79 if (c < 64) { 80 buffer.append(TEXT[c]); 81 } else { 82 buffer.append(c); 83 } 84 } 85 return buffer.toString(); 86 } 87 88 90 public static String block(Object object) { 91 if (object == null) { 92 return EMPTY_STRING; 93 } 94 return block(object.toString()); 95 } 96 97 98 109 public static String block(String text) { 110 int len; 111 if ((text == null) || ((len = text.length()) == 0)) { 112 return EMPTY_STRING; 113 } 114 StringBuffer buffer = new StringBuffer (len + (len >> 2)); 115 char c, prev = 0; 116 for (int i = 0; i < len; i++, prev = c) { 117 c = text.charAt(i); 118 if ((c == '\n') && (prev == '\r')) { 119 continue; } 121 if (c < 64) { 122 buffer.append(BLOCK[c]); 123 } else { 124 buffer.append(c); 125 } 126 } 127 return buffer.toString(); 128 } 129 130 131 133 public static String strict(Object object) { 134 if (object == null) { 135 return EMPTY_STRING; 136 } 137 return strict(object.toString()); 138 } 139 140 141 153 public static String strict(String text) { 154 int len; 155 if ((text == null) || ((len = text.length()) == 0)) { 156 return EMPTY_STRING; 157 } 158 StringBuffer buffer = new StringBuffer (len + (len >> 2)); 159 char c, prev = 0; 160 boolean prevSpace = false; 161 for (int i = 0; i < len; i++, prev = c) { 162 c = text.charAt(i); 163 164 if (c == ' ') { 165 if (prev != ' ') { 166 prevSpace = false; 167 } 168 if (prevSpace == false) { 169 buffer.append(' '); 170 } else { 171 buffer.append(" "); 172 } 173 prevSpace = !prevSpace; 174 continue; 175 } 176 if ((c == '\n') && (prev == '\r')) { 177 continue; } 179 if (c < 64) { 180 buffer.append(BLOCK[c]); 181 } else { 182 buffer.append(c); 183 } 184 } 185 return buffer.toString(); 186 } 187 188 189 190 192 public static String url(String value, PageContext pageContext) { 193 return url(value, "UTF-8", (HttpServletRequest ) pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 194 } 195 public static String url(String value, String encoding, PageContext pageContext) { 196 return url(value, encoding, (HttpServletRequest ) pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 197 } 198 199 public static String url(String value, HttpServletRequest request, HttpServletResponse response) { 200 return url(value, "UTF-8", request, response); 201 } 202 203 public static String url(String value, String encoding, HttpServletRequest request, HttpServletResponse response) { 204 String result = ServletUtil.resolveUrl(url(value, encoding), request); 205 if (ServletUtil.isAbsoluteUrl(result) == false) { 206 result = response.encodeURL(result); } 208 return result; 209 } 210 211 public static String url(String url) { 212 return url(url, "UTF-8"); 213 } 214 215 221 public static String url(String url, String encoding) { 222 int paramNdx = url.indexOf('?'); 223 if (paramNdx == -1) { 224 return url; 225 } 226 StringBuffer result = new StringBuffer (url.length() >> 1); 227 paramNdx++; 228 result.append(url.substring(0, paramNdx)); 229 while (true) { 230 int ampNdx = url.indexOf('&', paramNdx); 231 String q; 232 if (ampNdx == -1) { 233 q = url.substring(paramNdx); 234 } else { 235 q = url.substring(paramNdx, ampNdx); 236 } 237 int eqNdx = q.indexOf('='); 238 if (eqNdx == -1) { 239 result.append(q); 240 } else { 241 String name = q.substring(0, eqNdx); 242 appendUrl(result, name, encoding); 243 result.append('='); 244 String value = q.substring(eqNdx + 1); 245 if (value.length() > 0) { 246 appendUrl(result, value, encoding); 247 } 248 } 249 if (ampNdx == -1) { 250 break; 251 } 252 result.append('&'); 253 paramNdx = ampNdx + 1; 254 } 255 return result.toString(); 256 } 257 258 259 private static void appendUrl(StringBuffer result, String value, String encoding) { 260 byte[] bytes; 261 try { 262 bytes = value.getBytes(encoding); 263 } catch (UnsupportedEncodingException ueex) { 264 throw new IllegalArgumentException (ueex.toString()); 265 } 266 for (int i = 0; i < bytes.length; i++) { 267 int b = bytes[i]; 268 if (b < 0) { 269 b += 256; 270 } 271 result.append(URL[b]); 272 } 273 } 274 275 276 277 278 280 public static class UrlBuilder { 281 StringBuffer url; 282 String encoding; 283 HttpServletResponse response; 284 private boolean hasParams = false; 285 286 UrlBuilder(String baseUrl, String encoding, HttpServletRequest request, HttpServletResponse response) { 287 if (request != null) { 288 baseUrl = ServletUtil.resolveUrl(baseUrl, request); 289 } 290 url = new StringBuffer (baseUrl); 291 this.encoding = encoding; 292 this.response = response; 293 } 294 295 public UrlBuilder p(String name, String value) { 296 return param(name, value); 297 } 298 public UrlBuilder p(String name, Object value) { 299 return param(name, value); 300 } 301 public UrlBuilder param(String name, Object value) { 302 return param(name, value == null ? null : value.toString()); 303 } 304 public UrlBuilder param(String name, String value) { 305 url.append(hasParams ? '&' : '?'); 306 hasParams = true; 307 appendUrl(url, name, encoding); 308 if ((value != null) && (value.length() > 0)) { 309 url.append('='); 310 appendUrl(url, value, encoding); 311 } 312 return this; 313 } 314 315 public UrlBuilder p(String nameValue) { 316 return param(nameValue); 317 } 318 public UrlBuilder param(String nameValue) { 319 url.append(hasParams ? '&' : '?'); 320 hasParams = true; 321 int eqNdx = nameValue.indexOf('='); 322 String name; String value = null; 323 if (eqNdx == -1) { 324 name = nameValue; 325 } else { 326 name = nameValue.substring(0, eqNdx); 327 value = nameValue.substring(eqNdx + 1); 328 } 329 330 appendUrl(url, name, encoding); 331 if ((value != null) && (value.length() > 0)) { 332 url.append('='); 333 appendUrl(url, value, encoding); 334 } 335 return this; 336 } 337 338 339 public String toString() { 340 if (response != null) { 341 return response.encodeURL(url.toString()); 342 } 343 return url.toString(); 344 } 345 } 346 347 public static UrlBuilder baseUrl(String url) { 348 return new UrlBuilder(url, "UTF-8", null, null); 349 } 350 public static UrlBuilder baseUrl(String url, String encoding) { 351 return new UrlBuilder(url, encoding, null, null); 352 } 353 354 public static UrlBuilder baseUrl(String url, PageContext pageContext) { 355 return new UrlBuilder(url, "UTF-8", (HttpServletRequest ) pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 356 } 357 public static UrlBuilder baseUrl(String url, String encoding, PageContext pageContext) { 358 return new UrlBuilder(url, encoding, (HttpServletRequest ) pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 359 } 360 361 public static UrlBuilder baseUrl(String url, HttpServletRequest request, HttpServletResponse response) { 362 return new UrlBuilder(url, "UTF-8", request, response); 363 } 364 public static UrlBuilder baseUrl(String url, String encoding, HttpServletRequest request, HttpServletResponse response) { 365 return new UrlBuilder(url, encoding, request, response); 366 } 367 368 369 } 370 | Popular Tags |