public class HtmlUtils {
/**
* Escapes given text for placing it in the HTML body. If
* you need escaping for placing text in an attribut value,
* you should remove the escaping for the "\n" character.
*
* (c) Svetlin Nakov, 2004 - http://www.nakov.com
*/
public static String htmlEscape(String aText) {
if (aText == null) {
return "";
}
StringBuffer escapedText = new StringBuffer();
for (int i=0; i<aText.length(); i++) {
char ch = aText.charAt(i);
if (ch == '\'')
escapedText.append("'");
else if (ch == '\"')
escapedText.append(""");
else if (ch == '<')
escapedText.append("<");
else if (ch == '>')
escapedText.append(">");
else if (ch == '&')
escapedText.append("&");
else if (ch == '\n')
escapedText.append("<br>\n");
else if (ch == ' ')
escapedText.append(" ");
else if (ch == '\t')
escapedText.append(" ");
else
escapedText.append(ch);
}
String result = escapedText.toString();
return result;
}
}
Back to Internet
Programming with Java books's web site