use of org.javarosa.xpath.IExprDataType in project javarosa by opendatakit.
the class XPathEvalTest method getFunctionHandlers.
private EvaluationContext getFunctionHandlers() {
EvaluationContext ec = new EvaluationContext(null);
final Class[][] allPrototypes = { { Double.class, Double.class }, { Double.class }, { String.class, String.class }, { Double.class, String.class, Boolean.class }, { Boolean.class }, { Boolean.class, Double.class, String.class, Date.class, CustomType.class } };
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "testfunc";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return TRUE;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "regex";
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
System.out.println("EVAL REGEX TESTS:");
for (Object arg : args) {
System.out.println("REGEX ARGS: " + arg.toString());
}
// String.re args[0].
return TRUE;
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[2]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "add";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return (Double) args[0] + (Double) args[1];
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "proto";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[0]);
p.add(allPrototypes[1]);
p.add(allPrototypes[2]);
p.add(allPrototypes[3]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return printArgs(args);
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "raw";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[3]);
return p;
}
@Override
public boolean rawArgs() {
return true;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return printArgs(args);
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "null-proto";
}
@Override
public List<Class[]> getPrototypes() {
return null;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return FALSE;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "concat";
}
@Override
public List<Class[]> getPrototypes() {
return new ArrayList<Class[]>();
}
@Override
public boolean rawArgs() {
return true;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++) sb.append(XPathFuncExpr.toString(args[i]));
return sb.toString();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "convertible";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return new IExprDataType() {
@Override
public Boolean toBoolean() {
return TRUE;
}
@Override
public Double toNumeric() {
return 5.0;
}
public String toString() {
return "hi";
}
};
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "inconvertible";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return new Object();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "get-custom";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[4]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return (Boolean) args[0] ? new CustomSubType() : new CustomType();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "check-types";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[5]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
if (args.length != 5 || !(args[0] instanceof Boolean) || !(args[1] instanceof Double) || !(args[2] instanceof String) || !(args[3] instanceof Date) || !(args[4] instanceof CustomType))
fail("Types in custom function handler not converted properly/prototype not matched properly");
return TRUE;
}
});
return ec;
}
Aggregations