2007-01-30 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / System.Web / SiteMapNode.cs
1 //
2 // System.Web.SiteMapNode
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
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 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Web.UI;
36 using System.Web.UI.WebControls;
37 using System.ComponentModel;
38
39 namespace System.Web {
40         public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
41         
42                 private SiteMapNode () {}
43                 
44                 public SiteMapNode (SiteMapProvider provider, string key)
45                         : this (provider, key, null, null, null, null, null, null, null) {}
46                 public SiteMapNode (SiteMapProvider provider, string key, string url)
47                         : this (provider, key, url, null, null, null, null, null, null) {}
48                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title)
49                         : this (provider, key, url, title, null, null, null, null, null) {}
50                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description)
51                         : this (provider, key, url, title, description, null, null, null, null) {}
52                 
53                 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description,
54                                     IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys,
55                                     string implicitResourceKey)
56                 {
57                         if (provider == null)
58                                 throw new ArgumentNullException ("provider");
59                         if (key == null)
60                                 throw new ArgumentNullException ("key");
61                         
62                         this.provider = provider;
63                         this.key = key;
64                         this.url = url;
65                         this.title = title;
66                         this.description = description;
67                         this.roles = roles;
68                         this.attributes = attributes;
69                         this.resourceKeys = explicitResourceKeys;
70                         this.implicitResourceKey = implicitResourceKey;
71                 }
72
73                 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
74                 {
75                         return new SiteMapDataSourceView (owner, viewName, this);
76                 }
77                 
78                 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
79                 {
80                         return new SiteMapHierarchicalDataSourceView (this);
81                 }
82                 
83                 public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
84                 {
85                         return provider.IsAccessibleToUser (ctx, this);
86                 }
87                 
88                 public override string ToString()
89                 {
90                         return Title;
91                 }
92
93                 public virtual bool HasChildNodes {
94                         get { return ChildNodes != null && ChildNodes.Count != 0; }
95                 }
96
97                 public SiteMapNodeCollection GetAllNodes ()
98                 {
99                         SiteMapNodeCollection ret;
100                 
101                         ret = new SiteMapNodeCollection ();
102                         GetAllNodesRecursive (ret);
103                         return SiteMapNodeCollection.ReadOnly (ret);
104                 }
105                 
106                 void GetAllNodesRecursive(SiteMapNodeCollection c)
107                 {
108                         SiteMapNodeCollection childNodes = this.ChildNodes;
109                         
110                         if (childNodes.Count > 0) {
111                                 childNodes.AddRange (childNodes);
112                                 foreach (SiteMapNode n in childNodes)
113                                         n.GetAllNodesRecursive (c);
114                         }
115                 }
116
117                 
118                 public virtual bool IsDescendantOf (SiteMapNode node)
119                 {
120                         for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
121                                 if (n == node) return true; 
122
123                         return false; 
124                 }
125                 
126                 public virtual SiteMapNode NextSibling {
127                         get {
128                                 IList siblings = this.SiblingNodes;
129                                 if (siblings == null)
130                                         return null; 
131                                 
132                                 int pos = siblings.IndexOf (this);
133                                 if (pos >= 0 && pos < siblings.Count - 1)
134                                         return (SiteMapNode) siblings [pos + 1]; 
135                                 
136                                 return null; 
137                         }
138                 }
139                 
140                 public virtual SiteMapNode PreviousSibling {
141                         get {
142                                 IList siblings = this.SiblingNodes;
143                                 if (siblings == null)
144                                         return null; 
145                                 
146                                 int pos = siblings.IndexOf (this);
147                                 if (pos > 0 && pos < siblings.Count)
148                                         return (SiteMapNode) siblings [pos - 1]; 
149                                 
150                                 return null; 
151                         }
152                 }
153                 
154                 public virtual SiteMapNode ParentNode {
155                         get {
156                                 if (parent != null) return parent;
157                                 
158                                 SiteMapProvider provider = this.provider;
159                                 
160                                 do {
161                                         parent = provider.GetParentNode (this);
162                                         if (parent != null)
163                                                 return parent; 
164                                         
165                                         provider = provider.ParentProvider;
166                                 } while (provider != null);
167                                 return null;
168                         }
169                         set {
170                                 CheckWritable ();
171                                 parent = value;
172                         }
173                 }
174                 
175                 public virtual SiteMapNodeCollection ChildNodes {
176                         get {
177                                 if (childNodes != null) return childNodes;
178                                 return provider.GetChildNodes (this);
179                         } 
180                         set {
181                                 CheckWritable ();
182                                 childNodes = value;
183                         }
184                 }
185
186                 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; }  }
187                 
188                 SiteMapNodeCollection SiblingNodes {
189                         get {
190                                 if (ParentNode != null)
191                                         return ParentNode.ChildNodes;
192                                 
193                                 return null;
194                         }
195                 }
196                 
197                 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
198                 {
199                         if (attributeName == null)
200                                 throw new ArgumentNullException ("attributeName");
201                         
202                         if (resourceKeys != null){
203                                 string v = resourceKeys [attributeName];
204                                 if (v != null)
205                                         return v;
206                         }
207
208                         if (throwIfNotFound && defaultValue == null)
209                                 throw new InvalidOperationException ();
210
211                         return defaultValue;
212                 }
213
214                 [MonoTODO("Currently returns the argument passed")]
215                 protected string GetImplicitResourceString (string attributeName)
216                 {
217                         if (attributeName == null)
218                                 throw new ArgumentNullException ("attributeName");
219                         
220                         return attributeName;
221                 }
222                 
223                 public virtual string this [string key]
224                 {
225                         get {
226                                 string val = null;
227                                 if (provider.EnableLocalization) {
228                                         val = GetExplicitResourceString (key, null, true);
229                                         if (val == null) val = GetImplicitResourceString (key);
230                                 }
231                                 if (val != null) return null;
232                                 if (attributes != null) return attributes [key];
233                                 return null;
234                         }
235                         set {
236                                 CheckWritable ();
237                                 if (attributes == null) attributes = new NameValueCollection ();
238                                 attributes [key] = value;
239                         }
240                 }
241                 
242                 object ICloneable.Clone ()
243                 {
244                         return Clone (false);
245                 }
246                 
247                 public virtual SiteMapNode Clone ()
248                 {
249                         return Clone (false);
250                 }
251                 
252                 public virtual SiteMapNode Clone (bool cloneParentNodes)
253                 {
254                         SiteMapNode node = new SiteMapNode ();
255                         node.provider = provider;
256                         node.key = key;
257                         node.url = url;
258                         node.title = title;
259                         node.description = description;
260                         if (roles != null)
261                                 node.roles = new ArrayList (roles);
262                         if (attributes != null)
263                                 node.attributes = new NameValueCollection (attributes);
264                         if (cloneParentNodes && ParentNode != null)
265                                 node.parent = (SiteMapNode) ParentNode.Clone (true);
266                         return node;
267                 }
268                                 
269                 public override bool Equals (object ob)
270                 {
271                         SiteMapNode node = ob as SiteMapNode;
272                         if (node == null) return false;
273                         
274                         if (node.key != key ||
275                                         node.url != url ||
276                                         node.title != title ||
277                                         node.description != description) {
278                                 return false;
279                         }
280
281                         if (roles == null || node.roles == null) {
282                                 if (roles != node.roles)
283                                         return false;
284                         }
285                         else {
286                                 if (roles.Count != node.roles.Count)
287                                         return false;
288
289                                 foreach (object role in roles)
290                                         if (!node.roles.Contains (role)) return false;
291                         }
292                         if (attributes == null || node.attributes == null) {
293                                 if (attributes != node.attributes)
294                                         return false;
295                         }
296                         else {
297                                 if (attributes.Count != node.attributes.Count)
298                                         return false;
299
300                                 foreach (string k in attributes)
301                                         if (attributes[k] != node.attributes[k])
302                                                 return false;
303                         }
304                         return true;
305                 }
306                 
307                 public override int GetHashCode ()
308                 {
309                         return (key + url + title + description).GetHashCode ();
310                 }
311                 
312                 void CheckWritable ()
313                 {
314                         if (readOnly)
315                                 throw new InvalidOperationException ("Can't modify read-only node");
316                 }
317                                 
318                 #region Field Accessors
319                 
320                 protected NameValueCollection Attributes {
321                         get { return attributes; } 
322                         set { CheckWritable (); attributes = value; }
323                 }
324                 
325                 [Localizable (true)]
326                 public virtual string Description {
327                         get { return description != null ? description : ""; }
328                         set { CheckWritable (); description = value; }
329                 }
330                 
331                 [LocalizableAttribute (true)]
332                 public virtual string Title {
333                         get { return title != null ? title : ""; }
334                         set { CheckWritable (); title = value; }
335                 }
336                 
337                 public virtual string Url {
338                         get { return url != null ? url : ""; }
339                         set { CheckWritable (); url = value; }
340                 }
341                 
342                 public IList Roles {
343                         get { return roles; }
344                         set { CheckWritable (); roles = value; }
345                 }
346                 
347                 public bool ReadOnly {
348                         get { return readOnly; }
349                         set { readOnly = value; }
350                 }
351                 
352                 public string ResourceKey {
353                         get { return resourceKey; }
354                         set { resourceKey = value; }
355                 }
356                 
357                 public string Key { get { return key; } }
358                 public SiteMapProvider Provider { get { return provider; } }
359                 
360                 #endregion
361                 
362                 #region INavigateUIData
363                 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
364                 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
365                 {
366                         return ParentNode;
367                 }
368
369                 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
370                 object System.Web.UI.IHierarchyData.Item { get { return this; } }
371                 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
372                 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
373                 #endregion
374                 
375                 #region INavigateUIData
376                 string INavigateUIData.Name { get { return Title; }  }
377                 string INavigateUIData.NavigateUrl { get { return Url; } }
378                 string INavigateUIData.Value { get { return Title; } }
379                 #endregion
380
381                 #region Fields
382                 SiteMapProvider provider;
383                 string key;
384                 string url;
385                 string title;
386                 string description;
387                 IList roles;
388                 NameValueCollection attributes;
389                 NameValueCollection resourceKeys;
390                 bool readOnly;
391                 string resourceKey;
392                 SiteMapNode parent;
393                 string implicitResourceKey;
394                 SiteMapNodeCollection childNodes;
395                 #endregion
396                 
397         }
398 }
399 #endif
400