diff --git a/main/src/com/google/refine/expr/functions/ToString.java b/main/src/com/google/refine/expr/functions/ToString.java index f7ff6ba3d..40affe591 100644 --- a/main/src/com/google/refine/expr/functions/ToString.java +++ b/main/src/com/google/refine/expr/functions/ToString.java @@ -47,7 +47,7 @@ import com.google.refine.grel.Function; public class ToString implements Function { @Override - public Object call(Properties bindings, Object[] args) { + public String call(Properties bindings, Object[] args) { if (args.length >= 1) { Object o1 = args[0]; if (o1 != null) { @@ -64,7 +64,7 @@ public class ToString implements Function { } return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime()); } else { - return (o1 instanceof String) ? o1 : o1.toString(); + return (o1 instanceof String) ? (String) o1 : o1.toString(); } } } diff --git a/main/src/com/google/refine/expr/functions/strings/Escape.java b/main/src/com/google/refine/expr/functions/strings/Escape.java index 0d5e074d5..2f26cbb74 100644 --- a/main/src/com/google/refine/expr/functions/strings/Escape.java +++ b/main/src/com/google/refine/expr/functions/strings/Escape.java @@ -42,6 +42,7 @@ import org.json.JSONException; import org.json.JSONWriter; import com.google.refine.expr.EvalError; +import com.google.refine.expr.functions.ToString; import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.Function; @@ -52,8 +53,17 @@ public class Escape implements Function { 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 s; + if (o1 instanceof String) { + s = (String) o1; + } else if (o1 == null) { + s = ""; + } else { + // Use our own ToString so that formatting is consistent + ToString toString = new ToString(); + s = toString.call(bindings,new Object[] {o1}); + } + if (o2 instanceof String) { String mode = ((String) o2).toLowerCase(); if ("html".equals(mode)) { return StringEscapeUtils.escapeHtml(s); diff --git a/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java b/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java index 63d7a7556..11615ed39 100644 --- a/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java +++ b/main/tests/server/src/com/google/refine/tests/expr/functions/strings/ToFromConversionTests.java @@ -138,4 +138,31 @@ public class ToFromConversionTests extends RefineTest { // Calendar // String } + + @Test + public void testEscape() { + Assert.assertNull(invoke("escape")); + Assert.assertEquals(invoke("escape",null,"xml"),""); + Assert.assertEquals(invoke("escape", "mystring", "html"),"mystring"); + Assert.assertEquals(invoke("escape", "mystring", "xml"),"mystring"); + Assert.assertEquals(invoke("escape", "mystring", "csv"),"mystring"); + Assert.assertEquals(invoke("escape", "mystring", "url"),"mystring"); + Assert.assertEquals(invoke("escape", "mystring", "javascript"),"mystring"); + Assert.assertEquals(invoke("escape", 1, "html"),"1"); + Assert.assertEquals(invoke("escape", 1, "xml"),"1"); + Assert.assertEquals(invoke("escape", 1, "csv"),"1"); + Assert.assertEquals(invoke("escape", 1, "url"),"1"); + Assert.assertEquals(invoke("escape", 1, "javascript"),"1"); + Assert.assertEquals(invoke("escape", Long.parseLong("1"), "html"),"1"); + Assert.assertEquals(invoke("escape", Long.parseLong("1"), "xml"),"1"); + Assert.assertEquals(invoke("escape", Long.parseLong("1"), "csv"),"1"); + Assert.assertEquals(invoke("escape", Long.parseLong("1"), "url"),"1"); + Assert.assertEquals(invoke("escape", Long.parseLong("1"), "javascript"),"1"); + Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "html"),"1.23"); + Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "xml"),"1.23"); + Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "csv"),"1.23"); + Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "url"),"1.23"); + Assert.assertEquals(invoke("escape", Double.parseDouble("1.23"), "javascript"),"1.23"); + } + }