aboutsummaryrefslogtreecommitdiff
path: root/java/pablotron/luigi/Filter.java
blob: d89beabd91fa8760d79befac45cf7e7ae4cf4932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package pablotron.luigi;

import java.util.Map;
import java.util.HashMap;

public final class Filter {
  public interface Handler {
    public String filter(String val, String args[], Map<String, String> row);
  };

  public static Map<String, Handler> FILTERS = new HashMap<String, Handler>() {{
    put("null", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return "";
      }
    });

    put("s", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        int v = Integer.parseInt(val);
        return (v == 1) ? "" : "s";
      }
    });

    put("uc", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return val.toUpperCase();
      }
    });

    put("lc", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return val.toLowerCase();
      }
    });

    put("length", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return Integer.toString(val.length());
      }
    });

    put("trim", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        return val.trim();
      }
    });

    put("h", new Handler() {
      public String filter(String val, String args[], Map<String, String> row) {
        StringBuilder r = new StringBuilder(val.length());

        for (int i = 0, l = val.length(); i < l; i++) {
          char c = val.charAt(i);

          switch (c) {
            case '&':
              r.append("&amp;");
              break;
            case '<':
              r.append("&lt;");
              break;
            case '>':
              r.append("&gt;");
              break;
            case '\'':
              r.append("&apos;");
              break;
            case '"':
              r.append("&quot;");
              break;
            default:
              r.append(c);
          }
        }

        return r.toString();
/* 
 *         return val
 *           .replace("&", "&amp;")
 *           .replace("<", "&lt;")
 *           .replace(">", "&gt;")
 *           .replace("'", "&apos;")
 *           .replace("\"", "&quot;");
 */ 
      }
    });
  }};
};