html特殊字符标签 转回 特殊字符

时间:2022-03-30 08:58:32

public static final HashMap<String, Character> chartable;


public static String decode(String s) {

String t;

Character ch;

int tmppos, i;


int maxpos = s.length();

StringBuffer sb = new StringBuffer(maxpos);

int curpos = 0;

while (curpos < maxpos) {

char c = s.charAt(curpos++);

if (c == '&') {

tmppos = curpos;

if (tmppos < maxpos) {

char d = s.charAt(tmppos++);

if (d == '#') {

if (tmppos < maxpos) {

d = s.charAt(tmppos++);

if ((d == 'x') || (d == 'x')) {

if (tmppos < maxpos) {

d = s.charAt(tmppos++);

if (ishexdigit(d)) {

while (tmppos < maxpos) {

d = s.charAt(tmppos++);

if (!ishexdigit(d)) {

if (d == ';') {

t = s.substring(curpos + 2,

tmppos - 1);

try {

i = Integer.parseInt(t,

16);

if ((i >= 0)

&& (i < 65536)) {

c = (char) i;

curpos = tmppos;

}

} catch (NumberFormatException e) {

}

}

break;

}

}

}

}

} else if (isdigit(d)) {

while (tmppos < maxpos) {

d = s.charAt(tmppos++);

if (!isdigit(d)) {

if (d == ';') {

t = s.substring(curpos + 1,

tmppos - 1);

try {

i = Integer.parseInt(t);

if ((i >= 0) && (i < 65536)) {

c = (char) i;

curpos = tmppos;

}

} catch (NumberFormatException e) {

}

}

break;

}

}

}

}

} else if (isletter(d)) {

while (tmppos < maxpos) {

d = s.charAt(tmppos++);

if (!isletterordigit(d)) {

if (d == ';') {

t = s.substring(curpos, tmppos - 1);

ch = (Character) chartable.get(t);

if (ch != null) {

c = ch.charValue();

curpos = tmppos;

}

}

break;

}

}

}

}

}

sb.append(c);

}

return sb.toString();

}


private static boolean isletterordigit(char c) {

return isletter(c) || isdigit(c);

}


private static boolean ishexdigit(char c) {

return ishexletter(c) || isdigit(c);

}


private static boolean isletter(char c) {

return ((c >= 'a') && (c <= 'z')) || ((c >= 'a') && (c <= 'z'));

}


private static boolean ishexletter(char c) {

return ((c >= 'a') && (c <= 'f')) || ((c >= 'a') && (c <= 'f'));

}


private static boolean isdigit(char c) {

return (c >= '0') && (c <= '9');

}


public static String compact(String s) {

int maxpos = s.length();

StringBuffer sb = new StringBuffer(maxpos);

int curpos = 0;

while (curpos < maxpos) {

char c = s.charAt(curpos++);

if (isWhitespace(c)) {

while ((curpos < maxpos) && isWhitespace(s.charAt(curpos))) {

curpos++;

}

c = '\u0020';

}

sb.append(c);

}

return sb.toString();

}


public static boolean isWhitespace(char ch) {

return (ch == '\u0020') || (ch == '\r') || (ch == '\n')

|| (ch == '\u0009') || (ch == '\u000c') || (ch == '\u200b');

}


