New test.
[mono.git] / mcs / class / System / System.Net.Mime / 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 //
8 // Copyright (C) Tim Coleman, 2004
9 // Copyright (C) John Luke, 2005
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Text;
38
39 namespace System.Net.Mime {
40         public class ContentType
41         {
42                 #region Fields
43
44                 string mediaType;
45                 StringDictionary parameters = new StringDictionary ();
46
47                 #endregion // Fields
48
49                 #region Constructors
50
51                 public ContentType ()
52                 {
53                         mediaType = "application/octet-stream";
54                 }
55         
56                 public ContentType (string contentType)
57                 {
58                         if (contentType == null)
59                                 throw new ArgumentNullException ("contentType");
60                         if (contentType.Length < 1)
61                                 throw new ArgumentException ("contentType");
62
63                         try {
64                                 int index = contentType.IndexOf (";");
65                                 if (index > 0) {
66                                         string[] split = contentType.Split (';');
67                                         this.mediaType = split[0].Trim ();
68                                         for (int i = 1; i < split.Length; i++)
69                                         {
70                                                 Parse (split[i]);
71                                         }
72                                 }
73                                 else {
74                                         this.mediaType = contentType.Trim ();
75                                 }
76                         } catch {
77                                 throw new FormatException ();
78                         }
79                 }
80
81                 // parse key=value pairs like:
82                 // "charset=us-ascii"
83                 void Parse (string pair)
84                 {
85                         if (pair == null || pair.Length < 1)
86                                 return;
87
88                         string[] split = pair.Split ('=');
89                         if (split.Length == 2) {
90                                 switch (split[0].Trim ()) {
91                                         case "boundary":
92                                         case "charset":
93                                         case "name":
94                                                 parameters.Add (split[0].Trim (), split[1].Trim ());
95                                                 break;
96                                         default:
97                                                 // apparently parameters must go through Parameters.Add
98                                                 throw new FormatException ("invalid content-type format");
99                                 }
100                         }
101                 }
102
103                 #endregion // Constructors
104
105                 #region Properties
106
107                 public string Boundary {
108                         get { return parameters["boundary"]; }
109                         set { parameters["boundary"] = value; }
110                 }
111
112                 public string CharSet {
113                         get { return parameters["charset"]; }
114                         set { parameters["charset"] = value; }
115                 }
116
117                 public string MediaType {
118                         get { return mediaType; }
119                         set {
120                                 if (value == null)
121                                         throw new ArgumentNullException ();
122                                 if (value.Length < 1)
123                                         throw new ArgumentException ();
124                                 if (value.IndexOf (';') != -1)
125                                         throw new FormatException ();
126                                 mediaType = value;
127                         }
128                 }
129
130                 public string Name {
131                         get { return parameters["name"]; }
132                         set { parameters["name"] = value; }
133                 }
134
135                 public StringDictionary Parameters {
136                         get { return parameters; }
137                 }
138
139                 #endregion // Properties
140
141                 #region Methods
142
143                 public override bool Equals (object obj)
144                 {
145                         return Equals (obj as ContentType);
146                 }
147
148                 bool Equals (ContentType other)
149                 {
150                         return other != null && ToString () == other.ToString ();
151                 }
152                 
153                 public override int GetHashCode ()
154                 {
155                         return ToString ().GetHashCode ();
156                 }
157
158                 public override string ToString ()
159                 {
160                         StringBuilder sb = new StringBuilder ();
161                         sb.Append (MediaType);
162                         if (Parameters != null && Parameters.Count > 0) {
163                                 foreach (DictionaryEntry pair in parameters)
164                                 {
165                                         if (pair.Value != null && pair.Value.ToString ().Length > 0) {  
166                                                 sb.Append ("; ");
167                                                 sb.Append (pair.Key);
168                                                 sb.Append ("=");
169                                                 sb.Append (pair.Value);
170                                         }
171                                 }
172                         }
173                         return sb.ToString ();
174                 }
175
176                 #endregion // Methods
177         }
178 }
179
180 #endif // NET_2_0