Search in sources :

Example 21 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class Diff method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length >= 2) {
        Object o1 = args[0];
        Object o2 = args[1];
        if (o1 != null && o2 != null) {
            if (args.length == 2 && o1 instanceof String && o2 instanceof String) {
                return StringUtils.difference((String) o1, (String) o2);
            } else if ((o1 instanceof Date || o1 instanceof Calendar) && args.length == 3) {
                Object o3 = args[2];
                if (o3 != null && o3 instanceof String) {
                    try {
                        String unit = ((String) o3).toLowerCase();
                        Date c1 = (o1 instanceof Date) ? (Date) o1 : ((Calendar) o1).getTime();
                        Date c2;
                        if (o2 instanceof Date) {
                            c2 = (Date) o2;
                        } else if (o2 instanceof Calendar) {
                            c2 = ((Calendar) o2).getTime();
                        } else {
                            c2 = CalendarParser.parse((o2 instanceof String) ? (String) o2 : o2.toString()).getTime();
                        }
                        long delta = (c1.getTime() - c2.getTime()) / 1000;
                        if ("seconds".equals(unit)) {
                            return delta;
                        }
                        delta /= 60;
                        if ("minutes".equals(unit)) {
                            return delta;
                        }
                        delta /= 60;
                        if ("hours".equals(unit)) {
                            return delta;
                        }
                        long days = delta / 24;
                        if ("days".equals(unit)) {
                            return days;
                        }
                        if ("weeks".equals(unit)) {
                            return days / 7;
                        }
                        if ("months".equals(unit)) {
                            return days / 30;
                        }
                        if ("years".equals(unit)) {
                            return days / 365;
                        }
                        return new EvalError("Unknown time unit " + unit);
                    } catch (CalendarParserException e) {
                        return new EvalError(e);
                    }
                }
            }
        }
    }
    return new EvalError("Unexpected arguments - expecting either 2 strings or 2 dates and a unit string");
}
Also used : Calendar(java.util.Calendar) EvalError(com.google.refine.expr.EvalError) Date(java.util.Date) CalendarParserException(com.google.refine.expr.util.CalendarParserException)

Example 22 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class Match method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 2) {
        Object s = args[0];
        Object p = args[1];
        if (s != null && p != null && (p instanceof String || p instanceof Pattern)) {
            Pattern pattern = (p instanceof String) ? Pattern.compile((String) p) : (Pattern) p;
            Matcher matcher = pattern.matcher(s.toString());
            if (matcher.matches()) {
                int count = matcher.groupCount();
                String[] groups = new String[count];
                for (int i = 0; i < count; i++) {
                    groups[i] = matcher.group(i + 1);
                }
                return groups;
            } else {
                return null;
            }
        }
        return null;
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string or a regexp");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) EvalError(com.google.refine.expr.EvalError)

Example 23 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class Reinterpret method reinterpret.

private Object reinterpret(String str, String decoder, String encoder) {
    String result = null;
    byte[] bytes;
    if (decoder == null || decoder.isEmpty()) {
        bytes = str.getBytes();
    } else {
        try {
            bytes = str.getBytes(decoder);
        } catch (UnsupportedEncodingException e) {
            return new EvalError(ControlFunctionRegistry.getFunctionName(this) + ": source encoding '" + decoder + "' is not available or recognized.");
        }
    }
    try {
        if (encoder == null || encoder.isEmpty()) {
            // system default encoding
            result = new String(bytes);
        } else {
            result = new String(bytes, encoder);
        }
    } catch (UnsupportedEncodingException e) {
        return new EvalError(ControlFunctionRegistry.getFunctionName(this) + ": encoding '" + encoder + "' is not available or recognized.");
    }
    return result;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) EvalError(com.google.refine.expr.EvalError)

Aggregations

EvalError (com.google.refine.expr.EvalError)23 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 VariableExpr (com.google.refine.grel.ast.VariableExpr)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 Date (java.util.Date)4 Evaluable (com.google.refine.expr.Evaluable)3 ParsingException (com.google.refine.expr.ParsingException)3 Project (com.google.refine.model.Project)3 WrappedCell (com.google.refine.expr.WrappedCell)2 CalendarParserException (com.google.refine.expr.util.CalendarParserException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Element (org.jsoup.nodes.Element)2 CSVParser (au.com.bytecode.opencsv.CSVParser)1 ProjectJoin (com.google.refine.InterProjectModel.ProjectJoin)1 ProjectMetadata (com.google.refine.ProjectMetadata)1 Engine (com.google.refine.browsing.Engine)1 ExpressionNominalValueGrouper (com.google.refine.browsing.util.ExpressionNominalValueGrouper)1