2 // ProductInfoHeaderValue.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 namespace System.Net.Http.Headers
33 public class ProductInfoHeaderValue : ICloneable
35 public ProductInfoHeaderValue (ProductHeaderValue product)
38 throw new ArgumentNullException ();
43 public ProductInfoHeaderValue (string comment)
45 Parser.Token.CheckComment (comment);
49 public ProductInfoHeaderValue (string productName, string productVersion)
51 Product = new ProductHeaderValue (productName, productVersion);
54 private ProductInfoHeaderValue ()
58 public string Comment { get; private set; }
59 public ProductHeaderValue Product { get; private set; }
61 object ICloneable.Clone ()
63 return MemberwiseClone ();
66 public override bool Equals (object obj)
68 var source = obj as ProductInfoHeaderValue;
72 return Product != null ?
73 Product.Equals (source.Product) :
74 source.Comment == Comment;
77 public override int GetHashCode ()
79 return Product != null ?
80 Product.GetHashCode () :
81 Comment.GetHashCode ();
84 public static ProductInfoHeaderValue Parse (string input)
86 ProductInfoHeaderValue value;
87 if (TryParse (input, out value))
90 throw new FormatException (input);
93 public static bool TryParse (string input, out ProductInfoHeaderValue parsedValue)
97 var lexer = new Lexer (input);
98 if (!TryParseElement (lexer, out parsedValue) || parsedValue == null)
101 if (lexer.Scan () != Token.Type.End) {
109 internal static bool TryParse (string input, int minimalCount, out List<ProductInfoHeaderValue> result)
111 var list = new List<ProductInfoHeaderValue> ();
112 var lexer = new Lexer (input);
116 ProductInfoHeaderValue element;
117 if (!TryParseElement (lexer, out element))
120 if (element == null) {
121 if (list != null && minimalCount <= list.Count) {
133 static bool TryParseElement (Lexer lexer, out ProductInfoHeaderValue parsedValue)
139 if (lexer.ScanCommentOptional (out comment, out t)) {
143 parsedValue = new ProductInfoHeaderValue ();
144 parsedValue.Comment = comment;
148 if (t == Token.Type.End)
151 if (t != Token.Type.Token)
154 var value = new ProductHeaderValue ();
155 value.Name = lexer.GetStringValue (t);
158 if (t == Token.Type.SeparatorSlash) {
160 if (t != Token.Type.Token)
163 value.Version = lexer.GetStringValue (t);
166 parsedValue = new ProductInfoHeaderValue (value);
170 public override string ToString ()
175 return Product.ToString ();