Java String Quote quoteString(String value, boolean force)

Here you can find the source of quoteString(String value, boolean force)

Description

Returns the MIME type parameter value as either an HTTP token or HTTP quoted-string depending on whether or not it contains any characters that need to be escaped.

License

Open Source License

Parameter

Parameter Description
force When true the returned value is always a <code>quoted-string</code>.

Declaration


static public String quoteString(String value, boolean force) 

Method Source Code

//package com.java2s;
/**//from w w  w .j  a  v a 2  s  .c o  m
    
Copyright (C) SYSTAP, LLC 2006-2011.  All rights reserved.
    
Contact:
 SYSTAP, LLC
 4501 Tower Road
 Greensboro, NC 27410
 licenses@bigdata.com
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

public class Main {
    /**
     * Returns the MIME type parameter value as either an HTTP
     * <code>token</code> or HTTP <code>quoted-string</code> depending
     * on whether or not it contains any characters that need to be
     * escaped.
     *
     * @param force When true the returned value is always a
     * <code>quoted-string</code>.
     */

    static public String quoteString(String value, boolean force) {

        StringBuffer sb = new StringBuffer();

        int len = value.length();

        boolean didEscape = false;

        for (int i = 0; i < len; i++) {

            char ch = value.charAt(i);

            if (isHttpCtlChar(ch) || isHttpSeparatorChar(ch)) {

                sb.append('\\');

                didEscape = true;

            }

            sb.append(ch);

        }

        return (didEscape || force) ? "\"" + sb.toString() + "\"" : sb.toString();

    }

    /**
     * Returns true iff the character is an HTTP <code>CTL</code>
     * character.
     */

    static public boolean isHttpCtlChar(char ch) {

        int c = (int) ch;

        if (c >= 0 && c <= 31)
            return true;

        if (c == 127)
            return true;

        return false;

    }

    /**
     * Returns true iff the character is an HTTP
     * <code>separator</code> character.
     */

    static public boolean isHttpSeparatorChar(char ch) {

        switch (ch) {
        case '(':
        case ')':
        case '<':
        case '>':
        case '@':
        case ',':
        case ';':
        case ':':
        case '\\':
        case '"':
        case '/':
        case '[':
        case ']':
        case '?':
        case '=':
        case '{':
        case '}':
        case ' ': // ASCII space (32).
        case (char) 0x09: // Horizontal tab (9).

            return true;

        }

        return false;

    }
}

Related

  1. quoteString(String str)
  2. quoteString(String string)
  3. quoteString(String strVal)
  4. quoteString(String t)
  5. quoteString(String unquoted)
  6. quoteStringLiteral(String string)
  7. quoteStringSQL(String s)
  8. quoteStringSQL(String s)
  9. quoteStringValue(String value)