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

Parameter

Parameter Description
aTagFragment some HTML to be escaped

Return

escaped HTML

Declaration

private static String forHTMLTag(String aTagFragment) 

Method Source Code

//package com.java2s;
/*//  w  w w .  j a  v  a2s.  c om
 * RHQ Management Platform
 * Copyright (C) 2005-2012 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation, and/or the GNU Lesser
 * General Public License, version 2.1, also as published by the Free
 * Software Foundation.
 *
 * 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 and the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

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.)
     * 
     * forHTMLTag is copy-n-pasted from: http://www.javapractices.com/Topic96.cjp 
     * used to be in our util.StringUtil, we should really use jakarta's <code>StringEscapeUtils.escapeHTML()</code> 
     * method, however, at this time we do not want to pull in the entire 
     * Commons Lang API dependency for just one method.
     * 
     * @param aTagFragment
     *           some HTML to be escaped
     * @return escaped HTML
     */
    private static String forHTMLTag(String aTagFragment) {
        final StringBuffer result = new StringBuffer();

        final StringCharacterIterator iterator = new StringCharacterIterator(
                aTagFragment);

        for (char character = iterator.current(); character != CharacterIterator.DONE; character = iterator
                .next()) {
            switch (character) {
            case '<':
                result.append("&lt;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '\"':
                result.append("&quot;");
                break;
            case '\'':
                result.append("&#039;");
                break;
            case '\\':
                result.append("&#092;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '|':
                result.append("&#124;");
                break;
            case ',':
                result.append("&#44;");
                break;
            default:
                // the char is not a special one add it to the result as is
                result.append(character);
                break;
            }
        }
        return result.toString();
    }
}

Related

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