Java String Quote quote(String string, boolean escapeHtml)

Here you can find the source of quote(String string, boolean escapeHtml)

Description

quote

License

Apache License

Declaration

public static String quote(String string, boolean escapeHtml) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String quote(String string, boolean escapeHtml) {
        int length = string.length();
        if (string == null || length == 0) {
            return "\"\"";
        }// w  ww . j  a va 2  s .c  o m

        if (length > 1) {
            if (quoted(string)) {
                return string;
            }
            if (parenthesized(string)) {
                return string;
            }
        }

        char c = 0;
        int i;
        int len = string.length();
        StringBuilder sb = new StringBuilder(len + 10);
        String t;

        sb.append('"');
        for (i = 0; i < len; i += 1) {
            c = string.charAt(i);
            switch (c) {
            case '\\':
            case '"':
                sb.append('\\');
                sb.append(c);
                break;
            //             case '/':
            //                 if (b == '<') {
            //                     sb.append('\\');
            //                 }
            //                 sb.append(c);
            //                 break;
            case '\b':
                sb.append("\\b");
                break;
            case '\t':
                sb.append("\\t");
                break;
            case '\n':
                sb.append("\\\\n");
                break;
            case '\f':
                sb.append("\\f");
                break;
            case '\r':
                sb.append("\\\\r");
                break;
            case ',':
                sb.append(",");
                break;
            case '<':
                if (escapeHtml) {
                    sb.append("\\u003c");
                } else {
                    sb.append(c);
                }
                break;
            case '>':
                if (escapeHtml) {
                    sb.append("\\u003e");
                } else {
                    sb.append(c);
                }
                break;
            case '=':
                if (escapeHtml) {
                    sb.append("\\u003d");
                } else {
                    sb.append(c);
                }
                break;
            case '&':
                if (escapeHtml) {
                    sb.append("\\u0026");
                } else {
                    sb.append(c);
                }
                break;
            case '\'':
                if (escapeHtml) {
                    sb.append("\\u0027");
                } else {
                    sb.append(c);
                }
                break;
            default:
                if (c < ' ') {
                    t = "000" + Integer.toHexString(c);
                    sb.append("\\u" + t.substring(t.length() - 4));
                } else {
                    sb.append(c);
                }
            }
        }
        sb.append('"');
        return sb.toString();
    }

    public static boolean quoted(String str) {
        if (str.startsWith("\"") && str.endsWith("\"")) {
            return true;
        }
        if (str.startsWith("'") && str.endsWith("'")) {
            return true;
        }

        return false;
    }

    public static boolean parenthesized(String str) {
        if (isArrayOrList(str)) {
            return true;
        }
        if (isObjectOrMap(str)) {
            return true;
        }

        return false;
    }

    public static boolean isArrayOrList(String str) {
        if (str == null) {
            return false;
        }

        if (str.startsWith("[") && str.endsWith("]")) {
            return true;
        }

        return false;
    }

    public static boolean isObjectOrMap(String str) {
        if (str == null) {
            return false;
        }
        if (str.startsWith("{") && str.endsWith("}")) {
            return true;
        }

        return false;
    }
}

Related

  1. quote(String string)
  2. quote(String string)
  3. quote(String string)
  4. quote(String string)
  5. quote(String string)
  6. quote(String string, String charsToQuote)
  7. quote(String string, StringBuilder w)
  8. quote(String tableName)
  9. quote(String text)