View Javadoc

1   package org.paneris.util;
2   
3   public class SQLUtils {
4   
5     public static String escapedString(String s) {
6       int l = s.length();
7       for (int i = 0; i < l; ++i) {
8         char c = s.charAt(i);
9         switch (c) {
10          case '\'': case '\\':
11  
12           // damn, found one; catch up to here ...
13  
14          StringBuffer t = new StringBuffer(l + 2);
15          for (int j = 0; j < i; ++j)
16            t.append(s.charAt(j));
17          t.append('\\');
18          t.append(c);
19          // ... and continue
20          for (++i; i < l; ++i) {
21            c = s.charAt(i);
22            switch (c) {
23            case '\'': case '\\':
24              t.append('\\');
25            default:;
26            }
27            t.append(c);
28          }
29          return t.toString();
30          default:;
31        }
32      }
33      return s;
34    }
35  
36    public static void main(String[] args) {
37      for (int i = 0; i < args.length; ++i)
38        System.out.println(escapedString(args[i]));
39    }
40  }