use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.
the class Get method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 3) {
Object v = args[0];
Object from = args[1];
Object to = (args.length == 3) ? args[2] : null;
if (v != null && from != null) {
if (v instanceof HasFields && from instanceof String) {
return ((HasFields) v).getField((String) from, bindings);
} else if (v instanceof JSONObject && from instanceof String) {
try {
return ((JSONObject) v).get((String) from);
} catch (JSONException e) {
// ignore; will return null
}
} else {
if (from instanceof Number && (to == null || to instanceof Number)) {
if (v.getClass().isArray() || v instanceof List<?> || v instanceof HasFieldsList || v instanceof JSONArray) {
int length = 0;
if (v.getClass().isArray()) {
length = ((Object[]) v).length;
} else if (v instanceof HasFieldsList) {
length = ((HasFieldsList) v).length();
} else if (v instanceof JSONArray) {
length = ((JSONArray) v).length();
} else {
length = ExpressionUtils.toObjectList(v).size();
}
int start = ((Number) from).intValue();
if (start < 0) {
start = length + start;
}
start = Math.min(length, Math.max(0, start));
if (to == null) {
if (v.getClass().isArray()) {
return ((Object[]) v)[start];
} else if (v instanceof HasFieldsList) {
return ((HasFieldsList) v).get(start);
} else if (v instanceof JSONArray) {
try {
return ((JSONArray) v).get(start);
} catch (JSONException e) {
// ignore; will return null
}
} else {
return ExpressionUtils.toObjectList(v).get(start);
}
} else {
int end = ((Number) to).intValue();
if (end < 0) {
end = length + end;
}
end = Math.min(length, Math.max(start, end));
if (end > start) {
if (v.getClass().isArray()) {
Object[] a2 = new Object[end - start];
System.arraycopy(v, start, a2, 0, end - start);
return a2;
} else if (v instanceof HasFieldsList) {
return ((HasFieldsList) v).getSubList(start, end);
} else if (v instanceof JSONArray) {
JSONArray a = (JSONArray) v;
Object[] a2 = new Object[end - start];
for (int i = 0; i < a2.length; i++) {
try {
a2[i] = a.get(start + i);
} catch (JSONException e) {
// ignore
}
}
return a2;
} else {
return ExpressionUtils.toObjectList(v).subList(start, end);
}
}
}
} else {
String s = (v instanceof String) ? (String) v : v.toString();
int start = ((Number) from).intValue();
if (start < 0) {
start = s.length() + start;
}
start = Math.min(s.length(), Math.max(0, start));
if (to != null) {
int end = ((Number) to).intValue();
if (end < 0) {
end = s.length() + end;
}
end = Math.min(s.length(), Math.max(start, end));
return s.substring(start, end);
} else {
return s.substring(start, start + 1);
}
}
}
}
}
}
return null;
}
use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.
the class JythonHasFieldsWrapper method __finditem__.
@Override
public PyObject __finditem__(PyObject key) {
String k = (String) key.__tojava__(String.class);
Object v = _obj.getField(k, _bindings);
if (v != null) {
if (v instanceof PyObject) {
return (PyObject) v;
} else if (v instanceof HasFields) {
return new JythonHasFieldsWrapper((HasFields) v, _bindings);
} else if (Py.getAdapter().canAdapt(v)) {
return Py.java2py(v);
} else {
return new JythonObjectWrapper(v);
}
} else {
return null;
}
}
use of com.google.refine.expr.HasFields in project OpenRefine by OpenRefine.
the class HasField method call.
@Override
public Object call(Properties bindings, Object[] args) {
if (args.length > 1 && args.length <= 2) {
Object v = args[0];
Object f = args[1];
if (v != null && f != null && f instanceof String) {
String name = (String) f;
if (v instanceof HasFields) {
return ((HasFields) v).getField(name, bindings) != null;
} else if (v instanceof JSONObject) {
try {
return ((JSONObject) v).get(name) != null;
} catch (JSONException e) {
// ignore; will return false
}
}
}
}
return false;
}
Aggregations