Bug 15572. Lookup KnownTypeCollection element types in MSSimpleNamespace
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / Parser.cs
1 //
2 // Parser.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Net.Mail;
30 using System.Globalization;
31
32 namespace System.Net.Http.Headers
33 {
34         static class Parser
35         {
36                 public static class Token
37                 {
38                         public static bool TryParse (string input, out string result)
39                         {
40                                 if (input != null && Lexer.IsValidToken (input)) {
41                                         result = input;
42                                         return true;
43                                 }
44
45                                 result = null;
46                                 return false;
47                         }
48
49                         public static void Check (string s)
50                         {
51                                 if (s == null)
52                                         throw new ArgumentNullException ();
53
54                                 if (!Lexer.IsValidToken (s)) {
55                                         if (s.Length == 0)
56                                                 throw new ArgumentException ();
57
58                                         throw new FormatException (s);
59                                 }
60                         }
61
62                         public static bool TryCheck (string s)
63                         {
64                                 if (s == null)
65                                         return false;
66
67                                 if (!Lexer.IsValidToken (s)) {
68                                         if (s.Length == 0)
69                                                 return false;
70
71                                         return false;
72                                 }
73
74                                 return true;
75                         }
76
77                         public static void CheckQuotedString (string s)
78                         {
79                                 if (s == null)
80                                         throw new ArgumentNullException ();
81
82                                 var lexer = new Lexer (s);
83                                 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
84                                         return;
85
86                                 if (s.Length == 0)
87                                         throw new ArgumentException ();
88
89                                 throw new FormatException (s);
90                         }
91
92                         public static void CheckComment (string s)
93                         {
94                                 if (s == null)
95                                         throw new ArgumentNullException ();
96
97                                 var lexer = new Lexer (s);
98
99                                 string temp;
100                                 if (!lexer.ScanCommentOptional (out temp)) {
101                                         if (s.Length == 0)
102                                                 throw new ArgumentException ();
103
104                                         throw new FormatException (s);
105                                 }
106                         }
107                 }
108
109                 public static class DateTime
110                 {
111                         public new static readonly Func<object, string> ToString = l => ((DateTimeOffset) l).ToString ("r", CultureInfo.InvariantCulture);
112                         
113                         public static bool TryParse (string input, out DateTimeOffset result)
114                         {
115                                 return Lexer.TryGetDateValue (input, out result);
116                         }
117                 }
118
119                 public static class EmailAddress
120                 {
121                         public static bool TryParse (string input, out string result)
122                         {
123                                 try {
124                                         new MailAddress (input);
125                                         result = input;
126                                         return true;
127                                 } catch {
128                                         result = null;
129                                         return false;
130                                 }
131                         }
132                 }
133
134                 public static class Int
135                 {
136                         public static bool TryParse (string input, out int result)
137                         {
138                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
139                         }
140                 }
141
142                 public static class Long
143                 {
144                         public static bool TryParse (string input, out long result)
145                         {
146                                 return long.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
147                         }
148                 }
149
150                 public static class MD5
151                 {
152                         public static bool TryParse (string input, out byte[] result)
153                         {
154                                 try {
155                                         result = Convert.FromBase64String (input);
156                                         return true;
157                                 } catch {
158                                         result = null;
159                                         return false;
160                                 }
161                         }
162                 }
163
164                 public static class TimeSpanSeconds
165                 {
166                         public static bool TryParse (string input, out TimeSpan result)
167                         {
168                                 int value;
169                                 if (Int.TryParse (input, out value)) {
170                                         result = TimeSpan.FromSeconds (value);
171                                         return true;
172                                 }
173
174                                 result = TimeSpan.Zero;
175                                 return false;
176                         }
177                 }
178
179                 public static class Uri
180                 {
181                         public static bool TryParse (string input, out System.Uri result)
182                         {
183                                 return System.Uri.TryCreate (input, UriKind.RelativeOrAbsolute, out result);
184                         }
185
186                         public static void Check (string s)
187                         {
188                                 if (s == null)
189                                         throw new ArgumentNullException ();
190
191                                 System.Uri uri;
192                                 if (!TryParse (s, out uri)) {
193                                         if (s.Length == 0)
194                                                 throw new ArgumentException ();
195
196                                         throw new FormatException (s);
197                                 }
198                         }
199                 }
200         }
201 }