1 45 package org.ejbca.util; 46 47 import java.util.HashMap ; 48 import java.util.Map ; 49 50 public class HTMLTools { 51 static Object [][] entities = { 53 {"quot", new Integer (34)}, {"amp", new Integer (38)}, {"lt", new Integer (60)}, {"gt", new Integer (62)}, {"nbsp", new Integer (160)}, {"copy", new Integer (169)}, {"reg", new Integer (174)}, {"Agrave", new Integer (192)}, {"Aacute", new Integer (193)}, {"Acirc", new Integer (194)}, {"Atilde", new Integer (195)}, {"Auml", new Integer (196)}, {"Aring", new Integer (197)}, {"AElig", new Integer (198)}, {"Ccedil", new Integer (199)}, {"Egrave", new Integer (200)}, {"Eacute", new Integer (201)}, {"Ecirc", new Integer (202)}, {"Euml", new Integer (203)}, {"Igrave", new Integer (204)}, {"Iacute", new Integer (205)}, {"Icirc", new Integer (206)}, {"Iuml", new Integer (207)}, {"ETH", new Integer (208)}, {"Ntilde", new Integer (209)}, {"Ograve", new Integer (210)}, {"Oacute", new Integer (211)}, {"Ocirc", new Integer (212)}, {"Otilde", new Integer (213)}, {"Ouml", new Integer (214)}, {"Oslash", new Integer (216)}, {"Ugrave", new Integer (217)}, {"Uacute", new Integer (218)}, {"Ucirc", new Integer (219)}, {"Uuml", new Integer (220)}, {"Yacute", new Integer (221)}, {"THORN", new Integer (222)}, {"szlig", new Integer (223)}, {"agrave", new Integer (224)}, {"aacute", new Integer (225)}, {"acirc", new Integer (226)}, {"atilde", new Integer (227)}, {"auml", new Integer (228)}, {"aring", new Integer (229)}, {"aelig", new Integer (230)}, {"ccedil", new Integer (231)}, {"egrave", new Integer (232)}, {"eacute", new Integer (233)}, {"ecirc", new Integer (234)}, {"euml", new Integer (235)}, {"igrave", new Integer (236)}, {"iacute", new Integer (237)}, {"icirc", new Integer (238)}, {"iuml", new Integer (239)}, {"igrave", new Integer (236)}, {"iacute", new Integer (237)}, {"icirc", new Integer (238)}, {"iuml", new Integer (239)}, {"eth", new Integer (240)}, {"ntilde", new Integer (241)}, {"ograve", new Integer (242)}, {"oacute", new Integer (243)}, {"ocirc", new Integer (244)}, {"otilde", new Integer (245)}, {"ouml", new Integer (246)}, {"oslash", new Integer (248)}, {"ugrave", new Integer (249)}, {"uacute", new Integer (250)}, {"ucirc", new Integer (251)}, {"uuml", new Integer (252)}, {"yacute", new Integer (253)}, {"thorn", new Integer (254)}, {"yuml", new Integer (255)}, {"euro", new Integer (8364)}, }; 129 static Map e2i = new HashMap (); 130 static Map i2e = new HashMap (); 131 static { 132 for (int i=0; i<entities.length; ++i) { 133 e2i.put(entities[i][0], entities[i][1]); 134 i2e.put(entities[i][1], entities[i][0]); 135 } 136 } 137 138 144 public static String htmlescape(String s1) 145 { 146 if (s1 == null) return null; 147 StringBuffer buf = new StringBuffer (); 148 int i; 149 for (i=0; i<s1.length(); ++i) { 150 char ch = s1.charAt(i); 151 String entity = (String )i2e.get( new Integer (ch) ); 152 if (entity == null) { 153 if ((ch) > 128) { 154 buf.append("&#" + ((int)ch) + ";"); 155 } 156 else { 157 buf.append(ch); 158 } 159 } 160 else { 161 buf.append("&" + entity + ";"); 162 } 163 } 164 return buf.toString(); 165 } 166 167 178 public static String htmlunescape(String s1) { 179 if (s1 == null) return null; 180 StringBuffer buf = new StringBuffer (); 181 int i; 182 for (i=0; i<s1.length(); ++i) { 183 char ch = s1.charAt(i); 184 if (ch == '&') { 185 int semi = s1.indexOf(';', i+1); 186 if (semi == -1) { 187 buf.append(ch); 188 continue; 189 } 190 String entity = s1.substring(i+1, semi); 191 Integer iso; 192 if (entity.charAt(0) == '#') { 193 iso = new Integer (entity.substring(1)); 194 } 195 else { 196 iso = (Integer )e2i.get(entity); 197 } 198 if (iso == null) { 199 buf.append("&" + entity + ";"); 200 } 201 else { 202 buf.append((char)(iso.intValue())); 203 } 204 i = semi; 205 } 206 else { 207 buf.append(ch); 208 } 209 } 210 return buf.toString(); 211 } 212 } 213 | Popular Tags |