Java HTML Encode textToHTML(String text)

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

Description

Converts a text string to HTML or XML entities.

License

Apache License

Parameter

Parameter Description
text The string to convert.

Return

The converted string.

Declaration

public static final String textToHTML(String text) 

Method Source Code

//package com.java2s;
/*// w w w  .java  2s. c o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * 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.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

public class Main {
    /**
     * Converts a text string to HTML or XML entities.
     *
     * @author Erik C. Thauvin
     * @param text The string to convert.
     * @param xml Convert the string to XML if set to true.
     *
     * @return The converted string.
     */
    public static final String textToHTML(String text, boolean xml) {
        if (text == null)
            return "null";
        final StringBuffer html = new StringBuffer();

        // Loop thru each characters of the text
        for (int i = 0; i < text.length(); i++) {
            // Convert character to HTML/XML
            html.append(charToHTML(text.charAt(i), xml));
        }

        // Return HTML/XML string
        return html.toString();
    }

    /**
     * Converts a text string to HTML or XML entities.
     *
     * @param text The string to convert.
     * @author Erik C. Thauvin
     * @return The converted string.
     */
    public static final String textToHTML(String text) {
        return textToHTML(text, false);
    }

    /**
     * Converts a character to HTML or XML entity.
     *
     * @param ch The character to convert.
     * @param xml Convert the character to XML if set to true.
     * @author Erik C. Thauvin
     *
     * @return The converted string.
     */
    public static final String charToHTML(char ch, boolean xml) {
        int c;

        // Convert left bracket
        if (ch == '<') {
            return ("&lt;");
        }

        // Convert left bracket
        else if (ch == '>') {
            return ("&gt;");
        }

        // Convert ampersand
        else if (ch == '&') {
            return ("&amp;");
        }

        // Commented out to eliminate redundant numeric character codes (ROL-507)
        // High-ASCII character
        //else if (ch >= 128)
        //{
        //c = ch;
        //return ("&#" + c + ';');
        //}

        // Convert double quote
        else if (xml && (ch == '"')) {
            return ("&quot;");
        }

        // Convert single quote
        else if (xml && (ch == '\'')) {
            return ("&#39;");
        }

        // No conversion
        else {
            // Return character as string
            return (String.valueOf(ch));
        }
    }
}

Related

  1. textToHtml(String s)
  2. textToHtml(String text)
  3. textToHTML(String text)
  4. textToHtml(String text)
  5. textToHTML(String text)
  6. toHtml(String message)
  7. toHTML(String msg)
  8. toHTML(String org, boolean inputValue)
  9. toHtml(String p_str)