Java Encode encodeHTML(String input, boolean reduceToASCII, boolean useHTMLTags)

Here you can find the source of encodeHTML(String input, boolean reduceToASCII, boolean useHTMLTags)

Description

Encodes an input string of plain text to it's HTML representation.

License

Apache License

Parameter

Parameter Description
input The plain text string to convert
reduceToASCII true if you want all non-ASCII characters to be converted to entities
useHTMLTags true if you want line feeds to be converted to <br> tags, false if they should be removed

Return

The HTML representation of the plain text.

Declaration

public static String encodeHTML(String input, boolean reduceToASCII, boolean useHTMLTags) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * /*from  www . java  2 s  .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.io.IOException;

import java.io.StringReader;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Set;

public class Main {
    /**
     * For {@link #encodeHTML(String, boolean, int)}, to let linefeeds be converted to <br> tags
     */
    public static final int LINEFEEDS_CONVERT_TO_BR = 1;
    /**
     * For {@link #encodeHTML(String, boolean, int)}, to let linefeeds be kept untouched
     */
    public static final int LINEFEEDS_KEEP = 2;
    /**
     * For {@link #encodeHTML(String, boolean, int)}, to let linefeeds be removed from the output
     */
    public static final int LINEFEEDS_REMOVE = 3;

    /**
     * Encodes an input string of plain text to it's HTML representation.
     * Therefor:
     * <ul>
     * <li>All line feeds are converted to <br/> (if param useHTMLTags is true)
     * <li>The special characters &, <, > and " are converted to entities
     * <li> All characters with character code >= 127 are converted to HTML entities (if param reduceToASCII==true)
     * </ul>
     * 
     * @param input
     *            The plain text string to convert
     * @param reduceToASCII
     *            true if you want all non-ASCII characters to be converted to entities
     * @param useHTMLTags
     *            true if you want line feeds to be converted to <br> tags, false if they should be removed
     * @return The HTML representation of the plain text.
     */
    public static String encodeHTML(String input, boolean reduceToASCII, boolean useHTMLTags) {
        return encodeHTML(input, reduceToASCII, (useHTMLTags ? LINEFEEDS_CONVERT_TO_BR : LINEFEEDS_REMOVE));
    }

    /**
     * Encodes an input string of plain text to it's HTML representation.
     * Therefor:
     * <ul>
     * <li>All line feeds are converted to <br/> (if param useHTMLTags is true)
     * <li>The special characters &, <, > and " are converted to entities
     * <li> All characters with character code >= 127 are converted to HTML entities (if param reduceToASCII==true)
     * </ul>
     * 
     * @param input
     *            The plain text string to convert
     * @param reduceToASCII
     *            true if you want all non-ASCII characters to be converted to entities
     * @param lineFeedTreatment
     *            Way how line feeds are treated in the source. Use constants LINEFEEDS_... if you want them to be converted, ignored or removed
     * @return The HTML representation of the plain text.
     */
    public static String encodeHTML(String input, boolean reduceToASCII, int lineFeedTreatment) {
        return encodeHTML(input, reduceToASCII, lineFeedTreatment, null);
    }

    public static String encodeHTML(String input, boolean reduceToASCII, int lineFeedTreatment,
            Set<Integer> additionalCharsToEncode) {
        if (input == null) {
            return "";
        }

        if (additionalCharsToEncode == null) {
            additionalCharsToEncode = Collections.emptySet();
        }

        StringReader in = new StringReader(input);
        StringBuffer out = new StringBuffer();
        int ch;

        try {
            while ((ch = in.read()) != -1) {
                if (ch == '\n') {
                    if (lineFeedTreatment == LINEFEEDS_CONVERT_TO_BR) {
                        out.append("<br>");
                    } else if (lineFeedTreatment == LINEFEEDS_KEEP) {
                        out.append("\n");
                    }
                } else if ((!reduceToASCII || ch < 127) && ch != '&' && ch != '<' && ch != '>' && ch != '"'
                        && !additionalCharsToEncode.contains(ch)) {
                    out.append((char) ch);
                } else {
                    out.append('&').append('#').append(ch).append(';');
                }
            }
        } catch (IOException exc) {
            exc.printStackTrace();
        }

        return out.toString();
    }

    /**
     * Encodes an input string of plain text to it's HTML representation.
     * Therefor:
     * <ul>
     * <li>All line feeds are converted to <br/>
     * <li>The special characters &, <, > and " are converted to entities
     * <li> All characters with character code >= 127 are converted to HTML entities (if param reduceToASCII==true)
     * </ul>
     * 
     * @param input
     *            The plain text string to convert
     * @param reduceToASCII
     *            true if you want all non-ASCII characters to be converted to entities
     * @return The HTML representation of the plain text.
     */
    public static String encodeHTML(String input, boolean reduceToASCII) {
        return encodeHTML(input, reduceToASCII, true);
    }

    /**
     * Encodes an input string of plain text to it's HTML representation.
     * Therefor:
     * <ul>
     * <li>All line feeds are converted to <br/>
     * <li>The special characters &, <, > and " are converted to entities
     * <li> All characters with character code >= 127 are converted to HTML entities
     * </ul>
     * 
     * @param input
     *            The plain text string to convert
     * @return The HTML representation of the plain text.
     */
    public static String encodeHTML(String input) {
        return encodeHTML(input, true, true);
    }

    /**
     * Creates a new list that contains the string representations
     * of all elements of the original list
     * null values are preserved.
     * 
     * @param listOriginal
     * @return The list with all elements converted to their string representation
     */
    public static List<String> toString(List<Object> listOriginal) {

        List<String> list = new ArrayList<String>();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem != null) {
                list.add(String.valueOf(elem));
            } else {
                list.add(null);
            }
        }

        return list;

    }
}

Related

  1. encodeFile(String filePath)
  2. encodeForFilter(final String filterString)
  3. encodeForString(String str)
  4. encodeGoomojiSubject(String subject)
  5. encodeHeaderValue(String headerName, String headerValue)
  6. encodeHttpHeaderValueAsUTF8(String text)
  7. encodeImage(String sourceImagePath)
  8. encodeInt(long value)
  9. encodeKeyForFileSystemName(String originalKey)