1 package jodd.servlet; 2 3 import java.io.UnsupportedEncodingException; 4 import java.net.URLDecoder; 5 import java.net.URLEncoder; 6 7 10 public final class HtmlEncoder { 11 12 private static float newSizeFactor = 1.3f; 13 14 15 20 public static final String[] TABLE_HTML = new String[256]; 21 22 29 public static final String[] TABLE_HTML_STRICT = new String[256]; 30 31 static { 32 for (int i = 0; i < 10; i++) { 33 TABLE_HTML[i] = "�" + i + ";"; 34 } 35 for (int i = 10; i < 32; i++) { 36 TABLE_HTML[i] = "�" + i + ";"; 37 } 38 for (int i = 32; i < 128; i++) { 39 TABLE_HTML[i] = String.valueOf((char)i); 40 } 41 for (int i = 128; i < 256; i++) { 42 TABLE_HTML[i] = "&#" + i + ";"; 43 } 44 45 TABLE_HTML['\''] = "'"; TABLE_HTML['\"'] = """; TABLE_HTML['&'] = "&"; TABLE_HTML['<'] = "<"; TABLE_HTML['>'] = ">"; 52 System.arraycopy(TABLE_HTML, 0, TABLE_HTML_STRICT, 0, 256); 54 TABLE_HTML_STRICT[' '] = " "; 55 TABLE_HTML_STRICT['\n'] = "<br>"; TABLE_HTML_STRICT['\r'] = "<br>"; } 58 59 61 75 public static String encode(String string) { 76 if ((string == null) || (string.length() == 0)) { 77 return ""; 78 } 79 int n = string.length(); 80 StringBuffer buffer = new StringBuffer((int) (n * newSizeFactor)); 81 int tableLen = TABLE_HTML.length; 82 char c; 83 for (int i = 0; i < n; i++) { 84 c = string.charAt(i); 85 if (c < tableLen) { 86 buffer.append(TABLE_HTML[c]); 87 } else { 88 buffer.append("&#").append((int)c).append(';'); 89 } 90 } 91 return buffer.toString(); 92 } 93 94 111 public static String encodeTextStrict(String string) { 112 if ((string == null) || (string.length() == 0)) { 113 return ""; 114 } 115 int n = string.length(); 116 StringBuffer buffer = new StringBuffer((int) (n * newSizeFactor)); 117 int tableLen = TABLE_HTML_STRICT.length; 118 char c = 0, prev = 0; 119 for (int i = 0; i < n; i++, prev = c) { 120 c = string.charAt(i); 121 122 if ((c == '\n') && (prev == '\r')) { 123 continue; } 125 if (c < tableLen) { 126 buffer.append(TABLE_HTML_STRICT[c]); 127 } else { 128 buffer.append("&#").append((int)c).append(';'); 129 } 130 } 131 return buffer.toString(); 132 } 133 134 148 public static String encodeText(String string) { 149 if ((string == null) || (string.length() == 0)) { 150 return ""; 151 } 152 int n = string.length(); 153 StringBuffer buffer = new StringBuffer((int) (n * newSizeFactor)); 154 int tableLen = TABLE_HTML_STRICT.length; 155 char c = 0, prev = 0; 156 for (int i = 0; i < n; i++, prev = c) { 157 c = string.charAt(i);; 158 159 if (c == ' ') { 160 buffer.append(' '); 161 continue; 162 } 163 if ((c == '\n') && (prev == '\r')) { 164 continue; } 166 if (c < tableLen) { 167 buffer.append(TABLE_HTML_STRICT[c]); 168 } else { 169 buffer.append("&#").append((int)c).append(';'); 170 } 171 } 172 return buffer.toString(); 173 } 174 175 192 public static String encodeTextSmart(String string) { 193 if ((string == null) || (string.length() == 0)) { 194 return ""; 195 } 196 int n = string.length(); 197 StringBuffer buffer = new StringBuffer((int) (n * newSizeFactor)); 198 int tableLen = TABLE_HTML_STRICT.length; 199 char c = 0, prev = 0; 200 boolean prevSpace = false; 201 for (int i = 0; i < n; i++, prev = c) { 202 c = string.charAt(i);; 203 204 if (c == ' ') { 205 if (prev != ' ') { 206 prevSpace = false; 207 } 208 if (prevSpace == false) { 209 buffer.append(' '); 210 } else { 211 buffer.append(" "); 212 } 213 prevSpace = !prevSpace; 214 continue; 215 } 216 if ((c == '\n') && (prev == '\r')) { 217 continue; } 219 if (c < tableLen) { 220 buffer.append(TABLE_HTML_STRICT[c]); 221 } else { 222 buffer.append("&#").append((int)c).append(';'); 223 } 224 } 225 return buffer.toString(); 226 } 227 228 229 231 240 public static String encodeUrl(String string) { 241 return encodeUrl(string, "ISO-8859-1"); 242 } 243 252 public static String encodeUrl(String string, String encoding) { 253 if (string == null) { 254 return ""; 255 } 256 try { 257 return URLEncoder.encode(string, encoding); 258 } catch (UnsupportedEncodingException e) { 259 return null; 260 } 261 } 262 263 271 public static String decodeUrl(String string) { 272 return decodeUrl(string, "ISO-8859-1"); 273 } 274 275 283 public static String decodeUrl(String string, String encoding) { 284 if (string == null) { 285 return ""; 286 } 287 try { 288 return URLDecoder.decode(string, encoding); 289 } catch (UnsupportedEncodingException e) { 290 return null; 291 } 292 } 293 294 } 295 | Popular Tags |