KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > Sql


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.transformers;
11
12 import java.io.Reader JavaDoc;
13 import java.io.Writer JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * Encodings related to Sql. It can escape quotes, by replacing them by double quotes, as is
20  * needed in SQL statements.
21  *
22  * @author Michiel Meeuwissen
23  * @author Jaco de Groot
24  */

25
26 public class Sql extends ConfigurableReaderTransformer implements CharTransformer {
27     private final static String JavaDoc ENCODING = "ESCAPE_SINGLE_QUOTE";
28     public final static int ESCAPE_QUOTES = 1;
29
30     public Sql() {
31         super(ESCAPE_QUOTES);
32     }
33
34     public Sql(int conf) {
35         super(conf);
36     }
37
38     /**
39      * Escapes single quotes in a string.
40      * Escaping is done by doubling any quotes encountered.
41      * Strings that are rendered in such way can more easily be included
42      * in a SQL query.
43      * @param r the string to escape
44      * @param w The escaped string goes to this writer
45      * @return the writer
46      * @since MMBase-1.7
47      */

48     public static Writer JavaDoc singleQuote(Reader JavaDoc r, Writer JavaDoc w) {
49         try {
50             while (true) {
51                 int c = r.read();
52                 if (c == -1) break;
53                 if(c == '\'') w.write(c);
54                 w.write(c);
55             }
56         } catch (java.io.IOException JavaDoc e) {
57         }
58         return w;
59     }
60
61     /**
62      * Unescapes single quotes in a string.
63      * Unescaping is done by replacing two quotes with one quote.
64      * @param r the string to unescape
65      * @param w the result is written to this writer.
66      * @return the writer
67      * @since MMBase-1.7.2
68      */

69     public static Writer JavaDoc singleQuoteBack(Reader JavaDoc r, Writer JavaDoc w) {
70         try {
71             boolean skipNext = false;
72             while (true) {
73                 int c = r.read();
74                 if (c == -1) break;
75                 if(c == '\'') {
76                     if (skipNext) {
77                         skipNext = false;
78                     } else {
79                         w.write(c);
80                         skipNext = true;
81                     }
82                 } else {
83                       w.write(c);
84                       skipNext = false;
85                 }
86             }
87         } catch (java.io.IOException JavaDoc e) {
88         }
89         return w;
90     }
91
92     /**
93      * Used when registering this class as a possible Transformer
94      */

95
96     public Map JavaDoc transformers() {
97         HashMap JavaDoc h = new HashMap JavaDoc();
98         h.put(ENCODING, new Config(Sql.class, ESCAPE_QUOTES, "Escape single quotes for SQL statements"));
99         return h;
100     }
101
102     public Writer JavaDoc transform(Reader JavaDoc r, Writer JavaDoc w) {
103         switch(to){
104         case ESCAPE_QUOTES: return singleQuote(r, w);
105         default: throw new UnsupportedOperationException JavaDoc("Cannot transform");
106         }
107     }
108
109     public Writer JavaDoc transformBack(Reader JavaDoc r, Writer JavaDoc w) {
110         switch(to){
111         case ESCAPE_QUOTES: return singleQuoteBack(r, w);
112         default: throw new UnsupportedOperationException JavaDoc("Cannot transform");
113         }
114     }
115
116     public String JavaDoc getEncoding() {
117         return ENCODING;
118     }
119 }
120
Popular Tags