Merge pull request #228 from QuickJack/3e163743eda89cc8c239779a75dd245be12aee3c
[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 void CheckQuotedString (string s)
63                         {
64                                 if (s == null)
65                                         throw new ArgumentNullException ();
66
67                                 var lexer = new Lexer (s);
68                                 if (lexer.Scan () == Headers.Token.Type.QuotedString && lexer.Scan () == Headers.Token.Type.End)
69                                         return;
70
71                                 if (s.Length == 0)
72                                         throw new ArgumentException ();
73
74                                 throw new FormatException (s);
75                         }
76
77                         public static void CheckComment (string s)
78                         {
79                                 if (s == null)
80                                         throw new ArgumentNullException ();
81
82                                 var lexer = new Lexer (s);
83
84                                 string temp;
85                                 if (!lexer.ScanCommentOptional (out temp)) {
86                                         if (s.Length == 0)
87                                                 throw new ArgumentException ();
88
89                                         throw new FormatException (s);
90                                 }
91                         }
92                 }
93
94                 public static class DateTime
95                 {
96                         public static bool TryParse (string input, out DateTimeOffset result)
97                         {
98                                 return Lexer.TryGetDateValue (input, out result);
99                         }
100                 }
101
102                 public static class EmailAddress
103                 {
104                         public static bool TryParse (string input, out string result)
105                         {
106                                 try {
107                                         new MailAddress (input);
108                                         result = input;
109                                         return true;
110                                 } catch {
111                                         result = null;
112                                         return false;
113                                 }
114                         }
115                 }
116
117                 public static class Int
118                 {
119                         public static bool TryParse (string input, out int result)
120                         {
121                                 return int.TryParse (input, NumberStyles.None, CultureInfo.InvariantCulture, out result);
122                         }
123                 }
124
125                 public static class TimeSpanSeconds
126                 {
127                         public static bool TryParse (string input, out TimeSpan result)
128                         {
129                                 int value;
130                                 if (Int.TryParse (input, out value)) {
131                                         result = TimeSpan.FromSeconds (value);
132                                         return true;
133                                 }
134
135                                 result = TimeSpan.Zero;
136                                 return false;
137                         }
138                 }
139
140                 public static class Uri
141                 {
142                         public static bool TryParse (string input, out System.Uri result)
143                         {
144                                 return System.Uri.TryCreate ("http://" + input + "/", UriKind.Absolute, out result);
145                         }
146
147                         public static void Check (string s)
148                         {
149                                 if (s == null)
150                                         throw new ArgumentNullException ();
151
152                                 System.Uri uri;
153                                 if (!TryParse (s, out uri)) {
154                                         if (s.Length == 0)
155                                                 throw new ArgumentException ();
156
157                                         throw new FormatException (s);
158                                 }
159                         }
160                 }
161         }
162 }