Java HTML Encode encodeHTML(String aText)

Here you can find the source of encodeHTML(String aText)

Description

encode HTML

License

Apache License

Declaration

public static String encodeHTML(String aText) 

Method Source Code

//package com.java2s;
/*// w w w  .  j a v a2 s  .  c  om
 * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    public static String encodeHTML(String aText) {
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(aText);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '<') {
                result.append("&lt;");
            } else if (character == '>') {
                result.append("&gt;");
            } else if (character == '&') {
                result.append("&amp;");
                //            } else if (character == '\"') {
                //                result.append("&quot;");
                //            } else if (character == '\t') {
                //                addCharEntity(9, result);
                //            } else if (character == '!') {
                //                addCharEntity(33, result);
                //            } else if (character == '#') {
                //                addCharEntity(35, result);
                //            } else if (character == '$') {
                //                addCharEntity(36, result);
                //            } else if (character == '%') {
                //                addCharEntity(37, result);
                //            } else if (character == '\'') {
                //                addCharEntity(39, result);
                //            } else if (character == '(') {
                //                addCharEntity(40, result);
                //            } else if (character == ')') {
                //                addCharEntity(41, result);
                //            } else if (character == '*') {
                //                addCharEntity(42, result);
                //            } else if (character == '+') {
                //                addCharEntity(43, result);
                //            } else if (character == ',') {
                //                addCharEntity(44, result);
                //            } else if (character == '-') {
                //                addCharEntity(45, result);
                //            } else if (character == '.') {
                //                addCharEntity(46, result);
                //            } else if (character == '/') {
                //                addCharEntity(47, result);
                //            } else if (character == ':') {
                //                addCharEntity(58, result);
                //            } else if (character == ';') {
                //                addCharEntity(59, result);
                //            } else if (character == '=') {
                //                addCharEntity(61, result);
                //            } else if (character == '?') {
                //                addCharEntity(63, result);
                //            } else if (character == '@') {
                //                addCharEntity(64, result);
                //            } else if (character == '[') {
                //                addCharEntity(91, result);
                //            } else if (character == '\\') {
                //                addCharEntity(92, result);
                //            } else if (character == ']') {
                //                addCharEntity(93, result);
                //            } else if (character == '^') {
                //                addCharEntity(94, result);
                //            } else if (character == '_') {
                //                addCharEntity(95, result);
                //            } else if (character == '`') {
                //                addCharEntity(96, result);
                //            } else if (character == '{') {
                //                addCharEntity(123, result);
                //            } else if (character == '|') {
                //                addCharEntity(124, result);
                //            } else if (character == '}') {
                //                addCharEntity(125, result);
                //            } else if (character == '~') {
                //                addCharEntity(126, result);
            } 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 s)
  2. forHTML(final String aText)
  3. forHTML(String aText)
  4. forHtmlComplete(final String aText)