1 16 17 package org.apache.taglibs.standard.lang.jpath.encoding; 18 19 22 public class HtmlEncoder { 23 24 private static final String [] htmlCode = new String [256]; 25 26 static { 27 for (int i = 0; i < 10; i++) { 28 htmlCode[i] = "�" + i + ";"; 29 } 30 31 for (int i = 10; i < 32; i++) { 32 htmlCode[i] = "�" + i + ";"; 33 } 34 35 for (int i = 32; i < 128; i++) { 36 htmlCode[i] = String.valueOf((char)i); 37 } 38 39 htmlCode['\n'] = "<BR>\n"; 41 htmlCode['\"'] = """; htmlCode['&'] = "&"; htmlCode['<'] = "<"; htmlCode['>'] = ">"; 46 for (int i = 128; i < 256; i++) { 47 htmlCode[i] = "&#" + i + ";"; 48 } 49 } 50 51 58 public static String encode(String string) { 59 int n = string.length(); 60 char character; 61 StringBuffer buffer = new StringBuffer (); 62 for (int i = 0; i < n; i++) { 64 character = string.charAt(i); 65 try { 67 buffer.append(htmlCode[character]); 68 } 69 catch(ArrayIndexOutOfBoundsException aioobe) { 70 buffer.append(character); 71 } 72 } 73 return buffer.toString(); 74 } 75 76 } 77 78 | Popular Tags |