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