public static String HTMLEncode(String text) {
if (text == null) return "";
char c;
int len = text.length();
StringBuffer results = new StringBuffer(len);
for (int i = 0; i < len; ++i) {
c = text.charAt(i);
switch (c) {
case '&':
results.append("&");
break;
case '<':
results.append("<");
break;
case '>':
results.append(">");
break;
case 39:
results.append("'");
break;
case '"':
results.append(""");
break;
case '¡':
results.append("¡");
break;
case '¤':
results.append("¤");
break;
case '¥':
results.append("¥");
break;
case '|':
results.append("¦");
break;
case '§':
results.append("§");
break;
case '¨':
results.append("¨");
break;
case '©':
results.append("©");
break;
case 'ª':
results.append("ª");
break;
case '«':
results.append("«");
break;
case '»':
results.append("»");
break;
case '€':
results.append("€");
break;
case '£':
results.append("£");
break;
case '':
results.append("­");
break;
case '®':
results.append("®");
break;
case '¯':
results.append("¯");
break;
case '°':
results.append("°");
break;
case '±':
results.append("±");
break;
case '¹':
results.append("¹");
break;
case '²':
results.append("²");
break;
case '³':
results.append("³");
break;
case '´':
results.append("´");
break;
case 'µ':
results.append("µ");
break;
case '¶':
results.append("¶");
break;
case '·':
results.append("·");
break;
case '¸':
results.append("¸");
break;
case 'º':
results.append("º");
break;
case '¿':
results.append("¿");
break;
case 'ñ':
results.append("ñ");
break;
case 'Ñ':
results.append("Ñ");
break;
case 'á':
results.append("á");
break;
case 'é':
results.append("é");
break;
case 'í':
results.append("í");
break;
case 'ó':
results.append("ó");
break;
case 'ú':
results.append("ú");
break;
case 'ü':
results.append("ü");
break;
case 'Á':
results.append("Á");
break;
case 'À':
results.append("À");
break;
case 'Ä':
results.append("Ä");
break;
case 'Â':
results.append("Â");
break;
case 'Å':
results.append("Å");
break;
case 'É':
results.append("É");
break;
case 'È':
results.append("È");
break;
case 'Ë':
results.append("Ë");
break;
case 'Ê':
results.append("Ê");
break;
case 'Í':
results.append("Í");
break;
case 'Ì':
results.append("Ì");
break;
case 'Ï':
results.append("Ï");
break;
case 'Î':
results.append("Î");
break;
case 'Ó':
results.append("Ó");
break;
case 'Ò':
results.append("Ò");
break;
case 'Ö':
results.append("Ö");
break;
case 'Ô':
results.append("Ô");
break;
case 'Ú':
results.append("Ú");
break;
case 'Ù':
results.append("Ù");
break;
case 'Ü':
results.append("Ü");
break;
case 'Û':
results.append("Û");
break;
case '½':
results.append("½");
break;
case '¾':
results.append("¾");
break;
case '¼':
results.append("¼");
break;
case 'Ç':
results.append("Ç");
break;
case 'ç':
results.append("ç");
break;
case 'ð':
results.append("ð");
break;
case '¢':
results.append("¢");
break;
case 'Þ':
results.append("Þ");
break;
case 'þ':
results.append("þ");
break;
case 'Ð':
results.append("Ð");
break;
case '×':
results.append("×");
break;
case '÷':
results.append("÷");
break;
case 'Æ':
results.append("Æ");
break;
/*
uml => chr 168, #umlaut (dieresis)
laquo => chr 171, #angle quotation mark, left
not => chr 172, #not sign
shy => chr 173, #soft hyphen
reg => chr 174, #registered sign
macr => chr 175, #macron
deg => chr 176, #degree sign
sup2 => chr 178, #superscript two
sup3 => chr 179, #superscript three
acute => chr 180, #acute accent
micro => chr 181, #micro sign
para => chr 182, #pilcrow (paragraph sign)
cedil => chr 184, #cedilla
sup1 => chr 185, #superscript one
raquo => chr 187, #angle quotation mark, right
iquest => chr 191, #inverted question mark
Auml => chr 196, #capital A, dieresis or umlaut mark
Aring => chr 197, #capital A, ring
Ccedil => chr 199, #capital C, cedilla
Egrave => chr 200, #capital E, grave accent
Eacute => chr 201, #capital E, acute accent
Ecirc => chr 202, #capital E, circumflex accent
Euml => chr 203, #capital E, dieresis or umlaut mark
Igrave => chr 204, #capital I, grave accent
Iacute => chr 205, #capital I, acute accent
Icirc => chr 206, #capital I, circumflex accent
Iuml => chr 207, #capital I, dieresis or umlaut mark
Ouml => chr 214, #capital O, dieresis or umlaut mark
Oslash => chr 216, #capital O, slash
Ugrave => chr 217, #capital U, grave accent
Uacute => chr 218, #capital U, acute accent
Ucirc => chr 219, #capital U, circumflex accent
Uuml => chr 220, #capital U, dieresis or umlaut mark
Yacute => chr 221, #capital Y, acute accent
szlig => chr 223, #small sharp s, German (sz ligature)
agrave => chr 224, #small a, grave accent
aacute => chr 225, #small a, acute accent
acirc => chr 226, #small a, circumflex accent
atilde => chr 227, #small a, tilde
auml => chr 228, #small a, dieresis or umlaut mark
aring => chr 229, #small a, ring
aelig => chr 230, #small ae diphthong (ligature)
ccedil => chr 231, #small c, cedilla
egrave => chr 232, #small e, grave accent
eacute => chr 233, #small e, acute accent
ecirc => chr 234, #small e, circumflex accent
euml => chr 235, #small e, dieresis or umlaut mark
igrave => chr 236, #small i, grave accent
iacute => chr 237, #small i, acute accent
icirc => chr 238, #small i, circumflex accent
iuml => chr 239, #small i, dieresis or umlaut mark
eth => chr 240, #small eth, Icelandic
ntilde => chr 241, #small n, tilde
ograve => chr 242, #small o, grave accent
oacute => chr 243, #small o, acute accent
ocirc => chr 244, #small o, circumflex accent
otilde => chr 245, #small o, tilde
ouml => chr 246, #small o, dieresis or umlaut mark
divide => chr 247, #divide sign
oslash => chr 248, #small o, slash
ugrave => chr 249, #small u, grave accent
uacute => chr 250, #small u, acute accent
ucirc => chr 251, #small u, circumflex accent
uuml => chr 252, #small u, dieresis or umlaut mark
yacute => chr 253, #small y, acute accent
thorn => chr 254, #small thorn, Icelandic
yuml => chr 255, #small y, dieresis or umlaut mark
<!ENTITY Atilde CDATA "Ã" -- latin capital letter A with tilde,
U+00C3 ISOlat1 -->
<!ENTITY Aring CDATA "Å" -- latin capital letter A with ring above
= latin capital letter A ring,
U+00C5 ISOlat1 -->
<!ENTITY AElig CDATA "Æ" -- latin capital letter AE
= latin capital ligature AE,
U+00C6 ISOlat1 -->
<!ENTITY Egrave CDATA "È" -- latin capital letter E with grave,
U+00C8 ISOlat1 -->
<!ENTITY Eacute CDATA "É" -- latin capital letter E with acute,
U+00C9 ISOlat1 -->
<!ENTITY Ecirc CDATA "Ê" -- latin capital letter E with circumflex,
U+00CA ISOlat1 -->
<!ENTITY Euml CDATA "Ë" -- latin capital letter E with diaeresis,
U+00CB ISOlat1 -->
<!ENTITY Igrave CDATA "Ì" -- latin capital letter I with grave,
U+00CC ISOlat1 -->
<!ENTITY Iacute CDATA "Í" -- latin capital letter I with acute,
U+00CD ISOlat1 -->
<!ENTITY Icirc CDATA "Î" -- latin capital letter I with circumflex,
U+00CE ISOlat1 -->
<!ENTITY Iuml CDATA "Ï" -- latin capital letter I with diaeresis,
U+00CF ISOlat1 -->
<!ENTITY ETH CDATA "Ð" -- latin capital letter ETH, U+00D0 ISOlat1 -->
<!ENTITY Ograve CDATA "Ò" -- latin capital letter O with grave,
U+00D2 ISOlat1 -->
<!ENTITY Oacute CDATA "Ó" -- latin capital letter O with acute,
U+00D3 ISOlat1 -->
<!ENTITY Ocirc CDATA "Ô" -- latin capital letter O with circumflex,
U+00D4 ISOlat1 -->
<!ENTITY Otilde CDATA "Õ" -- latin capital letter O with tilde,
U+00D5 ISOlat1 -->
<!ENTITY Ouml CDATA "Ö" -- latin capital letter O with diaeresis,
U+00D6 ISOlat1 -->
<!ENTITY times CDATA "×" -- multiplication sign, U+00D7 ISOnum -->
<!ENTITY Oslash CDATA "Ø" -- latin capital letter O with stroke
= latin capital letter O slash,
U+00D8 ISOlat1 -->
<!ENTITY Ugrave CDATA "Ù" -- latin capital letter U with grave,
U+00D9 ISOlat1 -->
<!ENTITY Uacute CDATA "Ú" -- latin capital letter U with acute,
U+00DA ISOlat1 -->
<!ENTITY Ucirc CDATA "Û" -- latin capital letter U with circumflex,
U+00DB ISOlat1 -->
<!ENTITY Uuml CDATA "Ü" -- latin capital letter U with diaeresis,
U+00DC ISOlat1 -->
<!ENTITY Yacute CDATA "Ý" -- latin capital letter Y with acute,
U+00DD ISOlat1 -->
<!ENTITY THORN CDATA "Þ" -- latin capital letter THORN,
U+00DE ISOlat1 -->
<!ENTITY szlig CDATA "ß" -- latin small letter sharp s = ess-zed,
U+00DF ISOlat1 -->
<!ENTITY agrave CDATA "à" -- latin small letter a with grave
= latin small letter a grave,
U+00E0 ISOlat1 -->
<!ENTITY aacute CDATA "á" -- latin small letter a with acute,
U+00E1 ISOlat1 -->
<!ENTITY acirc CDATA "â" -- latin small letter a with circumflex,
U+00E2 ISOlat1 -->
<!ENTITY atilde CDATA "ã" -- latin small letter a with tilde,
U+00E3 ISOlat1 -->
<!ENTITY auml CDATA "ä" -- latin small letter a with diaeresis,
U+00E4 ISOlat1 -->
<!ENTITY aring CDATA "å" -- latin small letter a with ring above
= latin small letter a ring,
U+00E5 ISOlat1 -->
<!ENTITY aelig CDATA "æ" -- latin small letter ae
= latin small ligature ae, U+00E6 ISOlat1 -->
<!ENTITY egrave CDATA "è" -- latin small letter e with grave,
U+00E8 ISOlat1 -->
<!ENTITY ecirc CDATA "ê" -- latin small letter e with circumflex,
U+00EA ISOlat1 -->
<!ENTITY euml CDATA "ë" -- latin small letter e with diaeresis,
U+00EB ISOlat1 -->
<!ENTITY igrave CDATA "ì" -- latin small letter i with grave,
U+00EC ISOlat1 -->
<!ENTITY iacute CDATA "í" -- latin small letter i with acute,
U+00ED ISOlat1 -->
<!ENTITY icirc CDATA "î" -- latin small letter i with circumflex,
U+00EE ISOlat1 -->
<!ENTITY iuml CDATA "ï" -- latin small letter i with diaeresis,
U+00EF ISOlat1 -->
<!ENTITY ograve CDATA "ò" -- latin small letter o with grave,
U+00F2 ISOlat1 -->
<!ENTITY oacute CDATA "ó" -- latin small letter o with acute,
U+00F3 ISOlat1 -->
<!ENTITY ocirc CDATA "ô" -- latin small letter o with circumflex,
U+00F4 ISOlat1 -->
<!ENTITY otilde CDATA "õ" -- latin small letter o with tilde,
U+00F5 ISOlat1 -->
<!ENTITY ouml CDATA "ö" -- latin small letter o with diaeresis,
U+00F6 ISOlat1 -->
<!ENTITY divide CDATA "÷" -- division sign, U+00F7 ISOnum -->
<!ENTITY oslash CDATA "ø" -- latin small letter o with stroke,
= latin small letter o slash,
U+00F8 ISOlat1 -->
<!ENTITY ugrave CDATA "ù" -- latin small letter u with grave,
U+00F9 ISOlat1 -->
<!ENTITY uacute CDATA "ú" -- latin small letter u with acute,
U+00FA ISOlat1 -->
<!ENTITY ucirc CDATA "û" -- latin small letter u with circumflex,
U+00FB ISOlat1 -->
<!ENTITY uuml CDATA "ü" -- latin small letter u with diaeresis,
U+00FC ISOlat1 -->
<!ENTITY yacute CDATA "ý" -- latin small letter y with acute,
U+00FD ISOlat1 -->
<!ENTITY thorn CDATA "þ" -- latin small letter thorn,
U+00FE ISOlat1 -->
<!ENTITY yuml CDATA "ÿ" -- latin small letter y with diaeresis,
U+00FF ISOlat1 -->
*/
default:
if (c<256)
results.append(c);
else
results.append("&#"+String.valueOf(c)+";");
} // end switch (c)
} // end for (i)
return results.toString();
} // HTMLEncode