static {

chartable = new HashMap<String, Character>();

chartable.put("quot", new Character((char) 34));

chartable.put("amp", new Character((char) 38));

chartable.put("apos", new Character((char) 39));

chartable.put("lt", new Character((char) 60));

chartable.put("gt", new Character((char) 62));

chartable.put("nbsp", new Character((char) 160));

chartable.put("iexcl", new Character((char) 161));

chartable.put("cent", new Character((char) 162));

chartable.put("pound", new Character((char) 163));

chartable.put("curren", new Character((char) 164));

chartable.put("yen", new Character((char) 165));

chartable.put("brvbar", new Character((char) 166));

chartable.put("sect", new Character((char) 167));

chartable.put("uml", new Character((char) 168));

chartable.put("copy", new Character((char) 169));

chartable.put("ordf", new Character((char) 170));

chartable.put("laquo", new Character((char) 171));

chartable.put("not", new Character((char) 172));

chartable.put("shy", new Character((char) 173));

chartable.put("reg", new Character((char) 174));

chartable.put("macr", new Character((char) 175));

chartable.put("deg", new Character((char) 176));

chartable.put("plusmn", new Character((char) 177));

chartable.put("sup2", new Character((char) 178));

chartable.put("sup3", new Character((char) 179));

chartable.put("acute", new Character((char) 180));

chartable.put("micro", new Character((char) 181));

chartable.put("para", new Character((char) 182));

chartable.put("middot", new Character((char) 183));

chartable.put("cedil", new Character((char) 184));

chartable.put("sup1", new Character((char) 185));

chartable.put("ordm", new Character((char) 186));

chartable.put("raquo", new Character((char) 187));

chartable.put("frac14", new Character((char) 188));

chartable.put("frac12", new Character((char) 189));

chartable.put("frac34", new Character((char) 190));

chartable.put("iquest", new Character((char) 191));

chartable.put("Agrave", new Character((char) 192));

chartable.put("Aacute", new Character((char) 193));

chartable.put("Acirc", new Character((char) 194));

chartable.put("Atilde", new Character((char) 195));

chartable.put("Auml", new Character((char) 196));

chartable.put("Aring", new Character((char) 197));

chartable.put("AElig", new Character((char) 198));

chartable.put("Ccedil", new Character((char) 199));

chartable.put("Egrave", new Character((char) 200));

chartable.put("Eacute", new Character((char) 201));

chartable.put("Ecirc", new Character((char) 202));

chartable.put("Euml", new Character((char) 203));

chartable.put("Igrave", new Character((char) 204));

chartable.put("Iacute", new Character((char) 205));

chartable.put("Icirc", new Character((char) 206));

chartable.put("Iuml", new Character((char) 207));

chartable.put("ETH", new Character((char) 208));

chartable.put("Ntilde", new Character((char) 209));

chartable.put("Ograve", new Character((char) 210));

chartable.put("Oacute", new Character((char) 211));

chartable.put("Ocirc", new Character((char) 212));

chartable.put("Otilde", new Character((char) 213));

chartable.put("Ouml", new Character((char) 214));

chartable.put("times", new Character((char) 215));

chartable.put("Oslash", new Character((char) 216));

chartable.put("Ugrave", new Character((char) 217));

chartable.put("Uacute", new Character((char) 218));

chartable.put("Ucirc", new Character((char) 219));

chartable.put("Uuml", new Character((char) 220));

chartable.put("Yacute", new Character((char) 221));

chartable.put("THORN", new Character((char) 222));

chartable.put("szlig", new Character((char) 223));

chartable.put("agrave", new Character((char) 224));

chartable.put("aacute", new Character((char) 225));

chartable.put("acirc", new Character((char) 226));

chartable.put("atilde", new Character((char) 227));

chartable.put("auml", new Character((char) 228));

chartable.put("aring", new Character((char) 229));

chartable.put("aelig", new Character((char) 230));

chartable.put("ccedil", new Character((char) 231));

chartable.put("egrave", new Character((char) 232));

chartable.put("eacute", new Character((char) 233));

chartable.put("ecirc", new Character((char) 234));

chartable.put("euml", new Character((char) 235));

chartable.put("igrave", new Character((char) 236));

chartable.put("iacute", new Character((char) 237));

chartable.put("icirc", new Character((char) 238));

chartable.put("iuml", new Character((char) 239));

chartable.put("eth", new Character((char) 240));

chartable.put("ntilde", new Character((char) 241));

chartable.put("ograve", new Character((char) 242));

chartable.put("oacute", new Character((char) 243));

chartable.put("ocirc", new Character((char) 244));

chartable.put("otilde", new Character((char) 245));

chartable.put("ouml", new Character((char) 246));

chartable.put("divide", new Character((char) 247));

chartable.put("oslash", new Character((char) 248));

chartable.put("ugrave", new Character((char) 249));

chartable.put("uacute", new Character((char) 250));

chartable.put("ucirc", new Character((char) 251));

chartable.put("uuml", new Character((char) 252));

chartable.put("yacute", new Character((char) 253));

chartable.put("thorn", new Character((char) 254));

chartable.put("yuml", new Character((char) 255));

chartable.put("OElig", new Character((char) 338));

chartable.put("oelig", new Character((char) 339));

chartable.put("Scaron", new Character((char) 352));

chartable.put("scaron", new Character((char) 353));

chartable.put("fnof", new Character((char) 402));

chartable.put("circ", new Character((char) 710));

chartable.put("tilde", new Character((char) 732));

chartable.put("Alpha", new Character((char) 913));

chartable.put("Beta", new Character((char) 914));

chartable.put("Gamma", new Character((char) 915));

chartable.put("Delta", new Character((char) 916));

chartable.put("Epsilon", new Character((char) 917));

chartable.put("Zeta", new Character((char) 918));

chartable.put("Eta", new Character((char) 919));

chartable.put("Theta", new Character((char) 920));

chartable.put("Iota", new Character((char) 921));

chartable.put("Kappa", new Character((char) 922));

chartable.put("Lambda", new Character((char) 923));

chartable.put("Mu", new Character((char) 924));

chartable.put("Nu", new Character((char) 925));

chartable.put("Xi", new Character((char) 926));

chartable.put("Omicron", new Character((char) 927));

chartable.put("Pi", new Character((char) 928));

chartable.put("Rho", new Character((char) 929));

chartable.put("Sigma", new Character((char) 931));

chartable.put("Tau", new Character((char) 932));

chartable.put("Upsilon", new Character((char) 933));

chartable.put("Phi", new Character((char) 934));

chartable.put("Chi", new Character((char) 935));

chartable.put("Psi", new Character((char) 936));

chartable.put("Omega", new Character((char) 937));

chartable.put("alpha", new Character((char) 945));

chartable.put("beta", new Character((char) 946));

chartable.put("gamma", new Character((char) 947));

chartable.put("delta", new Character((char) 948));

chartable.put("epsilon", new Character((char) 949));

chartable.put("zeta", new Character((char) 950));

chartable.put("eta", new Character((char) 951));

chartable.put("theta", new Character((char) 952));

chartable.put("iota", new Character((char) 953));

chartable.put("kappa", new Character((char) 954));

chartable.put("lambda", new Character((char) 955));

chartable.put("mu", new Character((char) 956));

chartable.put("nu", new Character((char) 957));

chartable.put("xi", new Character((char) 958));

chartable.put("omicron", new Character((char) 959));

chartable.put("pi", new Character((char) 960));

chartable.put("rho", new Character((char) 961));

chartable.put("sigmaf", new Character((char) 962));

chartable.put("sigma", new Character((char) 963));

chartable.put("tau", new Character((char) 964));

chartable.put("upsilon", new Character((char) 965));

chartable.put("phi", new Character((char) 966));

chartable.put("chi", new Character((char) 967));

chartable.put("psi", new Character((char) 968));

chartable.put("omega", new Character((char) 969));

chartable.put("thetasym", new Character((char) 977));

chartable.put("upsih", new Character((char) 978));

chartable.put("piv", new Character((char) 982));

chartable.put("ensp", new Character((char) 8194));

chartable.put("emsp", new Character((char) 8195));

chartable.put("thinsp", new Character((char) 8201));

chartable.put("zwnj", new Character((char) 8204));

chartable.put("zwj", new Character((char) 8205));

chartable.put("lrm", new Character((char) 8206));

chartable.put("rlm", new Character((char) 8207));

chartable.put("ndash", new Character((char) 8211));

chartable.put("mdash", new Character((char) 8212));

chartable.put("lsquo", new Character((char) 8216));

chartable.put("rsquo", new Character((char) 8217));

chartable.put("sbquo", new Character((char) 8218));

chartable.put("ldquo", new Character((char) 8220));

chartable.put("rdquo", new Character((char) 8221));

chartable.put("bdquo", new Character((char) 8222));

chartable.put("dagger", new Character((char) 8224));

chartable.put("Dagger", new Character((char) 8225));

chartable.put("bull", new Character((char) 8226));

chartable.put("hellip", new Character((char) 8230));

chartable.put("permil", new Character((char) 8240));

chartable.put("prime", new Character((char) 8242));

chartable.put("Prime", new Character((char) 8243));

chartable.put("lsaquo", new Character((char) 8249));

chartable.put("rsaquo", new Character((char) 8250));

chartable.put("oline", new Character((char) 8254));

chartable.put("frasl", new Character((char) 8260));

chartable.put("euro", new Character((char) 8364));

chartable.put("image", new Character((char) 8465));

chartable.put("weierp", new Character((char) 8472));

chartable.put("real", new Character((char) 8476));

chartable.put("trade", new Character((char) 8482));

chartable.put("alefsym", new Character((char) 8501));

chartable.put("larr", new Character((char) 8592));

chartable.put("uarr", new Character((char) 8593));

chartable.put("rarr", new Character((char) 8594));

chartable.put("darr", new Character((char) 8595));

chartable.put("harr", new Character((char) 8596));

chartable.put("crarr", new Character((char) 8629));

chartable.put("lArr", new Character((char) 8656));

chartable.put("uArr", new Character((char) 8657));

chartable.put("rArr", new Character((char) 8658));

chartable.put("dArr", new Character((char) 8659));

chartable.put("hArr", new Character((char) 8660));

chartable.put("forall", new Character((char) 8704));

chartable.put("part", new Character((char) 8706));

chartable.put("exist", new Character((char) 8707));

chartable.put("empty", new Character((char) 8709));

chartable.put("nabla", new Character((char) 8711));

chartable.put("isin", new Character((char) 8712));

chartable.put("notin", new Character((char) 8713));

chartable.put("ni", new Character((char) 8715));

chartable.put("prod", new Character((char) 8719));

chartable.put("sum", new Character((char) 8721));

chartable.put("minus", new Character((char) 8722));

chartable.put("lowast", new Character((char) 8727));

chartable.put("radic", new Character((char) 8730));

chartable.put("prop", new Character((char) 8733));

chartable.put("infin", new Character((char) 8734));

chartable.put("ang", new Character((char) 8736));

chartable.put("and", new Character((char) 8743));

chartable.put("or", new Character((char) 8744));

chartable.put("cap", new Character((char) 8745));

chartable.put("cup", new Character((char) 8746));

chartable.put("int", new Character((char) 8747));

chartable.put("there4", new Character((char) 8756));

chartable.put("sim", new Character((char) 8764));

chartable.put("cong", new Character((char) 8773));

chartable.put("asymp", new Character((char) 8776));

chartable.put("ne", new Character((char) 8800));

chartable.put("equiv", new Character((char) 8801));

chartable.put("le", new Character((char) 8804));

chartable.put("ge", new Character((char) 8805));

chartable.put("sub", new Character((char) 8834));

chartable.put("sup", new Character((char) 8835));

chartable.put("nsub", new Character((char) 8836));

chartable.put("sube", new Character((char) 8838));

chartable.put("supe", new Character((char) 8839));

chartable.put("oplus", new Character((char) 8853));

chartable.put("otimes", new Character((char) 8855));

chartable.put("perp", new Character((char) 8869));

chartable.put("sdot", new Character((char) 8901));

chartable.put("lceil", new Character((char) 8968));

chartable.put("rceil", new Character((char) 8969));

chartable.put("lfloor", new Character((char) 8970));

chartable.put("rfloor", new Character((char) 8971));

chartable.put("lang", new Character((char) 9001));

chartable.put("rang", new Character((char) 9002));

chartable.put("loz", new Character((char) 9674));

chartable.put("spades", new Character((char) 9824));

chartable.put("clubs", new Character((char) 9827));

chartable.put("hearts", new Character((char) 9829));

chartable.put("diams", new Character((char) 9830));

}