~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/AspNet/MonoDevelop.AspNet/MonoDevelop.AspNet.Parser.Internal/TagAttributes.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// System.Web.Compilation.TagAttributes
3
 
//
4
 
// Authors:
5
 
//      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6
 
//
7
 
// (C) 2003 Ximian, Inc (http://www.ximian.com)
8
 
//
9
 
 
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;
32
 
using System.Collections;
33
 
using System.Text;
34
 
using System.Web;
35
 
using MonoDevelop.AspNet.Parser.Internal;
36
 
 
37
 
namespace MonoDevelop.AspNet.Parser.Dom
38
 
{
39
 
        public class TagAttributes
40
 
        {
41
 
                Hashtable atts_hash;
42
 
                Hashtable tmp_hash;
43
 
                ArrayList keys;
44
 
                ArrayList values;
45
 
                bool got_hashed;
46
 
 
47
 
                public TagAttributes ()
48
 
                {
49
 
                        got_hashed = false;
50
 
                        keys = new ArrayList ();
51
 
                        values = new ArrayList ();
52
 
                }
53
 
 
54
 
                void MakeHash ()
55
 
                {
56
 
                        atts_hash = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
57
 
                        for (int i = 0; i < keys.Count; i++) {
58
 
                                CheckServerKey (keys [i]);
59
 
                                atts_hash.Add (keys [i], values [i]);
60
 
                        }
61
 
                        got_hashed = true;
62
 
                        keys = null;
63
 
                        values = null;
64
 
                }
65
 
                
66
 
                public bool IsRunAtServer ()
67
 
                {
68
 
                        return got_hashed;
69
 
                }
70
 
 
71
 
                public void Add (object key, object value)
72
 
                {
73
 
                        if (key != null && value != null &&
74
 
                            0 == String.Compare ((string) key,  "runat", true)) {
75
 
                                if (0 != String.Compare ((string) value,  "server", true))
76
 
                                        throw new HttpException ("runat attribute must have a 'server' value");
77
 
 
78
 
                                if (got_hashed)
79
 
                                        return; // ignore duplicate runat="server"
80
 
 
81
 
                                MakeHash ();
82
 
                        }
83
 
 
84
 
                        if (value != null)
85
 
                                value = HttpUtility.HtmlDecode (value.ToString ());
86
 
 
87
 
                        if (got_hashed) {
88
 
                                CheckServerKey (key);
89
 
                                if (atts_hash.ContainsKey (key))
90
 
                                        throw new HttpException ("Tag contains duplicated '" + key +
91
 
                                                                 "' attributes.");
92
 
                                atts_hash.Add (key, value);
93
 
                        } else {
94
 
                                keys.Add (key);
95
 
                                values.Add (value);
96
 
                        }
97
 
                }
98
 
                
99
 
                public ICollection Keys 
100
 
                {
101
 
                        get { return (got_hashed ? atts_hash.Keys : keys); }
102
 
                }
103
 
 
104
 
                public ICollection Values 
105
 
                {
106
 
                        get { return (got_hashed ? atts_hash.Values : values); }
107
 
                }
108
 
 
109
 
                private int CaseInsensitiveSearch (string key)
110
 
                {
111
 
                        // Hope not to have many attributes when the tag is not a server tag...
112
 
                        for (int i = 0; i < keys.Count; i++){
113
 
                                if (0 == String.Compare ((string) keys [i], key, true))
114
 
                                        return i;
115
 
                        }
116
 
                        return -1;
117
 
                }
118
 
                
119
 
                public object this [object key]
120
 
                {
121
 
                        get {
122
 
                                if (got_hashed)
123
 
                                        return atts_hash [key];
124
 
 
125
 
                                int idx = CaseInsensitiveSearch ((string) key);
126
 
                                if (idx == -1)
127
 
                                        return null;
128
 
                                                
129
 
                                return values [idx];
130
 
                        }
131
 
 
132
 
                        set {
133
 
                                if (got_hashed) {
134
 
                                        CheckServerKey (key);
135
 
                                        atts_hash [key] = value;
136
 
                                } else {
137
 
                                        int idx = CaseInsensitiveSearch ((string) key);
138
 
                                        keys [idx] = value;
139
 
                                }
140
 
                        }
141
 
                }
142
 
                
143
 
                public int Count 
144
 
                {
145
 
                        get { return (got_hashed ? atts_hash.Count : keys.Count);}
146
 
                }
147
 
 
148
 
                public bool IsDataBound (string att)
149
 
                {
150
 
                        if (att == null || !got_hashed)
151
 
                                return false;
152
 
 
153
 
                        return (StrUtils.StartsWith (att, "<%#") && StrUtils.EndsWith (att, "%>"));
154
 
                }
155
 
                
156
 
                public Hashtable GetDictionary (string key)
157
 
                {
158
 
                        if (got_hashed)
159
 
                                return atts_hash;
160
 
 
161
 
                        if (tmp_hash == null)
162
 
                                tmp_hash = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
163
 
                        
164
 
                        tmp_hash.Clear ();
165
 
                        for (int i = keys.Count - 1; i >= 0; i--)
166
 
                                if (key == null || String.Compare (key, (string) keys [i], true) == 0)
167
 
                                        tmp_hash [keys [i]] = values [i];
168
 
 
169
 
                        return tmp_hash;
170
 
                }
171
 
                
172
 
                public override string ToString ()
173
 
                {
174
 
                        StringBuilder result = new StringBuilder ();
175
 
                        string value;
176
 
                        foreach (string key in Keys){
177
 
                                result.Append (key);
178
 
                                value = this [key] as string;
179
 
                                if (value != null)
180
 
                                        result.AppendFormat ("=\"{0}\"", value);
181
 
 
182
 
                                result.Append (' ');
183
 
                        }
184
 
 
185
 
                        if (result.Length > 0 && result [result.Length - 1] == ' ')
186
 
                                result.Length--;
187
 
                                
188
 
                        return result.ToString ();
189
 
                }
190
 
                
191
 
                void CheckServerKey (object key)
192
 
                {
193
 
                        if (key == null || ((string)key).Length == 0)
194
 
                                throw new HttpException ("The server tag is not well formed.");
195
 
                }
196
 
        }
197
 
}
198