Merge pull request #861 from RobertZenz/patch-1
[mono.git] / mcs / class / System.Net.Http / System.Net.Http.Headers / ProductInfoHeaderValue.cs
1 //
2 // ProductInfoHeaderValue.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.Collections.Generic;
30
31 namespace System.Net.Http.Headers
32 {
33         public class ProductInfoHeaderValue : ICloneable
34         {
35                 public ProductInfoHeaderValue (ProductHeaderValue product)
36                 {
37                         if (product == null)
38                                 throw new ArgumentNullException ();
39
40                         Product = product;
41                 }
42
43                 public ProductInfoHeaderValue (string comment)
44                 {
45                         Parser.Token.CheckComment (comment);
46                         Comment = comment;
47                 }
48
49                 public ProductInfoHeaderValue (string productName, string productVersion)
50                 {
51                         Product = new ProductHeaderValue (productName, productVersion);
52                 }
53
54                 private ProductInfoHeaderValue ()
55                 {
56                 }
57
58                 public string Comment { get; private set; }
59                 public ProductHeaderValue Product { get; private set; }
60
61                 object ICloneable.Clone ()
62                 {
63                         return MemberwiseClone ();
64                 }
65
66                 public override bool Equals (object obj)
67                 {
68                         var source = obj as ProductInfoHeaderValue;
69                         if (source == null)
70                                 return false;
71
72                         return Product != null ?
73                                 Product.Equals (source.Product) :
74                                 source.Comment == Comment;
75                 }
76
77                 public override int GetHashCode ()
78                 {
79                         return Product != null ?
80                                 Product.GetHashCode () :
81                                 Comment.GetHashCode ();
82                 }
83
84                 public static ProductInfoHeaderValue Parse (string input)
85                 {
86                         ProductInfoHeaderValue value;
87                         if (TryParse (input, out value))
88                                 return value;
89
90                         throw new FormatException (input);
91                 }
92                 
93                 public static bool TryParse (string input, out ProductInfoHeaderValue parsedValue)
94                 {
95                         parsedValue = null;
96
97                         var lexer = new Lexer (input);
98                         if (!TryParseElement (lexer, out parsedValue) || parsedValue == null)
99                                 return false;
100
101                         if (lexer.Scan () != Token.Type.End) {
102                                 parsedValue = null;
103                                 return false;
104                         }       
105
106                         return true;
107                 }
108
109                 internal static bool TryParse (string input, int minimalCount, out List<ProductInfoHeaderValue> result)
110                 {
111                         var list = new List<ProductInfoHeaderValue> ();
112                         var lexer = new Lexer (input);
113                         result = null;
114
115                         while (true) {
116                                 ProductInfoHeaderValue element;
117                                 if (!TryParseElement (lexer, out element))
118                                         return false;
119
120                                 if (element == null) {
121                                         if (list != null && minimalCount <= list.Count) {
122                                                 result = list;
123                                                 return true;
124                                         }
125
126                                         return false;
127                                 }
128
129                                 list.Add (element);
130                         }
131                 }
132
133                 static bool TryParseElement (Lexer lexer, out ProductInfoHeaderValue parsedValue)
134                 {
135                         string comment;
136                         parsedValue = null;
137                         Token t;
138
139                         if (lexer.ScanCommentOptional (out comment, out t)) {
140                                 if (comment == null)
141                                         return false;
142
143                                 parsedValue = new ProductInfoHeaderValue ();
144                                 parsedValue.Comment = comment;
145                                 return true;
146                         }
147
148                         if (t == Token.Type.End)
149                                 return true;
150
151                         if (t != Token.Type.Token)
152                                 return false;
153
154                         var value = new ProductHeaderValue ();
155                         value.Name = lexer.GetStringValue (t);
156
157                         t = lexer.Scan ();
158                         if (t == Token.Type.SeparatorSlash) {
159                                 t = lexer.Scan ();
160                                 if (t != Token.Type.Token)
161                                         return false;
162
163                                 value.Version = lexer.GetStringValue (t);
164                         }
165
166                         parsedValue = new ProductInfoHeaderValue (value);
167                         return true;
168                 }
169
170                 public override string ToString ()
171                 {
172                         if (Product == null)
173                                 return Comment;
174
175                         return Product.ToString ();
176                 }
177         }
178 }