Merge pull request #2080 from upsilon/fix-point-constructor
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / NullableStringValidator.cs
1 //
2 // NullableStringValidator.cs
3 //
4 // Authors:
5 //  Atsushi Enomoto (atsushi@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
28 //
29
30 // copied StringValidator.cs and changed class name and just one line 
31 // in Validate() (see there).
32
33
34 namespace System.Configuration
35 {
36         internal class NullableStringValidator: ConfigurationValidatorBase
37         {
38                 char[] invalidCharacters;
39                 int maxLength;
40                 int minLength;
41                 
42                 public NullableStringValidator (int minLength)
43                 {
44                         this.minLength = minLength;
45                         maxLength = int.MaxValue;
46                 }
47                 
48                 public NullableStringValidator (int minLength, int maxLength)
49                 {
50                         this.minLength = minLength;
51                         this.maxLength = maxLength;
52                 }
53                 
54                 public NullableStringValidator (int minLength, int maxLength, string invalidCharacters)
55                 {
56                         this.minLength = minLength;
57                         this.maxLength = maxLength;
58                         if (invalidCharacters != null)
59                                 this.invalidCharacters = invalidCharacters.ToCharArray ();
60                 }
61                 
62                 public override bool CanValidate (Type type)
63                 {
64                         return type == typeof(string);
65                 }
66
67                 public override void Validate (object value)
68                 {
69                         // This is the only difference from StringValidator:
70                         // null value is always allowed.
71                         if (value == null)
72                                 return;
73
74                         string s = (string) value;
75                         if (s == null || s.Length < minLength)
76                                 throw new ArgumentException ("The string must be at least " + minLength + " characters long.");
77                         if (s.Length > maxLength)
78                                 throw new ArgumentException ("The string must be no more than " + maxLength + " characters long.");
79                         if (invalidCharacters != null) {
80                                 int i = s.IndexOfAny (invalidCharacters);
81                                 if (i != -1)
82                                         throw new ArgumentException (String.Format ("The string cannot contain any of the following characters: '{0}'.", invalidCharacters));
83                         }
84                 }
85         }
86 }
87