Here you can find the source of escapeHTMLTagCopy(String text)
Parameter | Description |
---|---|
text | input text to be escaped |
public static String escapeHTMLTagCopy(String text)
//package com.java2s; //License from project: BSD License import java.text.CharacterIterator; import java.text.StringCharacterIterator; public class Main { /**/*from w w w . ja v a 2s. co m*/ * Replace characters having special meaning inside HTML tags * with their escaped equivalents, using character entities such as '&'. * * The escaped characters are : * < > " ' \ & * * This method ensures that arbitrary text appearing inside a tag does not "confuse" * the tag. For example, HREF='Blah.do?Page=1&Sort=ASC' * does not comply with strict HTML because of the ampersand, and should be changed to * HREF='Blah.do?Page=1&Sort=ASC'. * This method always returns a new String * @param text input text to be escaped * @return result the escaped text */ public static String escapeHTMLTagCopy(String text) { final StringBuffer result = new StringBuffer(); final StringCharacterIterator iterator = new StringCharacterIterator(text); 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 { //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); } }