copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System.IO / CheckArgument.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.IO.CheckArgument.cs 
4 //
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
6 // 
7 // Author:         Jim Richardson, develop@wtfo-guru.com
8 // Created:        Saturday, August 25, 2001 
9 //
10 // NOTE: All contributors can freely add to this class or make modifications
11 //       that do not break existing usage of methods 
12 //------------------------------------------------------------------------------
13
14 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37
38 using System;
39 using System.IO;
40
41 namespace System.IO
42 {
43         /// <summary>
44         /// A utility class to assist with various argument validations in System.IO
45         /// </summary>
46         internal sealed class CheckArgument
47         {
48                 /// <summary>
49                 /// Generates and exception if arg contains whitepace only
50                 /// </summary>
51                 public static void WhitespaceOnly (string arg, string desc)
52                 {
53                         if (arg != null && arg.Length > 0)
54                         {
55                                 string temp = arg.Trim ();
56                                 if (temp.Length == 0)
57                                 {
58                                         throw new ArgumentException (desc);
59                                 }
60                         }
61                 }
62                 
63                 /// <summary>
64                 /// Generates and exception if arg contains whitepace only
65                 /// </summary>
66                 public static void WhitespaceOnly (string arg)
67                 {
68                         WhitespaceOnly (arg, "Argument string consists of whitespace characters only.");
69                 }
70                 
71                 /// <summary>
72                 /// Generates and exception if arg is empty
73                 /// </summary>
74                 public static void Empty (string arg, string desc)
75                 {
76                         if (arg != null && arg.Length == 0)
77                         {
78                                 throw new ArgumentException (desc);
79                         }
80                 }
81                 
82                 /// <summary>
83                 /// Generates and exception if arg is empty
84                 /// </summary>
85                 public static void Empty (string arg)
86                 {
87                         Empty (arg, "Argument string is empty.");
88                 }
89                 
90                 /// <summary>
91                 /// Generates and exception if arg is null
92                 /// </summary>
93                 public static void Null (Object arg, string desc)
94                 {
95                         if (arg == null)
96                         {
97                                 throw new ArgumentNullException (desc);
98                         }
99                 }
100                 
101                 /// <summary>
102                 /// Generates and exception if arg is null
103                 /// </summary>
104                 public static void Null (Object arg)
105                 {
106                         if (arg == null)
107                         {
108                                 throw new ArgumentNullException ();
109                         }
110                 }
111                 
112                 /// <summary>
113                 /// Generates and exception if path contains invalid path characters
114                 /// </summary>
115                 public static void PathChars (string path, string desc)
116                 {
117                         if (path != null)
118                         {
119                                 if (path.IndexOfAny (System.IO.Path.InvalidPathChars) > -1)
120                                 {
121                                         throw new ArgumentException (desc);
122                                 }
123                         }
124                 }
125                 
126                 /// <summary>
127                 /// Generates and exception if path contains invalid path characters
128                 /// </summary>
129                 public static void PathChars (string path)
130                 {
131                         PathChars (path, "Path contains invalid characters");
132                 }
133                 
134                 /// <summary>
135                 /// Generates and exception if path too long
136                 /// </summary>
137                 [MonoTODO]
138                 public static void PathLength (string path, string desc)
139                 {
140                         //TODO: find out how long is too long
141                 }
142                 
143                 /// <summary>
144                 /// Generates and exception if path too long
145                 /// </summary>
146                 public static void PathLength (string path)
147                 {
148                         PathLength (path, "Path is too long");
149                 }
150                 
151                 /// <summary>
152                 /// Generates and exception if path is illegal
153                 /// </summary>
154                 public static void Path (string path, bool bAllowNull, bool bLength)
155                 {
156                         if (path != null) //allow null
157                         {
158                                 Empty (path, "Path cannot be the empty string");        // path can't be empty
159                                 WhitespaceOnly (path, "Path cannot be all whitespace"); // path can't be all whitespace
160                                 PathChars (path);       // path can't contain invalid characters
161                                 if (bLength)
162                                 {
163                                         PathLength ("Path too long");
164                                 }
165                         }
166                         else if (!bAllowNull)
167                         {
168                                 throw new ArgumentNullException ("Parameter name: path");
169                         }
170                 }
171                 
172                 /// <summary>
173                 /// Generates and exception if path is illegal
174                 /// </summary>
175                 public static void Path (string path, bool bAllowNull)
176                 {
177                         Path (path, bAllowNull, false);
178                 }
179                 
180                 /// <summary>
181                 /// Generates and exception if path is illegal
182                 /// </summary>
183                 public static void Path (string path)
184                 {
185                         Path (path, false, false);
186                 }
187
188         }
189 }       // namespace System.IO.Private