001: package com.javujavu.javux.util.code;
002:
003: public class CodeHtml {
004: private static final String[] tags = { "\u00C6AElig",
005: "\u00C1Aacute", "\u00C2Acirc", "\u00C0Agrave",
006: "\u0391Alpha", "\u00C5Aring", "\u00C3Atilde", "\u00C4Auml",
007: "\u0392Beta", "\u00C7Ccedil", "\u03A7Chi", "\u2021Dagger",
008: "\u0394Delta", "\u00D0ETH", "\u00C9Eacute", "\u00CAEcirc",
009: "\u00C8Egrave", "\u0395Epsilon", "\u0397Eta", "\u00CBEuml",
010: "\u0393Gamma", "\u00CDIacute", "\u00CEIcirc",
011: "\u00CCIgrave", "\u0399Iota", "\u00CFIuml", "\u039AKappa",
012: "\u039BLambda", "\u039CMu", "\u00D1Ntilde", "\u039DNu",
013: "\u0152OElig", "\u00D3Oacute", "\u00D4Ocirc",
014: "\u00D2Ograve", "\u03A9Omega", "\u039FOmicron",
015: "\u00D8Oslash", "\u00D5Otilde", "\u00D6Ouml", "\u03A6Phi",
016: "\u03A0Pi", "\u2033Prime", "\u03A8Psi", "\u03A1Rho",
017: "\u0160Scaron", "\u03A3Sigma", "\u00DETHORN", "\u03A4Tau",
018: "\u0398Theta", "\u00DAUacute", "\u00DBUcirc",
019: "\u00D9Ugrave", "\u03A5Upsilon", "\u00DCUuml", "\u039EXi",
020: "\u00DDYacute", "\u0178Yuml", "\u0396Zeta", "\u00E1aacute",
021: "\u00E2acirc", "\u00B4acute", "\u00E6aelig",
022: "\u00E0agrave", "\u2135alefsym", "\u03B1alpha",
023: "\u0026amp", "\u2227and", "\u2220ang", "\u00E5aring",
024: "\u2248asymp", "\u00E3atilde", "\u00E4auml", "\u201Ebdquo",
025: "\u03B2beta", "\u00A6brvbar", "\u2022bull", "\u2229cap",
026: "\u00E7ccedil", "\u00B8cedil", "\u00A2cent", "\u03C7chi",
027: "\u02C6circ", "\u2663clubs", "\u2245cong", "\u00A9copy",
028: "\u21B5crarr", "\u222Acup", "\u00A4curren", "\u21D3dArr",
029: "\u2020dagger", "\u2193darr", "\u00B0deg", "\u03B4delta",
030: "\u2666diams", "\u00F7divide", "\u00E9eacute",
031: "\u00EAecirc", "\u00E8egrave", "\u2205empty", "\u2003emsp",
032: "\u2002ensp", "\u03B5epsilon", "\u2261equiv", "\u03B7eta",
033: "\u00F0eth", "\u00EBeuml", "\u20ACeuro", "\u2203exist",
034: "\u0192fnof", "\u2200forall", "\u00BDfrac12",
035: "\u00BCfrac14", "\u00BEfrac34", "\u2044frasl",
036: "\u03B3gamma", "\u2265ge", "\u003Egt", "\u21D4hArr",
037: "\u2194harr", "\u2665hearts", "\u2026hellip",
038: "\u00EDiacute", "\u00EEicirc", "\u00A1iexcl",
039: "\u00ECigrave", "\u2111image", "\u221Einfin", "\u222Bint",
040: "\u03B9iota", "\u00BFiquest", "\u2208isin", "\u00EFiuml",
041: "\u03BAkappa", "\u21D0lArr", "\u03BBlambda", "\u2329lang",
042: "\u00ABlaquo", "\u2190larr", "\u2308lceil", "\u201Cldquo",
043: "\u2264le", "\u230Alfloor", "\u2217lowast", "\u25CAloz",
044: "\u200Elrm", "\u2039lsaquo", "\u2018lsquo", "\u003Clt",
045: "\u00AFmacr", "\u2014mdash", "\u00B5micro", "\u00B7middot",
046: "\u2212minus", "\u03BCmu", "\u2207nabla", "\u00A0nbsp",
047: "\u2013ndash", "\u2260ne", "\u220Bni", "\u00ACnot",
048: "\u2209notin", "\u2284nsub", "\u00F1ntilde", "\u03BDnu",
049: "\u00F3oacute", "\u00F4ocirc", "\u0153oelig",
050: "\u00F2ograve", "\u203Eoline", "\u03C9omega",
051: "\u03BFomicron", "\u2295oplus", "\u2228or", "\u00AAordf",
052: "\u00BAordm", "\u00F8oslash", "\u00F5otilde",
053: "\u2297otimes", "\u00F6ouml", "\u00B6para", "\u2202part",
054: "\u2030permil", "\u22A5perp", "\u03C6phi", "\u03C0pi",
055: "\u03D6piv", "\u00B1plusmn", "\u00A3pound", "\u2032prime",
056: "\u220Fprod", "\u221Dprop", "\u03C8psi", "\"quot",
057: "\u21D2rArr", "\u221Aradic", "\u232Arang", "\u00BBraquo",
058: "\u2192rarr", "\u2309rceil", "\u201Drdquo", "\u211Creal",
059: "\u00AEreg", "\u230Brfloor", "\u03C1rho", "\u200Frlm",
060: "\u203Arsaquo", "\u2019rsquo", "\u201Asbquo",
061: "\u0161scaron", "\u22C5sdot", "\u00A7sect", "\u00ADshy",
062: "\u03C3sigma", "\u03C2sigmaf", "\u223Csim", "\u2660spades",
063: "\u2282sub", "\u2286sube", "\u2211sum", "\u2283sup",
064: "\u00B9sup1", "\u00B2sup2", "\u00B3sup3", "\u2287supe",
065: "\u00DFszlig", "\u03C4tau", "\u2234there4", "\u03B8theta",
066: "\u03D1thetasym", "\u2009thinsp", "\u00FEthorn",
067: "\u02DCtilde", "\u00D7times", "\u2122trade", "\u21D1uArr",
068: "\u00FAuacute", "\u2191uarr", "\u00FBucirc",
069: "\u00F9ugrave", "\u00A8uml", "\u03D2upsih",
070: "\u03C5upsilon", "\u00FCuuml", "\u2118weierp", "\u03BExi",
071: "\u00FDyacute", "\u00A5yen", "\u00FFyuml", "\u03B6zeta",
072: "\u200Dzwj", "\u200Czwnj" };
073:
074: public static String decode(String in) {
075: if (in.indexOf('&') == -1)
076: return in;
077: int len = in.length();
078: StringBuffer out = new StringBuffer(len);
079: char c;
080: int wasAmp = 0;
081: int ampPos = 0;
082: int ampVal = 0;
083: for (int i = 0; i < len; i++) {
084: c = in.charAt(i);
085: if (wasAmp == 0) {
086: if (c == '&') {
087: wasAmp = 100;
088: ampPos = i;
089: } else
090: out.append(c);
091: } else if (wasAmp == 100) {
092: if (c == '#')
093: wasAmp = 200;
094: else {
095: wasAmp = 5000;
096: for (int j = 0; j < tags.length; j++) {
097: if (c == tags[j].charAt(1)) {
098: wasAmp = 1000 + j;
099: ampVal = tags[j].charAt(0);
100: break;
101: }
102: }
103: }
104: } else if (wasAmp == 200) {
105: if (c >= '0' && c <= '9') {
106: wasAmp = 300;
107: ampVal = c - '0';
108: } else if (c == 'x' || c == 'X') {
109: wasAmp = 400;
110: ampVal = 0;
111: } else
112: wasAmp = 5000;
113: } else if (c == ';') {
114: out.append((char) ampVal);
115: wasAmp = 0;
116: } else if (wasAmp == 300) {
117: if (c >= '0' && c <= '9') {
118: ampVal = ampVal * 10 + c - '0';
119: } else
120: wasAmp = 5000;
121: } else if (wasAmp == 400) {
122: if (c >= '0' && c <= '9') {
123: ampVal = ampVal * 16 + c - '0';
124: } else if (c >= 'a' && c <= 'f') {
125: ampVal = ampVal * 16 + c - 'a' + 10;
126: }
127: if (c >= 'A' && c <= 'F') {
128: ampVal = ampVal * 16 + c - 'A' + 10;
129: } else
130: wasAmp = 5000;
131: } else {
132: if (c != tags[wasAmp - 1000].charAt(i - ampPos)) {
133: int j = wasAmp - 1000 + 1, k;
134: wasAmp = 5000;
135: for (; j < tags.length; j++) {
136: if (i - ampPos >= tags[j].length())
137: break;
138: for (k = ampPos + 1; k < i; k++) {
139: if (in.charAt(k) != tags[j].charAt(k
140: - ampPos)) {
141: break;
142: }
143: }
144: if (k < i)
145: break;
146: if (c == tags[j].charAt(i - ampPos)) {
147: wasAmp = 1000 + j;
148: ampVal = tags[j].charAt(0);
149: break;
150: }
151: }
152: }
153: }
154: if (wasAmp == 5000) {
155: out.append('&');
156: i = ampPos;
157: wasAmp = 0;
158: }
159: }
160: return out.toString();
161: }
162: }
|