[WCF]: Implement missing WsdlImporter pieces.
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / ContentType.cs
1 //
2 // System.Net.Mime.ContentType.cs
3 //
4 // Authors:
5 //      Tim Coleman (tim@timcoleman.com)
6 //      John Luke (john.luke@gmail.com)
7 //      Atsushi Eno (atsushieno@veritas-vos-liberabit.com)
8 //
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) John Luke, 2005
11 // Copyright (C) Atsushi Eno, 2011
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Text;
38
39 namespace System.ServiceModel.Channels
40 {
41         internal class ContentType
42         {
43                 string mediaType;
44                 Dictionary<string,string> parameters = new Dictionary<string,string> ();
45
46                 public ContentType (string contentType)
47                 {
48                         if (contentType == null)
49                                 throw new ArgumentNullException ("contentType");
50                         if (contentType.Length == 0)
51                                 throw new ArgumentException ("contentType");
52
53                         string[] split = contentType.Split (';');
54                         this.MediaType = split[0].Trim ();
55                         for (int i = 1; i < split.Length; i++)
56                                 Parse (split[i].Trim ());
57                 }
58
59                 static char [] eq = new char [] { '=' };
60                 void Parse (string pair)
61                 {
62                         if (String.IsNullOrEmpty (pair))
63                                 return;
64
65                         string [] split = pair.Split (eq, 2);
66                         string key = split [0].Trim ();
67                         string val =  (split.Length > 1) ? split [1].Trim () : "";
68                         int l = val.Length;
69                         if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
70                                 val = val.Substring (1, l - 2);
71                         parameters.Add (key, val);
72                 }
73
74                 public string MediaType {
75                         get { return mediaType; }
76                         set {
77                                 if (value == null)
78                                         throw new ArgumentNullException ();
79                                 if (value.Length < 1)
80                                         throw new ArgumentException ();
81                                 if (value.IndexOf ('/') < 1)
82                                         throw new FormatException ();
83                                 if (value.IndexOf (';') != -1)
84                                         throw new FormatException ();
85                                 mediaType = value;
86                         }
87                 }
88
89                 public Dictionary<string,string> Parameters {
90                         get { return parameters; }
91                 }
92         }
93 }
94