The Labs \ Source Viewer \ SSCLI \ System.Configuration \ RegexStringValidator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RegexStringValidator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Collections;
  17. using System.IO;
  18. using System.Reflection;
  19. using System.Security.Permissions;
  20. using System.Xml;
  21. using System.Collections.Specialized;
  22. using System.Globalization;
  23. using System.ComponentModel;
  24. using System.Security;
  25. using System.Text;
  26. using System.Text.RegularExpressions;
  27. namespace System.Configuration
  28. {
  29.    
  30.     public class RegexStringValidator : ConfigurationValidatorBase
  31.     {
  32.         private string _expression;
  33.         private Regex _regex;
  34.        
  35.         public RegexStringValidator(string regex)
  36.         {
  37.             if (string.IsNullOrEmpty(regex)) {
  38.                 throw ExceptionUtil.ParameterNullOrEmpty("regex");
  39.             }
  40.            
  41.             _expression = regex;
  42.             _regex = new Regex(regex, RegexOptions.Compiled);
  43.         }
  44.        
  45.         public override bool CanValidate(Type type)
  46.         {
  47.             return (type == typeof(string));
  48.         }
  49.        
  50.         public override void Validate(object value)
  51.         {
  52.             ValidatorUtils.HelperParamValidation(value, typeof(string));
  53.            
  54.             if (value == null) {
  55.                 return;
  56.             }
  57.            
  58.             Match match = _regex.Match((string)value);
  59.            
  60.             if (!match.Success) {
  61.                 throw new ArgumentException(SR.GetString(SR.Regex_validator_error, _expression));
  62.             }
  63.         }
  64.     }
  65. }

Developer Fusion