2 // System.Web.Compilation.TagAttributes
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 // (C) 2003-2009 Novell, Inc (http://novell.com/)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Collections
;
34 using System
.Globalization
;
36 using System
.Web
.Util
;
38 namespace System
.Web
.Compilation
40 sealed class TagAttributes
48 public TagAttributes ()
51 keys
= new ArrayList ();
52 values
= new ArrayList ();
57 atts_hash
= new Hashtable (StringComparer
.InvariantCultureIgnoreCase
);
58 for (int i
= 0; i
< keys
.Count
; i
++) {
59 CheckServerKey (keys
[i
]);
60 atts_hash
.Add (keys
[i
], values
[i
]);
67 public bool IsRunAtServer ()
72 public void Add (object key
, object value)
74 if (key
!= null && value != null &&
75 0 == String
.Compare ((string) key
, "runat", true, Helpers
.InvariantCulture
)) {
76 if (0 != String
.Compare ((string) value, "server", true))
77 throw new HttpException ("runat attribute must have a 'server' value");
80 return; // ignore duplicate runat="server"
86 value = HttpUtility
.HtmlDecode (value.ToString ());
90 if (atts_hash
.ContainsKey (key
))
91 throw new HttpException ("Tag contains duplicated '" + key
+
93 atts_hash
.Add (key
, value);
100 public ICollection Keys
102 get { return (got_hashed ? atts_hash.Keys : keys); }
105 public ICollection Values
107 get { return (got_hashed ? atts_hash.Values : values); }
110 int CaseInsensitiveSearch (string key
)
112 // Hope not to have many attributes when the tag is not a server tag...
113 for (int i
= 0; i
< keys
.Count
; i
++){
114 if (0 == String
.Compare ((string) keys
[i
], key
, true, Helpers
.InvariantCulture
))
120 public object this [object key
]
124 return atts_hash
[key
];
126 int idx
= CaseInsensitiveSearch ((string) key
);
135 CheckServerKey (key
);
136 atts_hash
[key
] = value;
138 int idx
= CaseInsensitiveSearch ((string) key
);
146 get { return (got_hashed ? atts_hash.Count : keys.Count);}
149 public bool IsDataBound (string att
)
151 if (att
== null || !got_hashed
)
154 return (StrUtils
.StartsWith (att
, "<%#") && StrUtils
.EndsWith (att
, "%>"));
157 public IDictionary
GetDictionary (string key
)
162 if (tmp_hash
== null)
163 tmp_hash
= new Hashtable (StringComparer
.InvariantCultureIgnoreCase
);
166 for (int i
= keys
.Count
- 1; i
>= 0; i
--)
167 if (key
== null || String
.Compare (key
, (string) keys
[i
], true, Helpers
.InvariantCulture
) == 0)
168 tmp_hash
[keys
[i
]] = values
[i
];
173 public override string ToString ()
175 StringBuilder result
= new StringBuilder ("TagAttributes {");
177 foreach (string key
in Keys
){
180 value = this [key
] as string;
182 result
.AppendFormat ("=\"{0}\"", value);
184 result
.Append ("] ");
187 if (result
.Length
> 0 && result
[result
.Length
- 1] == ' ')
191 if (IsRunAtServer ())
192 result
.Append (" @Server");
194 return result
.ToString ();
197 void CheckServerKey (object key
)
199 if (key
== null || ((string)key
).Length
== 0)
200 throw new HttpException ("The server tag is not well formed.");