in jspwiki-util/src/main/java/org/apache/wiki/util/TextUtil.java [694:736]
public static String escapeHTMLEntities( final String buf ) {
final StringBuilder tmpBuf = new StringBuilder( buf.length() + 20 );
for( int i = 0; i < buf.length(); i++ ) {
final char ch = buf.charAt(i);
if( ch == '<' ) {
tmpBuf.append("<");
} else if( ch == '>' ) {
tmpBuf.append(">");
} else if( ch == '\"' ) {
tmpBuf.append(""");
} else if( ch == '&' ) {
// If the following is an XML entity reference (&#.*;) we'll leave it as it is; otherwise we'll replace it with an &
boolean isEntity = false;
final StringBuilder entityBuf = new StringBuilder();
if( i < buf.length() -1 ) {
for( int j = i; j < buf.length(); j++ ) {
final char ch2 = buf.charAt( j );
if( Character.isLetterOrDigit( ch2 ) || (ch2 == '#' && j == i+1) || ch2 == ';' || ch2 == '&' ) {
entityBuf.append(ch2);
if( ch2 == ';' ) {
isEntity = true;
break;
}
} else {
break;
}
}
}
if( isEntity ) {
tmpBuf.append( entityBuf );
i = i + entityBuf.length() - 1;
} else {
tmpBuf.append( "&" );
}
} else {
tmpBuf.append( ch );
}
}
return tmpBuf.toString();
}