Example #1
0
        public static ValueOrError <CrontabField> TryParse(CrontabFieldKind kind, string expression, ExceptionHandler onError)
        {
            var field = new CrontabField(CrontabFieldImpl.FromKind(kind));
            var error = field._impl.TryParse(expression, field.Accumulate, onError);

            return(error == null ? field : (ValueOrError <CrontabField>)error);
        }
Example #2
0
        public static T TryParse <T>(CrontabFieldKind kind, string expression, Func <CrontabField, T> valueSelector, Func <ExceptionProvider, T> errorSelector)
        {
            var field = new CrontabField(CrontabFieldImpl.FromKind(kind));
            var error = field._impl.TryParse(expression, field.Accumulate, null, e => e);

            return(error == null?valueSelector(field) : errorSelector(error));
        }
Example #3
0
        private static ValueOrError <CrontabSchedule> TryParse(string expression, ExceptionHandler onError)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var tokens = expression.Split(_separators, StringSplitOptions.RemoveEmptyEntries);

            if (tokens.Length != 5)
            {
                return(ErrorHandling.OnError(() => new CrontabException(string.Format(
                                                                            "'{0}' is not a valid crontab expression. It must contain at least 5 components of a schedule "
                                                                            + "(in the sequence of minutes, hours, days, months, days of week).",
                                                                            expression)), onError));
            }

            var fields = new CrontabField[5];

            for (var i = 0; i < fields.Length; i++)
            {
                var field = CrontabField.TryParse((CrontabFieldKind)i, tokens[i], onError);
                if (field.IsError)
                {
                    return(field.ErrorProvider);
                }

                fields[i] = field.Value;
            }

            return(new CrontabSchedule(fields[0], fields[1], fields[2], fields[3], fields[4]));
        }
Example #4
0
        private CrontabSchedule(
            CrontabField minutes, CrontabField hours,
            CrontabField days, CrontabField months,
            CrontabField daysOfWeek)
        {
            Debug.Assert(minutes != null);
            Debug.Assert(hours != null);
            Debug.Assert(days != null);
            Debug.Assert(months != null);
            Debug.Assert(daysOfWeek != null);

            _minutes    = minutes;
            _hours      = hours;
            _days       = days;
            _months     = months;
            _daysOfWeek = daysOfWeek;
        }
        public static T TryParse <T>(string expression, ParseOptions options, Converter <CrontabSchedule, T> valueSelector, Converter <ExceptionProvider, T> errorSelector)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var tokens = expression.Split(StringSeparatorStock.Space, StringSplitOptions.RemoveEmptyEntries);

            var includingSeconds   = options != null && options.IncludingSeconds;
            var expectedTokenCount = includingSeconds ? 6 : 5;

            if (tokens.Length < expectedTokenCount || tokens.Length > expectedTokenCount)
            {
                return(errorSelector(() =>
                {
                    var message = string.Format(
                        "'{0}' is an invalid crontab expression. It must contain {1}.",
                        expression,
                        includingSeconds
                        ? "6 components of a schedule in the sequence of seconds, minutes, hours, days, months, and days of week"
                        : "5 components of a schedule in the sequence of minutes, hours, days, months, and days of week");
                    return new CrontabException(message);
                }));
            }

            var fields = new CrontabField[6];

            var offset = includingSeconds ? 0 : 1;

            for (var i = 0; i < tokens.Length; i++)
            {
                var kind  = (CrontabFieldKind)i + offset;
                var field = CrontabField.TryParse(kind, tokens[i], v => new { ErrorProvider = (ExceptionProvider)null, Value = v },
                                                  e => new { ErrorProvider = e, Value = (CrontabField)null });
                if (field.ErrorProvider != null)
                {
                    return(errorSelector(field.ErrorProvider));
                }
                fields[i + offset] = field.Value;
            }

            return(valueSelector(new CrontabSchedule(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5])));
        }