Java String Double Quote doublequote(String str, boolean escapeHtml)

Here you can find the source of doublequote(String str, boolean escapeHtml)

Description

doublequote

License

Apache License

Declaration

public static String doublequote(String str, boolean escapeHtml) 

Method Source Code

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

public class Main {
    public static String doublequote(String str, boolean escapeHtml) {
        if (str == null) {
            return null;
        }//from  w  w w  . j  ava  2 s  .co  m

        return quote(str, escapeHtml);
    }

    public static String doublequote(Object obj, boolean escapeHtml) {
        return doublequote(obj.toString(), escapeHtml);
    }

    public static String doublequote(Object obj) {
        return doublequote(obj, true);
    }

    public static String quote(String string, boolean escapeHtml) {
        int length = string.length();
        if (string == null || length == 0) {
            return "\"\"";
        }

        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. doubleQuote(String message)
  2. doubleQuote(String s)
  3. doubleQuote(String s)
  4. doubleQuote(String str)
  5. doubleQuote(String str)
  6. doubleQuote(String string)
  7. doubleQuote(String value)
  8. doubleQuote(String x)
  9. doubleQuoted(String string)