Java HTML Escape htmlEscape(String tag)

Here you can find the source of htmlEscape(String tag)

Description

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

License

Open Source License

Declaration


private static String htmlEscape(String tag) 

Method Source Code

//package com.java2s;
/*//  w w  w . j a v a 2 s  .c  om
 * Title        :  AlchemiXmlUtil.java
 * Package      :  org.gridbus.alchemi.client.util
 * Project      :  AlchemiJavaAPI
 * Description   :  
 * Created on   :  4/08/2005
 * Author      :  Krishna Nadiminti (kna@cs.mu.oz.au)
 * Copyright    :  (c) 2005, Grid Computing and Distributed Systems Laboratory, 
 *                   Dept. of Computer Science and Software Engineering,
 *                   University of Melbourne, Australia.
 * 
 * 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 2 of the License, or (at your option) any later  version.
 * See the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)for more details.
 * 
 */

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.)
     */
    //adapted from: http://www.javapractices.com/Topic96.cjp
    private static String htmlEscape(String tag) {
        final StringBuffer result = new StringBuffer();
        if (tag == null) {
            return null;
        } else {
            for (int i = 0; i < tag.length(); i++) {
                char character = tag.charAt(i);
                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);
                }
            }
            //return Base64.encodeBytes(result.toString().getBytes());
            return result.toString();
        }
    }
}

Related

  1. htmlEscape(String source)
  2. htmlEscape(String str)
  3. htmlEscape(String str)
  4. htmlEscape(String str)
  5. htmlEscape(String string)
  6. htmlEscape(String text)
  7. htmlescapeAll(String s1)
  8. htmlEscapeBasicMarkup(final String text)
  9. HTMLEscapeBr(String original)