- add a bunch of new functions
- very lax date parser - lots of new advanced string functions - new version of commons-lang git-svn-id: http://google-refine.googlecode.com/svn/trunk@152 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
25fd5794cd
commit
f1923758e7
@ -7,7 +7,7 @@
|
||||
<classpathentry kind="lib" path="lib/jetty-util-6.1.22.jar" sourcepath="lib-src/jetty-util-6.1.22-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar" sourcepath="lib-src/log4j-1.2.15-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-codec-1.3.jar" sourcepath="lib-src/commons-codec-1.3-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-lang-2.4.jar" sourcepath="lib-src/commons-lang-2.4-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/commons-lang-2.5.jar" sourcepath="lib-src/commons-lang-2.5-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/cos-05Nov2002.jar"/>
|
||||
<classpathentry kind="lib" path="lib/json-20100208.jar" sourcepath="lib-src/json-20100208-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/poi-3.6.jar"/>
|
||||
|
Binary file not shown.
BIN
lib-src/commons-lang-2.5-sources.jar
Normal file
BIN
lib-src/commons-lang-2.5-sources.jar
Normal file
Binary file not shown.
Binary file not shown.
BIN
lib/commons-lang-2.5.jar
Normal file
BIN
lib/commons-lang-2.5.jar
Normal file
Binary file not shown.
1942
src/main/java/com/metaweb/gridworks/expr/CalendarParser.java
Normal file
1942
src/main/java/com/metaweb/gridworks/expr/CalendarParser.java
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,24 @@
|
||||
package com.metaweb.gridworks.expr;
|
||||
|
||||
// Taken from http://icecube.wisc.edu/~dglo/software/calparse/index.html
|
||||
// Copyright Dave Glowacki. Released under the BSD license.
|
||||
|
||||
/**
|
||||
* Thrown when an invalid date is encountered in <tt>CalendarParser</tt>.
|
||||
*/
|
||||
public class CalendarParserException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 7195725880623801198L;
|
||||
|
||||
/**
|
||||
* Default date format exception.
|
||||
*/
|
||||
public CalendarParserException() { super(); }
|
||||
|
||||
/**
|
||||
* Date format exception.
|
||||
*
|
||||
* @param str error message
|
||||
*/
|
||||
public CalendarParserException(String str) { super(str); }
|
||||
}
|
@ -12,6 +12,7 @@ import com.metaweb.gridworks.expr.controls.With;
|
||||
import com.metaweb.gridworks.expr.functions.Get;
|
||||
import com.metaweb.gridworks.expr.functions.Length;
|
||||
import com.metaweb.gridworks.expr.functions.Slice;
|
||||
import com.metaweb.gridworks.expr.functions.ToDate;
|
||||
import com.metaweb.gridworks.expr.functions.ToNumber;
|
||||
import com.metaweb.gridworks.expr.functions.ToString;
|
||||
import com.metaweb.gridworks.expr.functions.arrays.Join;
|
||||
@ -21,26 +22,41 @@ import com.metaweb.gridworks.expr.functions.booleans.And;
|
||||
import com.metaweb.gridworks.expr.functions.booleans.Not;
|
||||
import com.metaweb.gridworks.expr.functions.booleans.Or;
|
||||
import com.metaweb.gridworks.expr.functions.math.Ceil;
|
||||
import com.metaweb.gridworks.expr.functions.math.Exp;
|
||||
import com.metaweb.gridworks.expr.functions.math.Floor;
|
||||
import com.metaweb.gridworks.expr.functions.math.Ln;
|
||||
import com.metaweb.gridworks.expr.functions.math.Log;
|
||||
import com.metaweb.gridworks.expr.functions.math.Max;
|
||||
import com.metaweb.gridworks.expr.functions.math.Min;
|
||||
import com.metaweb.gridworks.expr.functions.math.Mod;
|
||||
import com.metaweb.gridworks.expr.functions.math.Pow;
|
||||
import com.metaweb.gridworks.expr.functions.math.Round;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Contains;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Diff;
|
||||
import com.metaweb.gridworks.expr.functions.strings.EndsWith;
|
||||
import com.metaweb.gridworks.expr.functions.strings.IndexOf;
|
||||
import com.metaweb.gridworks.expr.functions.strings.LastIndexOf;
|
||||
import com.metaweb.gridworks.expr.functions.strings.MD5;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Partition;
|
||||
import com.metaweb.gridworks.expr.functions.strings.RPartition;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Replace;
|
||||
import com.metaweb.gridworks.expr.functions.strings.ReplaceChars;
|
||||
import com.metaweb.gridworks.expr.functions.strings.ReplaceRegexp;
|
||||
import com.metaweb.gridworks.expr.functions.strings.SHA1;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Split;
|
||||
import com.metaweb.gridworks.expr.functions.strings.SplitByCharType;
|
||||
import com.metaweb.gridworks.expr.functions.strings.StartsWith;
|
||||
import com.metaweb.gridworks.expr.functions.strings.ToLowercase;
|
||||
import com.metaweb.gridworks.expr.functions.strings.ToTitlecase;
|
||||
import com.metaweb.gridworks.expr.functions.strings.ToUppercase;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Trim;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Unescape;
|
||||
import com.metaweb.gridworks.expr.functions.strings.Unicode;
|
||||
import com.metaweb.gridworks.expr.functions.tests.IsBlank;
|
||||
import com.metaweb.gridworks.expr.functions.tests.IsNotBlank;
|
||||
import com.metaweb.gridworks.expr.functions.tests.IsNotNull;
|
||||
import com.metaweb.gridworks.expr.functions.tests.IsNull;
|
||||
import com.metaweb.gridworks.expr.functions.tests.IsNumeric;
|
||||
|
||||
public class ControlFunctionRegistry {
|
||||
|
||||
@ -83,6 +99,7 @@ public class ControlFunctionRegistry {
|
||||
static {
|
||||
registerFunction("toString", new ToString());
|
||||
registerFunction("toNumber", new ToNumber());
|
||||
registerFunction("toDate", new ToDate());
|
||||
|
||||
registerFunction("toUppercase", new ToUppercase());
|
||||
registerFunction("toLowercase", new ToLowercase());
|
||||
@ -92,8 +109,22 @@ public class ControlFunctionRegistry {
|
||||
registerFunction("slice", new Slice());
|
||||
registerFunction("substring", new Slice());
|
||||
registerFunction("replace", new Replace());
|
||||
registerFunction("replaceRegexp", new ReplaceRegexp());
|
||||
registerFunction("replaceChars", new ReplaceChars());
|
||||
registerFunction("split", new Split());
|
||||
registerFunction("splitByCharType", new SplitByCharType());
|
||||
registerFunction("partition", new Partition());
|
||||
registerFunction("rpartition", new RPartition());
|
||||
registerFunction("trim", new Trim());
|
||||
registerFunction("strip", new Trim());
|
||||
registerFunction("contains", new Contains());
|
||||
registerFunction("unescape", new Unescape());
|
||||
registerFunction("length", new Length());
|
||||
registerFunction("sha1", new SHA1());
|
||||
registerFunction("md5", new MD5());
|
||||
registerFunction("unicode", new Unicode());
|
||||
registerFunction("diff", new Diff());
|
||||
registerFunction("chomp", new Diff());
|
||||
|
||||
registerFunction("indexOf", new IndexOf());
|
||||
registerFunction("lastIndexOf", new LastIndexOf());
|
||||
@ -111,6 +142,8 @@ public class ControlFunctionRegistry {
|
||||
registerFunction("min", new Min());
|
||||
registerFunction("log", new Log());
|
||||
registerFunction("ln", new Ln());
|
||||
registerFunction("pow", new Pow());
|
||||
registerFunction("exp", new Exp());
|
||||
|
||||
registerFunction("and", new And());
|
||||
registerFunction("or", new Or());
|
||||
@ -119,6 +152,7 @@ public class ControlFunctionRegistry {
|
||||
registerFunction("isNotNull", new IsNotNull());
|
||||
registerFunction("isBlank", new IsBlank());
|
||||
registerFunction("isNotBlank", new IsNotBlank());
|
||||
registerFunction("isNumeric", new IsNumeric());
|
||||
|
||||
registerControl("if", new If());
|
||||
registerControl("with", new With());
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.metaweb.gridworks.expr.functions;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.CalendarParser;
|
||||
import com.metaweb.gridworks.expr.CalendarParserException;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class ToDate implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 || args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
if (o1 != null && o1 instanceof String) {
|
||||
boolean month_first = true;
|
||||
if (args.length == 2) {
|
||||
Object o2 = args[1];
|
||||
if (o2 != null && o2 instanceof Boolean) {
|
||||
month_first = ((Boolean) o2).booleanValue();
|
||||
}
|
||||
}
|
||||
try {
|
||||
return CalendarParser.parse((String) o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
|
||||
} catch (CalendarParserException e) {
|
||||
// do something about
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns o converted to a date object");
|
||||
writer.key("params"); writer.value("o, boolean month_first (optional)");
|
||||
writer.key("returns"); writer.value("date");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.metaweb.gridworks.expr.functions;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
@ -10,8 +12,21 @@ import com.metaweb.gridworks.expr.Function;
|
||||
public class ToString implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1) {
|
||||
return args[0] instanceof String ? args[0] : args[0].toString();
|
||||
if (args.length >= 1) {
|
||||
Object o1 = args[0];
|
||||
if (o1 != null) {
|
||||
if (o1 instanceof Calendar) {
|
||||
if (args.length == 2) {
|
||||
Object o2 = args[1];
|
||||
if (o2 != null && o2 instanceof String) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat((String) o2);
|
||||
return formatter.format(((Calendar) o1).getTime());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return (o1 instanceof String) ? o1 : o1.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -22,7 +37,7 @@ public class ToString implements Function {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns o converted to a string");
|
||||
writer.key("params"); writer.value("o");
|
||||
writer.key("params"); writer.value("o, string format (optional)");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.metaweb.gridworks.expr.functions.date;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Now implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 0) {
|
||||
return Calendar.getInstance();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns the current time");
|
||||
writer.key("returns"); writer.value("date");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.metaweb.gridworks.expr.functions.math;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Exp implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 && args[0] instanceof Number) {
|
||||
return Math.exp(((Number) args[0]).doubleValue());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns e^n");
|
||||
writer.key("params"); writer.value("number n");
|
||||
writer.key("returns"); writer.value("number");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.metaweb.gridworks.expr.functions.math;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Pow implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2 && args[0] instanceof Number && args[1] instanceof Number) {
|
||||
return Math.pow(
|
||||
((Number) args[0]).doubleValue(),
|
||||
((Number) args[1]).doubleValue()
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns a^b");
|
||||
writer.key("params"); writer.value("number a, number b");
|
||||
writer.key("returns"); writer.value("number");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Chomp implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
||||
return StringUtils.chomp((String) o1, (String) o2);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Removes separator from the end of str if it's there, otherwise leave it alone.");
|
||||
writer.key("params"); writer.value("string str, string separator");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Contains implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object s1 = args[0];
|
||||
Object s2 = args[1];
|
||||
if (s1 != null && s2 != null && s1 instanceof String && s2 instanceof String) {
|
||||
return ((String) s1).indexOf((String) s2) > -1;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns whether s contains frag");
|
||||
writer.key("params"); writer.value("string s, string frag");
|
||||
writer.key("returns"); writer.value("boolean");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Diff implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
||||
return StringUtils.difference((String) o1,(String) o2);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Compares two Strings, and returns the portion where they differ. (More precisely, return the remainder of the second String, starting from where it's different from the first.)");
|
||||
writer.key("params"); writer.value("string s, string v");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class MD5 implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 && args[0] != null) {
|
||||
Object o = args[0];
|
||||
String s = (o instanceof String) ? (String) o : o.toString();
|
||||
return DigestUtils.md5Hex(s);
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string");
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns the MD5 hash of s");
|
||||
writer.key("params"); writer.value("string s");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Partition implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
||||
String s = (String) o1;
|
||||
String frag = (String) o2;
|
||||
int index = s.indexOf(frag);
|
||||
String[] output = new String[3];
|
||||
if (index > -1) {
|
||||
output[0] = s.substring(0, index);
|
||||
output[1] = frag;
|
||||
output[2] = s.substring(index + frag.length(), s.length());
|
||||
} else {
|
||||
output[0] = s;
|
||||
output[1] = "";
|
||||
output[2] = "";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns an array of strings [a,frag,b] where a is the string part before the first occurrence of frag in s and b is what's left.");
|
||||
writer.key("params"); writer.value("string s, string frag");
|
||||
writer.key("returns"); writer.value("array");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class RPartition implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
||||
String s = (String) o1;
|
||||
String frag = (String) o2;
|
||||
int index = s.lastIndexOf(frag);
|
||||
String[] output = new String[3];
|
||||
if (index > -1) {
|
||||
output[0] = s.substring(0, index);
|
||||
output[1] = frag;
|
||||
output[2] = s.substring(index + frag.length(), s.length());
|
||||
} else {
|
||||
output[0] = s;
|
||||
output[1] = "";
|
||||
output[2] = "";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns an array of strings [a,frag,b] where a is the string part before the last occurrence of frag in s and b is what's left.");
|
||||
writer.key("params"); writer.value("string s, string frag");
|
||||
writer.key("returns"); writer.value("array");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -6,20 +6,20 @@ import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Replace implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 3) {
|
||||
Object v = args[0];
|
||||
Object find = args[1];
|
||||
Object replace = args[2];
|
||||
if (v != null && find != null && replace != null
|
||||
&& find instanceof String && replace instanceof String) {
|
||||
return (v instanceof String ? (String) v : v.toString()).replace((String) find, (String) replace);
|
||||
}
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
Object o3 = args[2];
|
||||
if (o1 != null && o2 != null && o3 != null && o2 instanceof String && o3 instanceof String) {
|
||||
String str = (o1 instanceof String) ? (String) o1 : o1.toString();
|
||||
return str.replace((String) o2, (String) o3);
|
||||
}
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings");
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
|
||||
public class ReplaceChars implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 3) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
Object o3 = args[2];
|
||||
if (o1 != null && o2 != null && o3 != null && o2 instanceof String && o3 instanceof String) {
|
||||
String str = (o1 instanceof String) ? (String) o1 : o1.toString();
|
||||
return StringUtils.replaceChars(str, (String) o2, (String) o3);
|
||||
}
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings");
|
||||
}
|
||||
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns the string obtained by replacing all chars in f with the char in s at that same position");
|
||||
writer.key("params"); writer.value("string s, string f, string r");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
|
||||
public class ReplaceRegexp implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 3) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
Object o3 = args[2];
|
||||
if (o1 != null && o2 != null && o3 != null && o2 instanceof String && o3 instanceof String) {
|
||||
String str = (o1 instanceof String) ? (String) o1 : o1.toString();
|
||||
return str.replaceAll((String) o2, (String) o3);
|
||||
}
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 3 strings");
|
||||
}
|
||||
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns the string obtained by replacing f with r in s");
|
||||
writer.key("params"); writer.value("string s, string f, string r");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class SHA1 implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 && args[0] != null) {
|
||||
Object o = args[0];
|
||||
String s = (o instanceof String) ? (String) o : o.toString();
|
||||
return DigestUtils.shaHex(s);
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string");
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns the SHA-1 hash of s");
|
||||
writer.key("params"); writer.value("string s");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
|
||||
public class SplitByCharType implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1) {
|
||||
Object o = args[0];
|
||||
if (o != null) {
|
||||
String s = (o instanceof String) ? (String) o : o.toString();
|
||||
return StringUtils.splitByCharacterType(s);
|
||||
}
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 2 strings");
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns an array of strings obtained by splitting s grouping consecutive chars by their unicode type");
|
||||
writer.key("params"); writer.value("string s");
|
||||
writer.key("returns"); writer.value("array");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Trim implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1) {
|
||||
Object s1 = args[0];
|
||||
if (s1 != null && s1 instanceof String) {
|
||||
return ((String) s1).trim();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns copy of the string, with leading and trailing whitespace omitted.");
|
||||
writer.key("params"); writer.value("string s");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class Unescape implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 2) {
|
||||
Object o1 = args[0];
|
||||
Object o2 = args[1];
|
||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
||||
String s = (String) o1;
|
||||
String mode = ((String) o2).toLowerCase();
|
||||
if ("html".equals(mode)) {
|
||||
return StringEscapeUtils.unescapeHtml(s);
|
||||
} else if ("xml".equals(mode)) {
|
||||
return StringEscapeUtils.unescapeHtml(s);
|
||||
} else if ("csv".equals(mode)) {
|
||||
return StringEscapeUtils.unescapeCsv(s);
|
||||
} else if ("javascript".equals(mode)) {
|
||||
return StringEscapeUtils.unescapeJavaScript(s);
|
||||
} else if ("url".equals(mode)) {
|
||||
try {
|
||||
return URLDecoder.decode(s,"UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {}
|
||||
} else {
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " does not recognize mode '" + mode + "'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Unescapes all escaped parts of the string depending on the given escaping mode.");
|
||||
writer.key("params"); writer.value("string s, string mode ['html','xml','csv','url','javascript']");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.metaweb.gridworks.expr.functions.strings;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
|
||||
public class Unicode implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 && args[0] != null) {
|
||||
Object o = args[0];
|
||||
String s = (o instanceof String) ? (String) o : o.toString();
|
||||
int[] output = new int[s.length()];
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
output[i] = Character.getNumericValue(s.codePointAt(i));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an argument");
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns an array of strings describing each character of s in their full unicode notation");
|
||||
writer.key("params"); writer.value("string s");
|
||||
writer.key("returns"); writer.value("string");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.metaweb.gridworks.expr.functions.tests;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.expr.ControlFunctionRegistry;
|
||||
import com.metaweb.gridworks.expr.EvalError;
|
||||
import com.metaweb.gridworks.expr.Function;
|
||||
|
||||
public class IsNumeric implements Function {
|
||||
|
||||
public Object call(Properties bindings, Object[] args) {
|
||||
if (args.length == 1 && args[0] != null) {
|
||||
Object o = args[0];
|
||||
if (o instanceof Number) return true;
|
||||
String s = (o instanceof String) ? (String) o : o.toString();
|
||||
return StringUtils.isNumeric(s);
|
||||
}
|
||||
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an argument");
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("description"); writer.value("Returns whether o can represent a number");
|
||||
writer.key("params"); writer.value("o");
|
||||
writer.key("returns"); writer.value("boolean");
|
||||
writer.endObject();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user