From 750550e6df3cf8c9013f7fa455218b8d3847e181 Mon Sep 17 00:00:00 2001 From: "pabs@pablotron.org" Date: Thu, 18 Dec 2014 15:10:23 -0500 Subject: add java implementation --- java/pablotron/luigi/Filter.java | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 java/pablotron/luigi/Filter.java (limited to 'java/pablotron/luigi/Filter.java') diff --git a/java/pablotron/luigi/Filter.java b/java/pablotron/luigi/Filter.java new file mode 100644 index 0000000..d89beab --- /dev/null +++ b/java/pablotron/luigi/Filter.java @@ -0,0 +1,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 row); + }; + + public static Map FILTERS = new HashMap() {{ + put("null", new Handler() { + public String filter(String val, String args[], Map row) { + return ""; + } + }); + + put("s", new Handler() { + public String filter(String val, String args[], Map row) { + int v = Integer.parseInt(val); + return (v == 1) ? "" : "s"; + } + }); + + put("uc", new Handler() { + public String filter(String val, String args[], Map row) { + return val.toUpperCase(); + } + }); + + put("lc", new Handler() { + public String filter(String val, String args[], Map row) { + return val.toLowerCase(); + } + }); + + put("length", new Handler() { + public String filter(String val, String args[], Map row) { + return Integer.toString(val.length()); + } + }); + + put("trim", new Handler() { + public String filter(String val, String args[], Map row) { + return val.trim(); + } + }); + + put("h", new Handler() { + public String filter(String val, String args[], Map 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("&"); + break; + case '<': + r.append("<"); + break; + case '>': + r.append(">"); + break; + case '\'': + r.append("'"); + break; + case '"': + r.append("""); + break; + default: + r.append(c); + } + } + + return r.toString(); +/* + * return val + * .replace("&", "&") + * .replace("<", "<") + * .replace(">", ">") + * .replace("'", "'") + * .replace("\"", """); + */ + } + }); + }}; +}; -- cgit v1.2.3