Java HTML Encode forHTMLTag(String aTagFragment)

Here you can find the source of forHTMLTag(String aTagFragment)

Description

Replace characters having special meaning inside HTML tags with their escaped equivalents, using character entities such as '&'.

License

Open Source License

Declaration

public static final String forHTMLTag(String aTagFragment) 

Method Source Code


//package com.java2s;
/*/*from w w w .jav a  2 s .  c  o  m*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Joel Freyss
 */

import java.text.*;

public class Main {
    /**
    * Replace characters having special meaning <em>inside</em> HTML tags
    * with their escaped equivalents, using character entities such as <tt>'&amp;'</tt>.
    *
    * <P>The escaped characters are :
    * <ul>
    * <li> <
    * <li> >
    * <li> "
    * <li> '
    * <li> \
    * <li> &
    * </ul>
    *
    * <P>This method ensures that arbitrary text appearing inside a tag does not "confuse"
    * the tag. For example, <tt>HREF='Blah.do?Page=1&Sort=ASC'</tt>
    * does not comply with strict HTML because of the ampersand, and should be changed to
    * <tt>HREF='Blah.do?Page=1&amp;Sort=ASC'</tt>. This is commonly seen in building
    * query strings. (In JSTL, the c:url tag performs this task automatically.)
    */
    public static final String forHTMLTag(String aTagFragment) {
        final StringBuffer result = new StringBuffer();

        final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '<') {
                result.append("&lt;");
            } else if (character == '>') {
                result.append("&gt;");
            } else if (character == '\"') {
                result.append("&quot;");
            } else if (character == '\'') {
                result.append("&#039;");
            } else if (character == '\\') {
                result.append("&#092;");
            } else if (character == '&') {
                result.append("&amp;");
            } else {
                //the char is not a special one
                //add it to the result as is
                result.append(character);
            }
            character = iterator.next();
        }
        return result.toString();
    }
}

Related

  1. encodeHTML(String aText)
  2. encodeHTML(String s)
  3. forHTML(final String aText)
  4. forHTML(String aText)
  5. forHtmlComplete(final String aText)
  6. forHTMLTag(String aTagFragment)
  7. html(String text)
  8. html(String text)
  9. html_encode(String input_string)