Java String Quote QuoteValueIfNeeded(String str)

Here you can find the source of QuoteValueIfNeeded(String str)

Description

Quote Value If Needed

License

Creative Commons License

Declaration

public static String QuoteValueIfNeeded(String str) 

Method Source Code

//package com.java2s;

public class Main {
    public static String QuoteValueIfNeeded(String str) {
        return (!ShouldQuote(str)) ? str : QuoteValue(str);
    }//  w ww .j a  v  a  2 s  . c om

    private static boolean ShouldQuote(String str) {
        if (str.length() == 0) {
            // Empty String
            return true;
        }
        if (str.charAt(str.length() - 1) == ' ' || str.charAt(str.length() - 1) == '\t') {
            // Space or tab at end
            return true;
        }
        if (str.charAt(0) == ' ' || str.charAt(0) == '\t') {
            // Space or tab at beginning
            return true;
        }
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == '\\' || str.charAt(i) == '"') {
                return true;
            }
            if ((str.charAt(i) == ' ' || str.charAt(i) == '\t') && i + 1 < str.length()
                    && (str.charAt(i + 1) == ' ' || str.charAt(i + 1) == '\t')) {
                // run of two or more space and/or tab
                return true;
            }
            if ((str.charAt(i) == '\r') && i + 1 < str.length() && (str.charAt(i + 1) == '\n')) {
                // CRLF
                if (i == 0 && i + 2 < str.length() && (str.charAt(i + 1) == ' ' || str.charAt(i + 1) == '\t')) {
                    // CRLF followed by space or tab at beginning
                    return true;
                }
                continue;
            }
            char c = str.charAt(i);
            // Has specials, or CTLs other than tab
            if ((c < 0x20 && c != '\t') || c == 0x7F || c == 0x28 || c == 0x29 || c == 0x3c || c == 0x3e
                    || c == 0x5b || c == 0x5d || c == 0x3a || c == 0x3b || c == 0x40 || c == 0x5c || c == 0x2c
                    || c == 0x2e || c == '"') {
                return true;
            }
        }
        return false;
    }

    /**
     * Quotes a string according to RFC 5322 rules.
     * @param str A string object.
     * @return A string object.
     */
    public static String QuoteValue(String str) {
        StringBuilder builder = new StringBuilder();
        builder.append('"');
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == '\\' || str.charAt(i) == '"') {
                builder.append('\\');
                builder.append(str.charAt(i));
            } else {
                builder.append(str.charAt(i));
            }
        }
        builder.append('"');
        return builder.toString();
    }
}

Related

  1. quoteText(String textToQuote, boolean sbAppend)
  2. quoteTo(CharSequence cs, StringBuilder target)
  3. quoteTokenize(String clientResponse)
  4. QuoteValue(String str)
  5. quoteValue(String value)
  6. quoteValues(String value)
  7. quoteWhitespaces(String path)
  8. quoteWrap(String name)