Java String Quote quote(String name)

Here you can find the source of quote(String name)

Description

Return a representation of the given name ensuring quoting (wrapped with '`' characters).

License

LGPL

Parameter

Parameter Description
name The name to quote.

Return

The quoted version.

Declaration

public static String quote(String name) 

Method Source Code

//package com.java2s;
/*// w ww  .j  av  a 2  s.  c  o  m
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

public class Main {
    /**
     * Return a representation of the given name ensuring quoting (wrapped with '`' characters).  If already wrapped
     * return name.
     *
     * @param name The name to quote.
     *
     * @return The quoted version.
     */
    public static String quote(String name) {
        if (isEmpty(name) || isQuoted(name)) {
            return name;
        }
        // Convert the JPA2 specific quoting character (double quote) to Hibernate's (back tick)
        else if (name.startsWith("\"") && name.endsWith("\"")) {
            name = name.substring(1, name.length() - 1);
        }

        return "`" + name + '`';
    }

    public static boolean isEmpty(String string) {
        return string == null || string.length() == 0;
    }

    /**
     * Determine if the given string is quoted (wrapped by '`' characters at beginning and end).
     *
     * @param name The name to check.
     *
     * @return True if the given string starts and ends with '`'; false otherwise.
     */
    public static boolean isQuoted(String name) {
        return name != null && name.length() != 0
                && ((name.charAt(0) == '`' && name.charAt(name.length() - 1) == '`')
                        || (name.charAt(0) == '"' && name.charAt(name.length() - 1) == '"'));
    }
}

Related

  1. quote(String literal)
  2. quote(String message, String quotationMark)
  3. quote(String name)
  4. quote(String name)
  5. quote(String name)
  6. quote(String p_sql)
  7. quote(String p_string)
  8. quote(String path)
  9. quote(String s)