benou | Code :
- public static String encodeHTML(String text) {
- if (text == null) return null;
- StringBuffer buffer = new StringBuffer();
- int i=0;
- char c;
- boolean firstSpace = true; // indique si un espace a déja été encodé
- while (i < text.length()) {
- c = text.charAt(i++);
- int idx = hasToBeEncoded(c);
- if (c==' ') {
- if (firstSpace) { // si c'est le 1er espace, on l'encode pas
- idx=-1;
- firstSpace = false;
- }
- } else
- firstSpace = true;;
- if (idx != -1) {
- buffer.append('&').append(codes[idx][0]).append(';');
- } else if ((int)c > 128) {
- buffer.append("" + (int)c + ";" );
- } else {
- buffer.append(c);
- }
- }
- return new String(buffer);
- }
- protected static int hasToBeEncoded(char c) {
- for (int i=0; i < codes.length; i++)
- if (codes[i][1].indexOf(c)!=-1)
- return i;
- return -1;
- }
- private static String[][] codes = {
- {"agrave", "à"},
- {"acirc", "â"},
- {"auml", "ä"},
- {"aelig", "æ"},
- {"ccedil", "ç"},
- {"egrave", "è"},
- {"eacute", "é"},
- {"ecirc", "ê"},
- {"euml", "ë"},
- {"icirc", "î"},
- {"iuml", "ï"},
- {"ocirc", "ô"},
- {"ouml", "ö"},
- {"ucirc", "û"},
- {"uuml", "ü"},
- {"gt", ">"},
- {"lt", "<"},
- {"times", "×"},
- {"divide", "÷"},
- {"nbsp", " "},
- {"copy", "©"},
- {"amp", "&"},
- {"quot", "\""},
- {"reg", "®"},
- {"micro", "µ"},
- {"sect", "§"},
- {"curren", "¤"},
- {"deg", "°"},
- };
|
remarque : tous les caractères spécieux ne sont pas indiqués dans cette liste pour des raisons d'efficacités. Dans le cas où il n'est aps présant, il va utiliser le format "code_unicode;" si tu souhaites avoir une gestion complète (enfin je crois) des caractères spéciaux sans utiliser le code unicode, tu peux utiliser cette liste là :
Code :
- private static String[][] codes = {
- {"nbsp", String.valueOf((char)160)},
-
- {"iexcl", String.valueOf((char) 161)},
- {"cent", String.valueOf((char) 162)},
- {"pound", String.valueOf((char) 163)},
- {"curren", String.valueOf((char) 164)},
- {"yen", String.valueOf((char) 165)},
- {"brvbar", String.valueOf((char) 166)},
-
- {"sect", String.valueOf((char) 167)},
- {"uml", String.valueOf((char) 168)},
-
- {"copy", String.valueOf((char) 169)},
- {"ordf", String.valueOf((char) 170)},
- {"laquo", String.valueOf((char) 171)},
-
- {"not", String.valueOf((char) 172)},
- {"shy", String.valueOf((char) 173)},
-
- {"reg", String.valueOf((char) 174)},
-
- {"macr", String.valueOf((char) 175)},
-
- {"deg", String.valueOf((char) 176)},
- {"plusmn", String.valueOf((char) 177)},
-
- {"sup2", String.valueOf((char) 178)},
-
- {"sup3", String.valueOf((char) 179)},
-
- {"acute", String.valueOf((char) 180)},
-
- {"micro", String.valueOf((char) 181)},
- {"para", String.valueOf((char) 182)},
-
- {"middot", String.valueOf((char) 183)},
-
- {"cedil", String.valueOf((char) 184)},
- {"sup1", String.valueOf((char) 185)},
-
- {"ordm", String.valueOf((char) 186)},
-
- {"raquo", String.valueOf((char) 187)},
-
- {"frac14", String.valueOf((char) 188)},
-
- {"frac12", String.valueOf((char) 189)},
-
- {"frac34", String.valueOf((char) 190)},
-
- {"iquest", String.valueOf((char) 191)},
-
- {"Agrave", String.valueOf((char) 192)},
-
-
- {"Aacute", String.valueOf((char) 193)},
-
- {"Acirc", String.valueOf((char) 194)},
-
- {"Atilde", String.valueOf((char) 195)},
-
- {"Auml", String.valueOf((char) 196)},
-
- {"Aring", String.valueOf((char) 197)},
-
-
- {"AElig", String.valueOf((char) 198)},
-
-
- {"Ccedil", String.valueOf((char) 199)},
-
- {"Egrave", String.valueOf((char) 200)},
-
- {"Eacute", String.valueOf((char) 201)},
-
- {"Ecirc", String.valueOf((char) 202)},
-
- {"Euml", String.valueOf((char) 203)},
-
- {"Igrave", String.valueOf((char) 204)},
-
- {"Iacute", String.valueOf((char) 205)},
-
- {"Icirc", String.valueOf((char) 206)},
-
- {"Iuml", String.valueOf((char) 207)},
-
- {"ETH", String.valueOf((char) 208)},
- {"Ntilde", String.valueOf((char) 209)},
-
- {"Ograve", String.valueOf((char) 210)},
-
- {"Oacute", String.valueOf((char) 211)},
-
- {"Ocirc", String.valueOf((char) 212)},
-
- {"Otilde", String.valueOf((char) 213)},
-
- {"Ouml", String.valueOf((char) 214)},
-
- {"times", String.valueOf((char) 215)},
- {"Oslash", String.valueOf((char) 216)},
-
-
- {"Ugrave", String.valueOf((char) 217)},
-
- {"Uacute", String.valueOf((char) 218)},
-
- {"Ucirc", String.valueOf((char) 219)},
-
- {"Uuml", String.valueOf((char) 220)},
-
- {"Yacute", String.valueOf((char) 221)},
-
- {"THORN", String.valueOf((char) 222)},
-
- {"szlig", String.valueOf((char) 223)},
-
- {"agrave", String.valueOf((char) 224)},
-
-
- {"aacute", String.valueOf((char) 225)},
-
- {"acirc", String.valueOf((char) 226)},
-
- {"atilde", String.valueOf((char) 227)},
-
- {"auml", String.valueOf((char) 228)},
-
- {"aring", String.valueOf((char) 229)},
-
-
- {"aelig", String.valueOf((char) 230)},
-
- {"ccedil", String.valueOf((char) 231)},
-
- {"egrave", String.valueOf((char) 232)},
-
- {"eacute", String.valueOf((char) 233)},
-
- {"ecirc", String.valueOf((char) 234)},
-
- {"euml", String.valueOf((char) 235)},
-
- {"igrave", String.valueOf((char) 236)},
-
- {"iacute", String.valueOf((char) 237)},
-
- {"icirc", String.valueOf((char) 238)},
-
- {"iuml", String.valueOf((char) 239)},
-
- {"eth", String.valueOf((char) 240)},
- {"ntilde", String.valueOf((char) 241)},
-
- {"ograve", String.valueOf((char) 242)},
-
- {"oacute", String.valueOf((char) 243)},
-
- {"ocirc", String.valueOf((char) 244)},
-
- {"otilde", String.valueOf((char) 245)},
-
- {"ouml", String.valueOf((char) 246)},
-
- {"divide", String.valueOf((char) 247)},
- {"oslash", String.valueOf((char) 248)},
-
-
- {"ugrave", String.valueOf((char) 249)},
-
- {"uacute", String.valueOf((char) 250)},
-
- {"ucirc", String.valueOf((char) 251)},
-
- {"uuml", String.valueOf((char) 252)},
-
- {"yacute", String.valueOf((char) 253)},
-
- {"thorn", String.valueOf((char) 254)},
-
- {"yuml", String.valueOf((char) 255)},
-
- {"Alpha", String.valueOf((char) 913)},
- {"Beta", String.valueOf((char) 914)},
- {"Gamma", String.valueOf((char) 915)},
-
- {"Delta", String.valueOf((char) 916)},
-
- {"Epsilon", String.valueOf((char) 917)},
- {"Zeta", String.valueOf((char) 918)},
- {"Eta", String.valueOf((char) 919)},
- {"Theta", String.valueOf((char) 920)},
-
- {"Iota", String.valueOf((char) 921)},
- {"Kappa", String.valueOf((char) 922)},
- {"Lambda", String.valueOf((char) 923)},
-
- {"Mu", String.valueOf((char) 924)},
- {"Nu", String.valueOf((char) 925)},
- {"Xi", String.valueOf((char) 926)},
- {"Omicron", String.valueOf((char) 927)},
- {"Pi", String.valueOf((char) 928)},
- {"Rho", String.valueOf((char) 929)},
- {"Sigma", String.valueOf((char) 931)},
-
- {"Tau", String.valueOf((char) 932)},
- {"Upsilon", String.valueOf((char) 933)},
-
- {"Phi", String.valueOf((char) 934)},
-
- {"Chi", String.valueOf((char) 935)},
- {"Psi", String.valueOf((char) 936)},
-
- {"Omega", String.valueOf((char) 937)},
-
- {"alpha", String.valueOf((char) 945)},
-
- {"beta", String.valueOf((char) 946)},
- {"gamma", String.valueOf((char) 947)},
-
- {"delta", String.valueOf((char) 948)},
-
- {"epsilon", String.valueOf((char) 949)},
-
- {"zeta", String.valueOf((char) 950)},
- {"eta", String.valueOf((char) 951)},
- {"theta", String.valueOf((char) 952)},
-
- {"iota", String.valueOf((char) 953)},
- {"kappa", String.valueOf((char) 954)},
-
- {"lambda", String.valueOf((char) 955)},
-
- {"mu", String.valueOf((char) 956)},
- {"nu", String.valueOf((char) 957)},
- {"xi", String.valueOf((char) 958)},
- {"omicron", String.valueOf((char) 959)},
- {"pi", String.valueOf((char) 960)},
- {"rho", String.valueOf((char) 961)},
- {"sigmaf", String.valueOf((char) 962)},
-
- {"sigma", String.valueOf((char) 963)},
-
- {"tau", String.valueOf((char) 964)},
- {"upsilon", String.valueOf((char) 965)},
-
- {"phi", String.valueOf((char) 966)},
- {"chi", String.valueOf((char) 967)},
- {"psi", String.valueOf((char) 968)},
- {"omega", String.valueOf((char) 969)},
-
- {"thetasym", String.valueOf((char) 977)},
-
- {"upsih", String.valueOf((char) 978)},
-
- {"piv", String.valueOf((char) 982)},
- {"bull", String.valueOf((char) 8226)},
-
- {"hellip", String.valueOf((char) 8230)},
-
- {"prime", String.valueOf((char) 8242)},
- {"Prime", String.valueOf((char) 8243)},
-
- {"oline", String.valueOf((char) 8254)},
-
- {"frasl", String.valueOf((char) 8260)},
- {"weierp", String.valueOf((char) 8472)},
-
- {"image", String.valueOf((char) 8465)},
-
- {"real", String.valueOf((char) 8476)},
-
- {"trade", String.valueOf((char) 8482)},
- {"alefsym", String.valueOf((char) 8501)},
-
- {"larr", String.valueOf((char) 8592)},
- {"uarr", String.valueOf((char) 8593)},
- {"rarr", String.valueOf((char) 8594)},
- {"darr", String.valueOf((char) 8595)},
- {"harr", String.valueOf((char) 8596)},
- {"crarr", String.valueOf((char) 8629)},
-
- {"lArr", String.valueOf((char) 8656)},
- {"uArr", String.valueOf((char) 8657)},
- {"rArr", String.valueOf((char) 8658)},
- {"dArr", String.valueOf((char) 8659)},
- {"hArr", String.valueOf((char) 8660)},
- {"forall", String.valueOf((char) 8704)},
- {"part", String.valueOf((char) 8706)},
- {"exist", String.valueOf((char) 8707)},
- {"empty", String.valueOf((char) 8709)},
-
- {"nabla", String.valueOf((char) 8711)},
-
- {"isin", String.valueOf((char) 8712)},
- {"notin", String.valueOf((char) 8713)},
- {"ni", String.valueOf((char) 8715)},
- {"prod", String.valueOf((char) 8719)},
-
- {"sum", String.valueOf((char) 8721)},
- {"minus", String.valueOf((char) 8722)},
- {"lowast", String.valueOf((char) 8727)},
- {"radic", String.valueOf((char) 8730)},
-
- {"prop", String.valueOf((char) 8733)},
- {"infin", String.valueOf((char) 8734)},
- {"ang", String.valueOf((char) 8736)},
- {"and", String.valueOf((char) 8743)},
- {"or", String.valueOf((char) 8744)},
- {"cap", String.valueOf((char) 8745)},
- {"cup", String.valueOf((char) 8746)},
- {"int", String.valueOf((char) 8747)},
- {"there4", String.valueOf((char) 8756)},
- {"sim", String.valueOf((char) 8764)},
-
- {"cong", String.valueOf((char) 8773)},
- {"asymp", String.valueOf((char) 8776)},
-
- {"ne", String.valueOf((char) 8800)},
- {"equiv", String.valueOf((char) 8801)},
- {"le", String.valueOf((char) 8804)},
- {"ge", String.valueOf((char) 8805)},
-
- {"sub", String.valueOf((char) 8834)},
- {"sup", String.valueOf((char) 8835)},
- {"nsub", String.valueOf((char) 8836)},
- {"sube", String.valueOf((char) 8838)},
- {"supe", String.valueOf((char) 8839)},
-
- {"oplus", String.valueOf((char) 8853)},
-
- {"otimes", String.valueOf((char) 8855)},
-
- {"perp", String.valueOf((char) 8869)},
-
- {"sdot", String.valueOf((char) 8901)},
- {"lceil", String.valueOf((char) 8968)},
-
- {"rceil", String.valueOf((char) 8969)},
- {"lfloor", String.valueOf((char) 8970)},
-
- {"rfloor", String.valueOf((char) 8971)},
- {"lang", String.valueOf((char) 9001)},
-
- {"rang", String.valueOf((char) 9002)},
-
- {"loz", String.valueOf((char) 9674)},
- {"spades", String.valueOf((char) 9824)},
- {"clubs", String.valueOf((char) 9827)},
-
- {"hearts", String.valueOf((char) 9829)},
-
- {"diams", String.valueOf((char) 9830)},
- {"quot", String.valueOf((char) 34)},
-
- {"amp", String.valueOf((char) 38)},
- {"lt", String.valueOf((char) 60)},
- {"gt", String.valueOf((char) 62)},
- {"OElig", String.valueOf((char) 338)},
-
- {"oelig", String.valueOf((char) 339)},
- {"Scaron", String.valueOf((char) 352)},
-
- {"scaron", String.valueOf((char) 353)},
-
- {"Yuml", String.valueOf((char) 376)},
-
- {"circ", String.valueOf((char) 710)},
-
- {"tilde", String.valueOf((char) 732)},
- {"ensp", String.valueOf((char) 8194)},
- {"emsp", String.valueOf((char) 8195)},
- {"thinsp", String.valueOf((char) 8201)},
- {"zwnj", String.valueOf((char) 8204)},
-
- {"zwj", String.valueOf((char) 8205)},
- {"lrm", String.valueOf((char) 8206)},
- {"rlm", String.valueOf((char) 8207)},
- {"ndash", String.valueOf((char) 8211)},
- {"mdash", String.valueOf((char) 8212)},
- {"lsquo", String.valueOf((char) 8216)},
-
- {"rsquo", String.valueOf((char) 8217)},
-
- {"sbquo", String.valueOf((char) 8218)},
- {"ldquo", String.valueOf((char) 8220)},
-
- {"rdquo", String.valueOf((char) 8221)},
-
- {"bdquo", String.valueOf((char) 8222)},
- {"dagger", String.valueOf((char) 8224)},
- {"Dagger", String.valueOf((char) 8225)},
- {"permil", String.valueOf((char) 8240)},
- {"lsaquo", String.valueOf((char) 8249)},
- {"rsaquo", String.valueOf((char) 8250)},
- {"euro", String.valueOf((char) 8364)}
- };
|
remarque : il y a moyen d'utiliser une Map plutot qu'un double tableau pour grandement optimiser la méthode hasToBeEncoded ! |