EntityTable defaultEntityTable = EntityTable.getDefaultEntityTable();
Matcher matcher = Pattern.compile("\\&\\w+\\;").matcher(orig);
int delta = 0;
while (matcher.find()) {
String name = matcher.group().substring(0, matcher.group().length() - 1);
int code = defaultEntityTable.entityCode(name);
if (0 != code) {
String replacement = "&#" + code + ";";
buff.replace(matcher.start() - delta, matcher.end() - delta, replacement);
delta = delta + matcher.group().length() - replacement.length();
}