Unescape any MySQL escape sequences. : String Escape « Data Type « Java






Unescape any MySQL escape sequences.

     
import java.util.StringTokenizer;

/* Copyright (c) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.google.gdata.util.common.base;


/**
 * Some common string manipulation utilities.
 */
public class Util{

    /**
     * Unescape any MySQL escape sequences.
     * See MySQL language reference Chapter 6 at
     * <a href="http://www.mysql.com/doc/">http://www.mysql.com/doc/</a>.
     * This function will <strong>not</strong> work for other SQL-like
     * dialects.
     * @param s string to unescape, with the surrounding quotes.
     * @return unescaped string, without the surrounding quotes.
     * @exception IllegalArgumentException if s is not a valid MySQL string.
     */
    public static String unescapeMySQLString(String s)
        throws IllegalArgumentException {
      // note: the same buffer is used for both reading and writing
      // it works because the writer can never outrun the reader
      char chars[] = s.toCharArray();

      // the string must be quoted 'like this' or "like this"
      if (chars.length < 2 || chars[0] != chars[chars.length-1] ||
          (chars[0] != '\'' && chars[0] != '"')) {
        throw new IllegalArgumentException("not a valid MySQL string: " + s);
      }

      // parse the string and decode the backslash sequences; in addition,
      // quotes can be escaped 'like this: ''', "like this: """, or 'like this: "'
      int j = 1;  // write position in the string (never exceeds read position)
      int f = 0;  // state: 0 (normal), 1 (backslash), 2 (quote)
      for (int i = 1; i < chars.length - 1; i++) {
        if (f == 0) {             // previous character was normal
    if (chars[i] == '\\') {
      f = 1;  // backslash
    } else if (chars[i] == chars[0]) {
      f = 2;  // quoting character
    } else {
      chars[j++] = chars[i];
    }
        } else if (f == 1) {      // previous character was a backslash
    switch (chars[i]) {
      case '0':   chars[j++] = '\0';   break;
      case '\'':  chars[j++] = '\'';   break;
      case '"':   chars[j++] = '"';    break;
      case 'b':   chars[j++] = '\b';   break;
      case 'n':   chars[j++] = '\n';   break;
      case 'r':   chars[j++] = '\r';   break;
      case 't':   chars[j++] = '\t';   break;
      case 'z':   chars[j++] = '\032'; break;
      case '\\':  chars[j++] = '\\';   break;
      default:
        // if the character is not special, backslash disappears
        chars[j++] = chars[i];
        break;
    }
    f = 0;
        } else {                  // previous character was a quote
    // quoting characters must be doubled inside a string
    if (chars[i] != chars[0]) {
      throw new IllegalArgumentException("not a valid MySQL string: " + s);
    }
    chars[j++] = chars[0];
    f = 0;
        }
      }
      // string contents cannot end with a special character
      if (f != 0) {
        throw new IllegalArgumentException("not a valid MySQL string: " + s);
      }

      // done
      return new String(chars, 1, j - 1);
    }

}

   
    
    
    
    
  








Related examples in the same category

1.Ends With Ignore Case
2.Escape commas in the string using the default escape char
3.Escapes characters that have special meaning to regular expressions
4.unEscape String
5.Quote string
6.Unquote string
7.Removes the double quote from the start and end of the supplied string if it starts and ends with this character
8.Escape string
9.Unescape any C escape sequences (\n, \r, \\, \ooo, etc) and return the resulting string.
10.Escape a string for use inside as XML element content. This escapes less-than and ampersand, only.
11.Escape a string for use inside as XML single-quoted attributes.
12.Replace String With Escape