Here you can find the source of forHTML(String aText)
public static String forHTML(String aText)
//package com.java2s; /*/*from w w w .ja va2 s .c o m*/ * Copyright (C) 2008 Tolven Inc * * This library is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * Contact: info@tolvenhealth.com */ import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class Main { /** * Escape characters for text appearing in HTML markup. * * <P>The following characters are replaced with corresponding HTML * character entities : * <table border='1' cellpadding='3' cellspacing='0'> * <tr><th> Character </th><th> Encoding </th></tr> * <tr><td> < </td><td> < </td></tr> * <tr><td> > </td><td> > </td></tr> * <tr><td> & </td><td> & </td></tr> * <tr><td> " </td><td> "</td></tr> * <tr><td> ' </td><td> '</td></tr> * <tr><td> ( </td><td> (</td></tr> * <tr><td> ) </td><td> )</td></tr> * <tr><td> # </td><td> #</td></tr> * <tr><td> % </td><td> %</td></tr> * <tr><td> ; </td><td> ;</td></tr> * <tr><td> + </td><td> + </td></tr> * <tr><td> - </td><td> - </td></tr> * </table> * */ public static String forHTML(String aText) { if (aText == null) return null; final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '&') { result.append("&"); } else if (character == '\"') { result.append("""); } else if (character == '\'') { result.append("'"); } else if (character == '(') { result.append("("); } else if (character == ')') { result.append(")"); } else if (character == '#') { result.append("#"); } else if (character == '%') { result.append("%"); } else if (character == ';') { result.append(";"); } else if (character == '+') { result.append("+"); } else if (character == '-') { result.append("-"); } else { //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); } }