KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Escape


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 /**
13  * Class for escaping single quotes in a string, so that they can be safely
14  * included in a SQL statement.
15  *
16  * @deprecated Use org.mmbase.util.Encode
17  * @version $Id: Escape.java,v 1.7 2004/09/29 14:29:23 pierre Exp $
18  */

19 public class Escape {
20
21     /**
22      * Escapes single quotes in a string.
23      * Escaping is done by doubling any quotes encountered.
24      * Strings that are rendered in such way can more easily be included
25      * in a SQL query.
26      * @param in the string to escape
27      * @return the escaped string
28      * @duplicate use {@link Encode} Encode encoder = new Encode("ESCAPE_SINGLE_QUOTE");<BR>encoder.encode("MMBase it's escaping quotes");
29      */

30     static public String JavaDoc singlequote(String JavaDoc in) {
31         if (in == null || in.indexOf('\'') == -1) return in;
32
33         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
34         char[] data = in.toCharArray();
35         for (int i = 0; i < data.length; i++) {
36             if (data[i] == '\'') sb.append("''");
37             else sb.append(data[i]);
38         }
39         return sb.toString();
40     }
41 }
42
Popular Tags