Search in sources :

Example 16 with EvalError

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

the class ForRange method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object fromO = args[0].evaluate(bindings);
    Object toO = args[1].evaluate(bindings);
    Object stepO = args[2].evaluate(bindings);
    if (ExpressionUtils.isError(fromO)) {
        return fromO;
    } else if (ExpressionUtils.isError(toO)) {
        return toO;
    } else if (ExpressionUtils.isError(stepO)) {
        return stepO;
    } else if (!(fromO instanceof Number) || !(toO instanceof Number) || !(stepO instanceof Number)) {
        return new EvalError("First, second, and third arguments of forRange must all be numbers");
    }
    String indexName = ((VariableExpr) args[3]).getName();
    Object oldIndexValue = bindings.get(indexName);
    try {
        List<Object> results = new ArrayList<Object>();
        if (isIntegral((Number) fromO) && isIntegral((Number) stepO)) {
            long from = ((Number) fromO).longValue();
            long step = ((Number) stepO).longValue();
            double to = ((Number) toO).doubleValue();
            while (from < to) {
                bindings.put(indexName, from);
                Object r = args[4].evaluate(bindings);
                results.add(r);
                from += step;
            }
        } else {
            double from = ((Number) fromO).longValue();
            double step = ((Number) stepO).longValue();
            double to = ((Number) toO).doubleValue();
            while (from < to) {
                bindings.put(indexName, from);
                Object r = args[4].evaluate(bindings);
                results.add(r);
                from += step;
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old values bound to the variables, if any.
             */
        if (oldIndexValue != null) {
            bindings.put(indexName, oldIndexValue);
        } else {
            bindings.remove(indexName);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr)

Example 17 with EvalError

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

the class ToDate method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 0) {
        // missing value, can this happen?
        return null;
    }
    Object arg0 = args[0];
    String o1;
    if (arg0 instanceof Date) {
        return arg0;
    } else if (arg0 instanceof Calendar) {
        return ((Calendar) arg0).getTime();
    } else if (arg0 instanceof Long) {
        // treat integers as years
        o1 = ((Long) arg0).toString();
    } else if (arg0 instanceof String) {
        o1 = (String) arg0;
    } else {
        // ignore cell values that aren't strings
        return new EvalError("Not a String - cannot parse to date");
    }
    // "o, boolean month_first (optional)"
    if (args.length == 1 || (args.length == 2 && args[1] instanceof Boolean)) {
        boolean month_first = true;
        if (args.length == 2) {
            month_first = (Boolean) args[1];
        }
        try {
            return CalendarParser.parse(o1, (month_first) ? CalendarParser.MM_DD_YY : CalendarParser.DD_MM_YY);
        } catch (CalendarParserException e) {
            Date d = ParsingUtilities.stringToDate(o1);
            if (d != null) {
                return d;
            } else {
                try {
                    return javax.xml.bind.DatatypeConverter.parseDateTime(o1).getTime();
                } catch (IllegalArgumentException e2) {
                }
            // alternate implementation which may be useful on some JVMs?
            //                    try {
            //                        return javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(o1).toGregorianCalendar().getTime();
            //                    } catch (DatatypeConfigurationException e2) {     
            //                    }
            }
            return new EvalError("Cannot parse to date");
        }
    }
    // "o, format1, format2 (optional), ..."
    Locale locale = Locale.getDefault();
    if (args.length >= 2) {
        for (int i = 1; i < args.length; i++) {
            if (!(args[i] instanceof String)) {
                // skip formats that aren't strings
                continue;
            }
            String format = StringUtils.trim((String) args[i]);
            DateFormat formatter;
            // Attempt to parse first string as a language tag
            if (i == 1) {
                // Locale possibleLocale = Locale.forLanguageTag(format); // Java 1.7+ only
                Locale possibleLocale;
                int c = format.indexOf('_');
                if (c > 0) {
                    possibleLocale = new Locale(format.substring(0, c), format.substring(c + 1));
                } else {
                    possibleLocale = new Locale(format);
                }
                boolean valid = false;
                for (Locale l : DateFormat.getAvailableLocales()) {
                    if (l.equals(possibleLocale)) {
                        locale = possibleLocale;
                        valid = true;
                        break;
                    }
                }
                if (valid) {
                    // If we got a valid locale
                    if (args.length == 2) {
                        // No format strings to try, process using default
                        formatter = DateFormat.getDateInstance(DateFormat.DEFAULT, locale);
                        formatter.setLenient(true);
                        GregorianCalendar date = parse(o1, formatter);
                        if (date != null) {
                            return date;
                        } else {
                            return new EvalError("Unable to parse as date");
                        }
                    }
                    // Don't try to process locale string as a format string if it was valid
                    continue;
                }
            }
            try {
                formatter = new SimpleDateFormat(format, locale);
            } catch (IllegalArgumentException e) {
                return new EvalError("Unknown date format");
            }
            formatter.setLenient(true);
            GregorianCalendar date = parse(o1, formatter);
            if (date != null) {
                return date;
            }
        }
        return new EvalError("Unable to parse as date");
    }
    return null;
}
Also used : Locale(java.util.Locale) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) EvalError(com.google.refine.expr.EvalError) Date(java.util.Date) CalendarParserException(com.google.refine.expr.util.CalendarParserException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 18 with EvalError

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

the class FunctionCallExpr method evaluate.

@Override
public Object evaluate(Properties bindings) {
    Object[] args = new Object[_args.length];
    for (int i = 0; i < _args.length; i++) {
        Object v = _args[i].evaluate(bindings);
        if (ExpressionUtils.isError(v)) {
            // bubble up the error
            return v;
        }
        args[i] = v;
    }
    try {
        return _function.call(bindings, args);
    } catch (Exception e) {
        return new EvalError(e);
    }
}
Also used : EvalError(com.google.refine.expr.EvalError)

Example 19 with EvalError

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

the class Uniques method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 1) {
        Object v = args[0];
        if (v != null) {
            if (v instanceof JSONArray) {
                try {
                    v = JSONUtilities.toArray((JSONArray) v);
                } catch (JSONException e) {
                    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " fails to process a JSON array: " + e.getMessage());
                }
            }
            if (v.getClass().isArray() || v instanceof List<?>) {
                Set<Object> set = null;
                if (v.getClass().isArray()) {
                    Object[] a = (Object[]) v;
                    set = new HashSet<Object>(a.length);
                    for (Object element : a) {
                        set.add(element);
                    }
                } else {
                    set = new HashSet<Object>(ExpressionUtils.toObjectList(v));
                }
                return set.toArray();
            }
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EvalError(com.google.refine.expr.EvalError)

Example 20 with EvalError

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

the class Inc method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 3 && args[0] != null && (args[0] instanceof Calendar || args[0] instanceof Date) && args[1] != null && args[1] instanceof Number && args[2] != null && args[2] instanceof String) {
        Calendar date;
        if (args[0] instanceof Calendar) {
            // must copy so not to modify original
            date = (Calendar) ((Calendar) args[0]).clone();
        } else {
            date = Calendar.getInstance();
            date.setTime((Date) args[0]);
        }
        int amount = ((Number) args[1]).intValue();
        String unit = (String) args[2];
        date.add(getField(unit), amount);
        return date;
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a date, a number and a string");
}
Also used : Calendar(java.util.Calendar) EvalError(com.google.refine.expr.EvalError) Date(java.util.Date)

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