magnusbakken

Untitled

Sep 25th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1.     /// <summary>
  2.     /// Defines operations related to validating cron expressions.
  3.     /// </summary>
  4.     public interface ICronValidator
  5.     {
  6.         /// <summary>
  7.         /// Validates the given cron expression.
  8.         /// </summary>
  9.         /// <param name="cronExpression">The cron expression to validate.</param>
  10.         /// <param name="errorMessage">An error message that will be set if the expression is invalid.</param>
  11.         /// <returns>True if the cron expression is valid, otherwise false.</returns>
  12.         bool ValidateCronExpression(string cronExpression, out string errorMessage);
  13.     }
  14.  
  15.     /// <summary>
  16.     /// A class that validates cron expressions using the cron expression API of Quartz.
  17.     /// </summary>
  18.     public class QuartzCronValidator : ICronValidator
  19.     {
  20.         /// <summary>
  21.         /// Initializes a new instance of the <see cref="QuartzCronValidator"/> class.
  22.         /// </summary>
  23.         public QuartzCronValidator()
  24.         {
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Validates the given cron expression.
  29.         /// </summary>
  30.         /// <param name="cronExpression">The cron expression to validate.</param>
  31.         /// <param name="errorMessage">An error message that will be set if the expression is invalid.</param>
  32.         /// <returns>True if the cron expression is valid, otherwise false.</returns>
  33.         public bool ValidateCronExpression(string cronExpression, out string errorMessage)
  34.         {
  35.             // There doesn't appear to be any other way to do this with Quartz.NET.
  36.             try
  37.             {
  38.                 new CronExpression(cronExpression);
  39.             }
  40.             catch (FormatException ex)
  41.             {
  42.                 errorMessage = ex.Message;
  43.                 return false;
  44.             }
  45.  
  46.             errorMessage = null;
  47.             return true;
  48.         }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment