KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > MySQLCodec


1 /*
2  * $$Id: MySQLCodec.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.util;
26
27 /**
28  * DOCUMENT ME!
29  *
30  * @author $author$
31  * @version $Revision: 1.3 $
32  */

33 public class MySQLCodec {
34     /** It is the default array of symbols to be encoded into the SQL safe form. */
35     public static char[] defSpecSymbols = new char[] { '"', '\'', '\\' };
36
37     public static final char ESCAPE = '\\';
38
39     /**
40      * This method is a wrapper for the <code>encode(String text , char[]
41      * specSymbols)</code>
42      * method. It uses the defSpecialSymbols array as special symbols.
43      *
44      * @param text
45      * It is the string to be encoded using the default array of
46      * special symbols.
47      *
48      * @return the encoded representation of the "text" parameter
49      */

50     public static String JavaDoc encode(final String JavaDoc text) {
51         return encode(text, defSpecSymbols);
52     }
53
54     /**
55      * This method is a wrapper for the <code>encode(String text , char[]
56      * specSymbols)</code>
57      * method. It allows to specify special symbols as elements of the String
58      * object ("String specSymbols" parameter).
59      *
60      * @param text
61      * It is the string to be encoded using special symbols from the
62      * "specSymbols" parameter.
63      * @param specSymbols
64      * It is the set of special symbols to be encoded within the
65      * "text" parameter.
66      *
67      * @return the encoded representation of the "text" parameter
68      */

69     public static String JavaDoc encode(final String JavaDoc text, final String JavaDoc specSymbols) {
70         final char[] cSpecSymbols;
71
72         if ((specSymbols == null) || (specSymbols.length() == 0)) {
73             cSpecSymbols = defSpecSymbols;
74         } else {
75             cSpecSymbols = specSymbols.toCharArray();
76         }
77
78         return encode(text, cSpecSymbols);
79     }
80
81     /**
82      * It is the main encoding method. It encodes all special symbols in the
83      * "text" parameter, using the specSymbols parameter as an array of special
84      * symbols to be changed. AlSpecial symbols are translated to the "&#xxx;"
85      * code, where the "xxx" part is the decimal code of the correspondent
86      * special symbol.
87      *
88      * @param text
89      * It is the string to be encoded using special symbols from the
90      * "specSymbols" array.
91      * @param specSymbols
92      * It is the array of special symbols to be encoded within the
93      * "text" parameter.
94      *
95      * @return the encoded representation of the "text" parameter
96      */

97     public static String JavaDoc encode(final String JavaDoc text, char[] specSymbols) {
98         if (text == null) {
99             return null;
100         }
101
102         if ((specSymbols == null) || (specSymbols.length == 0)) {
103             specSymbols = defSpecSymbols;
104         }
105
106         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(text.length());
107         char[] cText = text.toCharArray();
108
109         for (int i = 0; i < cText.length; i++) {
110             char cTextChar = cText[i]; // Speed up acess speed
111
boolean isSpecial = false;
112
113             for (int ss = 0; ss < specSymbols.length; ss++) {
114                 if (specSymbols[ss] == cTextChar) {
115                     isSpecial = true;
116                     buffer.append(ESCAPE);
117                     buffer.append(cTextChar);
118
119                     break; // Exit the for cycle
120
}
121             }
122
123             if (!isSpecial) {
124                 buffer.append(cTextChar);
125             }
126         }
127
128         return buffer.toString();
129     }
130 }
131
Popular Tags