Java HTML Encode toHtmlString(String s)

Here you can find the source of toHtmlString(String s)

Description

to Html String

License

Open Source License

Declaration

public static String toHtmlString(String s) 

Method Source Code

//package com.java2s;

public class Main {
    public static final String BOLD_START = "%%%BOLD_START%%%";
    public static final String BOLD_END = "%%%BOLD_END%%%";
    public static final String NEW_LINE = "%%%NEW_LINE%%%";

    public static String toHtmlString(String s) {
        String toReturn = null;/*  ww w .j av a2 s  .co  m*/
        if (s == null) {
            return null;
        }
        if (s.contains("!DOCTYPE HTML")) {
            s = replace(s, "!DOCTYPE HTML", "");
            return s;
        }
        toReturn = s;
        if (toReturn.contains("<")) {
            if (toReturn.contains("</a>") || toReturn.contains("</A>")) {
                return toReturn;
            }
            toReturn = replace(toReturn, "<B>", BOLD_START);
            toReturn = replace(toReturn, "<b>", BOLD_START);
            toReturn = replace(toReturn, "</B>", BOLD_END);
            toReturn = replace(toReturn, "</b>", BOLD_END);
            toReturn = replace(toReturn, "<BR>", NEW_LINE);
            toReturn = replace(toReturn, "<br>", NEW_LINE);
            toReturn = replace(toReturn, "<", "&lt;");
            toReturn = replace(toReturn, ">", "&gt;");
            toReturn = replace(toReturn, BOLD_START, "<B>");
            toReturn = replace(toReturn, BOLD_END, "</B>");
            toReturn = replace(toReturn, NEW_LINE, "<br>");
        }
        toReturn = replace(toReturn, "\r\n", "<br>");
        //toReturn = replace(toReturn, "\n\r", "<br>");
        toReturn = replace(toReturn, "\r", "<br>");
        toReturn = replace(toReturn, "\n", "<br>");
        toReturn = replace(toReturn, " ", "&nbsp;");
        toReturn = replace(toReturn, "\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        return toReturn;
    }

    /**
     * The source was taken from the internet. Replace a text in other text in a
     * given input text.
     * 
     * @param lookIn
     *            The text to look in.
     * @param lookFor
     *            The text to look for.
     * @param replaceWith
     *            The text to replace with.
     * 
     * @return The text after the replace operation.
     */
    public static String replace(String lookIn, String lookFor, String replaceWith) {
        int count = 0;
        int i, j;
        StringBuffer sb;

        for (i = 0; (i = lookIn.indexOf(lookFor, i)) != -1; i += lookFor.length())
            ++count;
        if (count == 0) {
            return lookIn;
        }
        sb = new StringBuffer(lookIn.length() + count * (replaceWith.length() - lookFor.length()));
        for (i = 0; (j = lookIn.indexOf(lookFor, i)) != -1; i = j + lookFor.length())
            sb.append(lookIn.substring(i, j)).append(replaceWith);
        sb.append(lookIn.substring(i));
        return sb.toString();
    }
}

Related

  1. toHtmlInput(String str)
  2. toHtmlLine(String input)
  3. toHTMLString(String in)
  4. toHtmlString(String input)
  5. toHTMLString(String org)
  6. toHTMLString(String str)
  7. toHtmlText(String s)
  8. toHtmlText(String text)
  9. toHtmlTitle(final String title)