importing messaging-2008 branch to trunk [continued]
[mono.git] / mcs / class / System.Web / System.Web.UI / AttributeCollection.cs
1 //
2 // System.Web.UI.AttributeCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Globalization;
33 using System.Security.Permissions;
34
35 namespace System.Web.UI {
36
37         // CAS - no InheritanceDemand here as the class is sealed
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public sealed class AttributeCollection
40         {
41                 StateBag bag;
42                 CssStyleCollection styleCollection;
43                 internal const string StyleAttribute = "style";
44                 
45                 public AttributeCollection (StateBag bag)
46                 {
47                         this.bag = bag;
48                 }
49
50                 public override bool Equals (object obj) 
51                 {
52                         AttributeCollection other = obj as AttributeCollection;
53                         if (other == null) {
54                                 return false;
55                         }
56
57                         if (Count != other.Count) {
58                                 return false;
59                         }
60
61                         foreach (string key in Keys) {
62                                 if (0 == String.CompareOrdinal (key, StyleAttribute)) {
63                                         continue;
64                                 }
65                                 if (0 == String.CompareOrdinal (other [key], this [key])) {
66                                         return false;
67                                 }
68                         }
69
70                         if ((styleCollection == null && other.styleCollection != null) ||
71                                 (styleCollection != null && other.styleCollection == null)) {
72                                 return false;
73                         }
74                         else if (styleCollection != null){
75                                 // other.styleCollection != null too
76                                 if (styleCollection.Count != other.styleCollection.Count){
77                                         return false;
78                                 }
79                                 foreach (string styleKey in styleCollection.Keys){
80                                         if (0 == String.CompareOrdinal(styleCollection [styleKey], other.styleCollection [styleKey])) {
81                                                 return false;
82                                         }
83                                 }
84                         }
85
86                         return true;
87                 }
88
89                 public override int GetHashCode () 
90                 {
91                         int hashValue = 0;
92                         
93                         foreach (string key in Keys) {
94                                 if (key == StyleAttribute) {
95                                         continue;
96                                 }
97                                 hashValue ^= key.GetHashCode ();
98                                 string value = this [key];
99                                 if (value != null) {
100                                         hashValue ^= value.GetHashCode ();
101                                 }
102                         }
103
104                         if (styleCollection != null) {
105                                 foreach (string styleKey in styleCollection.Keys) {
106                                         hashValue ^= styleCollection [styleKey].GetHashCode ();
107                                         string styleValue = styleCollection [styleKey];
108                                         if (styleValue != null) {
109                                                 hashValue ^= styleValue.GetHashCode ();
110                                         }
111                                 }
112                         }
113
114                         return hashValue;
115                 }
116
117                 public int Count {
118                         get { return bag.Count; }
119                 }
120
121                 public CssStyleCollection CssStyle {
122                         get {
123                                 if (styleCollection == null)
124                                         styleCollection = new CssStyleCollection (bag);
125                                 return styleCollection;
126                         }
127                 }
128
129                 public string this [string key] {
130                         get { return bag [key] as string; }
131
132                         set {
133                                 Add (key, value);
134                         }
135                 }
136
137                 public ICollection Keys {
138                         get { return bag.Keys; }
139                 }
140
141                 public void Add (string key, string value)
142                 {
143                         if (0 == String.Compare (key, StyleAttribute, true, CultureInfo.InvariantCulture)) {
144                                 CssStyle.Value = value;
145                                 return;
146                         }
147                         bag.Add (key, value);
148                 }
149
150                 public void AddAttributes (HtmlTextWriter writer)
151                 {
152                         foreach (string key in bag.Keys) {
153                                 string value = bag [key] as string;
154                                 writer.AddAttribute (key, value);
155                         }
156                 }
157
158                 public void Clear ()
159                 {
160                         CssStyle.Clear ();
161                         bag.Clear ();
162                 }
163
164                 public void Remove (string key)
165                 {
166                         if (0 == String.Compare (key, StyleAttribute, true, CultureInfo.InvariantCulture)) {
167                                 CssStyle.Clear ();
168                                 return;
169                         }
170                         bag.Remove (key);
171                 }
172
173                 public void Render (HtmlTextWriter writer)
174                 {
175                         foreach (string key in bag.Keys) {
176                                 string value = bag [key] as string;
177                                 if (value != null)
178                                         writer.WriteAttribute (key, value, true);
179                         }
180                 }
181
182 #if NET_2_0
183                 internal void CopyFrom (AttributeCollection attributeCollection)
184                 {
185                         if (attributeCollection == null || attributeCollection.Count == 0)
186                                 return;
187
188                         foreach (string key in attributeCollection.bag.Keys)
189                                 this.Add (key, attributeCollection [key]);
190                 }
191 #endif
192         }
193 }