/**
* Decodes html entities.
* @param s the String to decode
* @return the newly decoded String
*/
public static String htmlEntityDecode(String s) {
System.out.println("Asked to decode: " + s);
int i = 0, j = 0, pos = 0;
StringBuffer sb = new StringBuffer();
for ( pos = 0 ; pos < s.length() ; pos++ ) {
System.out.println("Char at " + pos + " = " + s.charAt(pos));
if ( s.charAt(pos) == '&' && s.charAt(pos + 1) == '#' && s.charAt(pos + 4) == ';' ) {
// get pos+2 and pos+3, and sort them out.
int n = -1;
for (int q = 2; q <= 3; q++ ) {
char c = s.charAt(pos + q);
System.out.println("Processing " + c);
if ('0' <= c && c <= '9')
n = (n == -1 ? 0 : n * 10) + c - '0';
else {
System.out.println("breaking");
break;
}
}
// Should have n here.
if (n != -1) {
sb.append((char)n);
pos = pos + 4;
}
} else {
// Add it onto the string.
sb.append(s.charAt(pos));
}
}
System.out.println("SB = " + sb.toString());
/* Original stuff from Koders that didn't work with: Le jus d'une orange
while ((i = s.indexOf("", pos)) != -1 && (j = s.indexOf(';', i)) != -1) {
int n = -1;
for (i += 2; i < j; ++i) {
char c = s.charAt(i);
if ('0' <= c && c <= '9')
n = (n == -1 ? 0 : n * 10) + c - '0';
else {
System.out.println("breaking");
break;
}
}
if (i != j) n = -1; // malformed entity - abort
if (n != -1) {
sb.append((char)n);
System.out.println("sb.append = " + (char) n);
i = j + 1; // skip ';'
} else {
for (int k = pos; k < i; ++k) {
System.out.println("sb.append = " + s.charAt(k));
sb.append(s.charAt(k));
}
}
pos = i;
}
if (sb.length() == 0)
return s;
else
sb.append(s.substring(pos, s.length()));
System.out.println("Returning: " + sb.toString());
*/
return sb.toString();
}