Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / StringWithQualityHeaderValue.cs
1 //
2 // StringWithQualityHeaderValue.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.Globalization;
30 namespace System.Net.Http.Headers
31 {
32         public class StringWithQualityHeaderValue : ICloneable
33         {
34                 public StringWithQualityHeaderValue (string value)
35                 {
36                         Parser.Token.Check (value);
37                         this.Value = value;
38                 }
39
40                 public StringWithQualityHeaderValue (string value, double quality)
41                         : this (value)
42                 {
43                         if (quality < 0 || quality > 1)
44                                 throw new ArgumentOutOfRangeException ("quality");
45
46                         Quality = quality;
47                 }
48
49                 private StringWithQualityHeaderValue ()
50                 {
51                 }
52
53                 public double? Quality { get; private set; }
54                 public string Value { get; private set; }
55
56                 object ICloneable.Clone ()
57                 {
58                         return MemberwiseClone ();
59                 }
60
61                 public override bool Equals (object obj)
62                 {
63                         var source = obj as StringWithQualityHeaderValue;
64                         return source != null &&
65                                 string.Equals (source.Value, Value, StringComparison.OrdinalIgnoreCase) &&
66                                 source.Quality == Quality;
67                 }
68
69                 public override int GetHashCode ()
70                 {
71                         return Value.ToLowerInvariant ().GetHashCode () ^ Quality.GetHashCode ();
72                 }
73
74                 public static StringWithQualityHeaderValue Parse (string input)
75                 {
76                         StringWithQualityHeaderValue value;
77                         if (TryParse (input, out value))
78                                 return value;
79
80                         throw new FormatException (input);
81                 }
82                 
83                 public static bool TryParse (string input, out StringWithQualityHeaderValue parsedValue)
84                 {
85                         parsedValue = null;
86
87                         var lexer = new Lexer (input);
88                         var t = lexer.Scan ();
89                         if (t != Token.Type.Token)
90                                 return false;
91
92                         var value = new StringWithQualityHeaderValue ();
93                         value.Value = lexer.GetStringValue (t);
94
95                         t = lexer.Scan ();
96                         if (t == Token.Type.SeparatorSemicolon) {
97                                 t = lexer.Scan ();
98                                 if (t != Token.Type.Token)
99                                         return false;
100
101                                 var s = lexer.GetStringValue (t);
102                                 if (s != "q" && s != "Q")
103                                         return false;
104
105                                 t = lexer.Scan ();
106                                 if (t != Token.Type.SeparatorEqual)
107                                         return false;
108
109                                 t = lexer.Scan ();
110
111                                 double d;
112                                 if (!lexer.TryGetDoubleValue (t, out d))
113                                         return false;
114
115                                 if (d > 1)
116                                         return false;
117
118                                 value.Quality = d;
119
120                                 t = lexer.Scan ();
121                         }
122
123                         if (t != Token.Type.End)
124                                 return false;
125
126                         parsedValue = value;
127                         return true;
128                 }
129
130                 public override string ToString ()
131                 {
132                         if (Quality != null)
133                                 return Value + "; q=" + Quality.Value.ToString ("0.0##", CultureInfo.InvariantCulture);
134
135                         return Value;
136                 }
137         }
138 }