Java String Unquote unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)

Here you can find the source of unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)

Description

Trims identifier, removes quote chars from first and last positions and replaces double occurrences of quote char from entire identifier, i.e converts quoted identifier into form as it is stored in database.

License

Open Source License

Parameter

Parameter Description
identifier a parameter
useAnsiQuotedIdentifiers should we check for " quotes too.

Return

  • null -> null
  • abc -> abc
  • `abc` -> abc
  • `ab``c` -> ab`c
  • `"ab`c"` -> "ab`c"
  • `ab"c` -> ab"c
  • "abc" -> abc
  • "`ab""c`" -> `ab"c`
  • "ab`c" -> ab`c
  • Declaration

    public static String unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers) 
    

    Method Source Code

    //package com.java2s;
    /*// w  ww .  java 2 s  .  co  m
     Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
        
        
      The MySQL Connector/J is licensed under the terms of the GPLv2
      <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
      There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
      this software, see the FLOSS License Exception
      <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
        
      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., 51 Franklin St, Fifth
      Floor, Boston, MA 02110-1301  USA
        
        
        
     */
    
    public class Main {
        /**
         * Trims identifier, removes quote chars from first and last positions
         * and replaces double occurrences of quote char from entire identifier,
         * i.e converts quoted identifier into form as it is stored in database.
         *
         * @param identifier
         * @param useAnsiQuotedIdentifiers should we check for " quotes too.
         * @return
         * <li>null -> null</li>
         * <li>abc -> abc</li>
         * <li>`abc` -> abc</li>
         * <li>`ab``c` -> ab`c</li>
         * <li>`"ab`c"` -> "ab`c"</li>
         * <li>`ab"c` -> ab"c</li>
         * <li>"abc" -> abc</li>
         * <li>"`ab""c`" -> `ab"c`</li>
         * <li>"ab`c" -> ab`c</li>
         */
        public static String unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers) {
            if (identifier == null) {
                return null;
            }
    
            identifier = identifier.trim();
    
            String quoteChar = null;
    
            // Backquotes are always valid identifier quotes
            if (identifier.startsWith("`") && identifier.endsWith("`")) {
                quoteChar = "`";
            }
    
            if (quoteChar == null && useAnsiQuotedIdentifiers) {
                if (identifier.startsWith("\"") && identifier.endsWith("\"")) {
                    quoteChar = "\"";
                }
            }
    
            if (quoteChar != null) {
                identifier = identifier.substring(1, (identifier.length() - 1));
                return identifier.replaceAll(quoteChar + quoteChar, quoteChar);
            }
    
            return identifier;
        }
    
        private static boolean startsWith(byte[] dataFrom, String chars) {
            for (int i = 0; i < chars.length(); i++) {
                if (dataFrom[i] != chars.charAt(i)) {
                    return false;
                }
            }
            return true;
        }
    
        private static boolean endsWith(byte[] dataFrom, String suffix) {
            for (int i = 1; i <= suffix.length(); i++) {
                int dfOffset = dataFrom.length - i;
                int suffixOffset = suffix.length() - i;
                if (dataFrom[dfOffset] != suffix.charAt(suffixOffset)) {
                    return false;
                }
            }
            return true;
        }
    }
    

    Related

    1. unquoteCharacterLiteral(String charLiteralStr)
    2. unquoteCString(String str)
    3. unquotedCharLiteral(char c)
    4. unquotedString(String str)
    5. unquoteHtmlContent(String x)
    6. unQuoteIdentifier(String identifier, String quoteChar)
    7. unQuoteIdentifier(String identifier, String quoteChar)
    8. unquoteImpl(String s, char delim)
    9. unquotePath(String path)