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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="StringValidator.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 StringValidator : ConfigurationValidatorBase
  31.     {
  32.         private int _minLength;
  33.         private int _maxLength;
  34.         private string _invalidChars;
  35.        
  36.         public StringValidator(int minLength) : this(minLength, int.MaxValue, null)
  37.         {
  38.         }
  39.        
  40.         public StringValidator(int minLength, int maxLength) : this(minLength, maxLength, null)
  41.         {
  42.         }
  43.        
  44.         public StringValidator(int minLength, int maxLength, string invalidCharacters)
  45.         {
  46.             _minLength = minLength;
  47.             _maxLength = maxLength;
  48.             _invalidChars = invalidCharacters;
  49.         }
  50.        
  51.         public override bool CanValidate(Type type)
  52.         {
  53.             return (type == typeof(string));
  54.         }
  55.        
  56.         public override void Validate(object value)
  57.         {
  58.             ValidatorUtils.HelperParamValidation(value, typeof(string));
  59.            
  60.             string data = value as string;
  61.             int len = (data == null ? 0 : data.Length);
  62.            
  63.             if (len < _minLength) {
  64.                 throw new ArgumentException(SR.GetString(SR.Validator_string_min_length, _minLength));
  65.             }
  66.            
  67.             if (len > _maxLength) {
  68.                 throw new ArgumentException(SR.GetString(SR.Validator_string_max_length, _maxLength));
  69.             }
  70.            
  71.             // Check if the string contains any invalid characters
  72.             if ((len > 0) && (_invalidChars != null) && (_invalidChars.Length > 0)) {
  73.                 char[] array = new char[_invalidChars.Length];
  74.                
  75.                 _invalidChars.CopyTo(0, array, 0, _invalidChars.Length);
  76.                
  77.                 if (data.IndexOfAny(array) != -1) {
  78.                     throw new ArgumentException(SR.GetString(SR.Validator_string_invalid_chars, _invalidChars));
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }

Developer Fusion