Java HTML Escape escapeHTML(String text)

Here you can find the source of escapeHTML(String text)

Description

escape HTML

License

Open Source License

Declaration

public static String escapeHTML(String text) 

Method Source Code

//package com.java2s;
/* Copyright Panopto 2009 - 2011
 * // ww w .  j a v  a  2s .  c om
 * This file is part of the Panopto plugin for Blackboard.
 * 
 * The Panopto plugin for Blackboard 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.
 *
 * The Panopto plugin for Blackboard 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 the Panopto plugin for Blackboard.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static String escapeHTML(String text) {
        StringBuilder escaped = new StringBuilder();
        StringCharacterIterator iterator = new StringCharacterIterator(text);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '<') {
                escaped.append("&lt;");
            } else if (character == '>') {
                escaped.append("&gt;");
            } else if (character == '&') {
                escaped.append("&amp;");
            } else if (character == '\"') {
                escaped.append("&quot;");
            } else if (character == '\'') {
                escaped.append("&#039;");
            } else if (character == '(') {
                escaped.append("&#040;");
            } else if (character == ')') {
                escaped.append("&#041;");
            } else if (character == '#') {
                escaped.append("&#035;");
            } else if (character == '%') {
                escaped.append("&#037;");
            } else if (character == ';') {
                escaped.append("&#059;");
            } else if (character == '+') {
                escaped.append("&#043;");
            } else if (character == '-') {
                escaped.append("&#045;");
            } else {
                escaped.append(character);
            }

            character = iterator.next();
        }

        return escaped.toString();
    }
}

Related

  1. escapeHtml(String s)
  2. escapeHTML(String s)
  3. escapeHTML(String s)
  4. escapeHtml(String s)
  5. escapeHTML(String text)
  6. escapeHTMLLine(String line)
  7. escapeHTMLSpecialChars(String aText)
  8. escapeHTMLTagCopy(String text)
  9. htmlEscape(final String text)