Java String Escape escapeCellText(String text, boolean wrap, boolean multiline)

Here you can find the source of escapeCellText(String text, boolean wrap, boolean multiline)

Description

Escape character that has special meaning in HTML such as <, &, etc..

License

Open Source License

Parameter

Parameter Description
text the text
wrap whether to allow wrap
multiline whether to show multiple line

Return

the HTML

Declaration

public static String escapeCellText(String text, boolean wrap, boolean multiline) 

Method Source Code

//package com.java2s;

import java.util.List;

public class Main {
    /**/*from  www . j ava  2 s .co m*/
     * Escape character that has special meaning in HTML such as &lt;, &amp;, etc..
     * @param text the text
     * @param wrap whether to allow wrap
     * @param multiline whether to show multiple line
     * @return the HTML 
     */
    public static String escapeCellText(String text, boolean wrap, boolean multiline) {
        final StringBuffer out = new StringBuffer();
        for (int j = 0, tl = text.length(); j < tl; ++j) {
            char cc = text.charAt(j);
            switch (cc) {
            case '&':
                out.append("&amp;");
                break;
            case '<':
                out.append("&lt;");
                break;
            case '>':
                out.append("&gt;");
                break;
            case ' ':
                out.append(wrap ? " " : "&nbsp;");
                break;
            case '\n':
                if (wrap && multiline) {
                    out.append("<br/>");
                    break;
                }
            default:
                out.append(cc);
            }
        }
        return out.toString();
    }

    private static String escapeCellText(String text, boolean wrap, boolean multiline, List<int[]> runs) {
        StringBuffer out = new StringBuffer();
        if (text != null) {
            int j = 0;
            for (int[] run : runs) {
                for (int tl = run[0]; j < tl; ++j) {
                    char cc = text.charAt(j);
                    out.append(cc);
                }
                for (int tl = run[1]; j < tl; ++j) {
                    char cc = text.charAt(j);
                    switch (cc) {
                    case '&':
                        out.append("&amp;");
                        break;
                    case '<':
                        out.append("&lt;");
                        break;
                    case '>':
                        out.append("&gt;");
                        break;
                    case ' ':
                        out.append(wrap ? " " : "&nbsp;");
                        break;
                    case '\n':
                        if (wrap && multiline) {
                            out.append("<br/>");
                            break;
                        }
                    default:
                        out.append(cc);
                    }
                }
            }
            for (int tl = text.length(); j < tl; ++j) {
                char cc = text.charAt(j);
                out.append(cc);
            }
        }
        return out.toString();
    }
}

Related

  1. escape(String s)
  2. escape(String s)
  3. escape(String str)
  4. escape(String string, String chars)
  5. escapeASN1(String str)
  6. escapeCharacters(String string)
  7. escapeHTML(String s)
  8. escapeHTML(String source)
  9. escapeHTMLTags(String in)