package
net.java2000.tools;
import java.util.HashMap;
/** */ /**
* 替换HTMl里面的字符 e.g.: < > " å И 水
*
* @author 赵学庆
*/
public class HTMLDecoder ... {
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)
import java.util.HashMap;
/** */ /**
* 替换HTMl里面的字符 e.g.: < > " å И 水
*
* @author 赵学庆
*/
public class HTMLDecoder ... {
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)