001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.transformers;
011:
012: import java.io.Reader;
013: import java.io.Writer;
014: import java.util.HashMap;
015: import java.util.Map;
016:
017: /**
018: * Encodings related to Sql. It can escape quotes, by replacing them by double quotes, as is
019: * needed in SQL statements.
020: *
021: * @author Michiel Meeuwissen
022: * @author Jaco de Groot
023: */
024:
025: public class Sql extends ConfigurableReaderTransformer implements
026: CharTransformer {
027: private final static String ENCODING = "ESCAPE_SINGLE_QUOTE";
028: public final static int ESCAPE_QUOTES = 1;
029:
030: public Sql() {
031: super (ESCAPE_QUOTES);
032: }
033:
034: public Sql(int conf) {
035: super (conf);
036: }
037:
038: /**
039: * Escapes single quotes in a string.
040: * Escaping is done by doubling any quotes encountered.
041: * Strings that are rendered in such way can more easily be included
042: * in a SQL query.
043: * @param r the string to escape
044: * @param w The escaped string goes to this writer
045: * @return the writer
046: * @since MMBase-1.7
047: */
048: public static Writer singleQuote(Reader r, Writer w) {
049: try {
050: while (true) {
051: int c = r.read();
052: if (c == -1)
053: break;
054: if (c == '\'')
055: w.write(c);
056: w.write(c);
057: }
058: } catch (java.io.IOException e) {
059: }
060: return w;
061: }
062:
063: /**
064: * Unescapes single quotes in a string.
065: * Unescaping is done by replacing two quotes with one quote.
066: * @param r the string to unescape
067: * @param w the result is written to this writer.
068: * @return the writer
069: * @since MMBase-1.7.2
070: */
071: public static Writer singleQuoteBack(Reader r, Writer w) {
072: try {
073: boolean skipNext = false;
074: while (true) {
075: int c = r.read();
076: if (c == -1)
077: break;
078: if (c == '\'') {
079: if (skipNext) {
080: skipNext = false;
081: } else {
082: w.write(c);
083: skipNext = true;
084: }
085: } else {
086: w.write(c);
087: skipNext = false;
088: }
089: }
090: } catch (java.io.IOException e) {
091: }
092: return w;
093: }
094:
095: /**
096: * Used when registering this class as a possible Transformer
097: */
098:
099: public Map<String, Config> transformers() {
100: Map<String, Config> h = new HashMap<String, Config>();
101: h.put(ENCODING, new Config(Sql.class, ESCAPE_QUOTES,
102: "Escape single quotes for SQL statements"));
103: return h;
104: }
105:
106: public Writer transform(Reader r, Writer w) {
107: switch (to) {
108: case ESCAPE_QUOTES:
109: return singleQuote(r, w);
110: default:
111: throw new UnsupportedOperationException("Cannot transform");
112: }
113: }
114:
115: public Writer transformBack(Reader r, Writer w) {
116: switch (to) {
117: case ESCAPE_QUOTES:
118: return singleQuoteBack(r, w);
119: default:
120: throw new UnsupportedOperationException("Cannot transform");
121: }
122: }
123:
124: public String getEncoding() {
125: return ENCODING;
126: }
127: }
|