1 16 package org.apache.myfaces.renderkit.html.util; 17 18 19 20 21 24 public abstract class HTMLEncoder 25 { 26 29 public static String encode (String string) 30 { 31 return encode(string, false, true); 32 } 33 34 37 public static String encode (String string, boolean encodeNewline) 38 { 39 return encode(string, encodeNewline, true); 40 } 41 42 48 public static String encode (String string, 49 boolean encodeNewline, 50 boolean encodeSubsequentBlanksToNbsp) 51 { 52 if (string == null) 53 { 54 return ""; 55 } 56 57 StringBuffer sb = null; String app; 59 char c; 60 for (int i = 0; i < string.length (); ++i) 61 { 62 app = null; 63 c = string.charAt(i); 64 switch (c) 65 { 66 case '"': app = """; break; case '&': app = "&"; break; case '<': app = "<"; break; case '>': app = ">"; break; case ' ': 71 if (encodeSubsequentBlanksToNbsp && 72 (i == 0 || (i - 1 >= 0 && string.charAt(i - 1) == ' '))) 73 { 74 app = " "; 76 } 77 break; 78 case '\n': 79 if (encodeNewline) 80 { 81 app = "<br/>"; 82 } 83 break; 84 85 case '\u00E4' : app = "ä"; break; 87 case '\u00C4' : app = "Ä"; break; 88 case '\u00F6' : app = "ö"; break; 89 case '\u00D6' : app = "Ö"; break; 90 case '\u00FC' : app = "ü"; break; 91 case '\u00DC' : app = "Ü"; break; 92 case '\u00DF' : app = "ß"; break; 93 94 case '\u20AC': app = "€"; break; 97 case '\u00AB': app = "«"; break; 98 case '\u00BB': app = "»"; break; 99 case '\u00A0': app = " "; break; 100 101 default: 102 if (((int)c) >= 0x80) 103 { 104 app = "&#" + ((int)c) + ";"; 106 } 107 break; 108 } 109 if (app != null) 110 { 111 if (sb == null) 112 { 113 sb = new StringBuffer (string.substring(0, i)); 114 } 115 sb.append(app); 116 } else { 117 if (sb != null) 118 { 119 sb.append(c); 120 } 121 } 122 } 123 124 if (sb == null) 125 { 126 return string; 127 } 128 else 129 { 130 return sb.toString(); 131 } 132 } 133 134 135 } 136 | Popular Tags |