Issue 548 - Convert non-strings to strings before escaping
git-svn-id: http://google-refine.googlecode.com/svn/trunk@2463 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
2845e8d1d2
commit
e97e7523b2
@ -47,7 +47,7 @@ import com.google.refine.grel.Function;
|
|||||||
public class ToString implements Function {
|
public class ToString implements Function {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object call(Properties bindings, Object[] args) {
|
public String call(Properties bindings, Object[] args) {
|
||||||
if (args.length >= 1) {
|
if (args.length >= 1) {
|
||||||
Object o1 = args[0];
|
Object o1 = args[0];
|
||||||
if (o1 != null) {
|
if (o1 != null) {
|
||||||
@ -64,7 +64,7 @@ public class ToString implements Function {
|
|||||||
}
|
}
|
||||||
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
|
return formatter.format(o1 instanceof Date ? ((Date) o1) : ((Calendar) o1).getTime());
|
||||||
} else {
|
} else {
|
||||||
return (o1 instanceof String) ? o1 : o1.toString();
|
return (o1 instanceof String) ? (String) o1 : o1.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ import org.json.JSONException;
|
|||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
import com.google.refine.expr.EvalError;
|
import com.google.refine.expr.EvalError;
|
||||||
|
import com.google.refine.expr.functions.ToString;
|
||||||
import com.google.refine.grel.ControlFunctionRegistry;
|
import com.google.refine.grel.ControlFunctionRegistry;
|
||||||
import com.google.refine.grel.Function;
|
import com.google.refine.grel.Function;
|
||||||
|
|
||||||
@ -52,8 +53,17 @@ public class Escape implements Function {
|
|||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
Object o1 = args[0];
|
Object o1 = args[0];
|
||||||
Object o2 = args[1];
|
Object o2 = args[1];
|
||||||
if (o1 != null && o2 != null && o1 instanceof String && o2 instanceof String) {
|
String s;
|
||||||
String s = (String) o1;
|
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();
|
String mode = ((String) o2).toLowerCase();
|
||||||
if ("html".equals(mode)) {
|
if ("html".equals(mode)) {
|
||||||
return StringEscapeUtils.escapeHtml(s);
|
return StringEscapeUtils.escapeHtml(s);
|
||||||
|
@ -138,4 +138,31 @@ public class ToFromConversionTests extends RefineTest {
|
|||||||
// Calendar
|
// Calendar
|
||||||
// String
|
// 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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user