2 // System.Web.SiteMapNode
5 // Ben Maurer (bmaurer@users.sourceforge.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
36 using System.Web.UI.WebControls;
37 using System.ComponentModel;
38 using System.Resources;
40 namespace System.Web {
41 public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
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) {}
54 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description,
55 IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys,
56 string implicitResourceKey)
59 throw new ArgumentNullException ("provider");
61 throw new ArgumentNullException ("key");
63 this.provider = provider;
67 this.description = description;
69 this.attributes = attributes;
70 this.resourceKeys = explicitResourceKeys;
71 this.resourceKey = implicitResourceKey;
74 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
76 return new SiteMapDataSourceView (owner, viewName, this);
79 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
81 return new SiteMapHierarchicalDataSourceView (this);
84 public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
86 return provider.IsAccessibleToUser (ctx, this);
89 public override string ToString()
94 public virtual bool HasChildNodes {
95 get { return ChildNodes != null && ChildNodes.Count != 0; }
98 public SiteMapNodeCollection GetAllNodes ()
100 SiteMapNodeCollection ret;
102 ret = new SiteMapNodeCollection ();
103 GetAllNodesRecursive (ret);
104 return SiteMapNodeCollection.ReadOnly (ret);
107 void GetAllNodesRecursive(SiteMapNodeCollection c)
109 SiteMapNodeCollection childNodes = this.ChildNodes;
111 if (childNodes != null && childNodes.Count > 0) {
112 c.AddRange (childNodes);
113 foreach (SiteMapNode n in childNodes)
114 n.GetAllNodesRecursive (c);
119 public virtual bool IsDescendantOf (SiteMapNode node)
121 for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
122 if (n == node) return true;
127 public virtual SiteMapNode NextSibling {
129 IList siblings = this.SiblingNodes;
130 if (siblings == null)
133 int pos = siblings.IndexOf (this);
134 if (pos >= 0 && pos < siblings.Count - 1)
135 return (SiteMapNode) siblings [pos + 1];
141 public virtual SiteMapNode PreviousSibling {
143 IList siblings = this.SiblingNodes;
144 if (siblings == null)
147 int pos = siblings.IndexOf (this);
148 if (pos > 0 && pos < siblings.Count)
149 return (SiteMapNode) siblings [pos - 1];
155 public virtual SiteMapNode ParentNode {
157 if (parent != null) return parent;
159 SiteMapProvider provider = this.provider;
162 parent = provider.GetParentNode (this);
166 provider = provider.ParentProvider;
167 } while (provider != null);
176 internal SiteMapNodeCollection ChildNodesInternal {
178 if (childNodes == null)
179 childNodes = new SiteMapNodeCollection ();
184 public virtual SiteMapNodeCollection ChildNodes {
186 if (childNodes != null) return childNodes;
187 return provider.GetChildNodes (this);
195 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
197 SiteMapNodeCollection SiblingNodes {
199 if (ParentNode != null)
200 return ParentNode.ChildNodes;
206 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
208 if (attributeName == null)
209 throw new ArgumentNullException ("attributeName");
211 if (resourceKeys != null){
212 string[] values = resourceKeys.GetValues (attributeName);
213 if (values != null && values.Length == 2) {
215 object o = HttpContext.GetGlobalResourceObject (values [0], values [1]);
219 catch (MissingManifestResourceException) {
222 if (throwIfNotFound && defaultValue == null)
223 throw new InvalidOperationException (String.Format ("The resource object with classname '{0}' and key '{1}' was not found.", values [0], values [1]));
230 protected string GetImplicitResourceString (string attributeName)
232 if (attributeName == null)
233 throw new ArgumentNullException ("attributeName");
235 string resourceKey = ResourceKey;
236 if (String.IsNullOrEmpty (resourceKey))
240 object o = HttpContext.GetGlobalResourceObject (provider.ResourceKey, resourceKey + "." + attributeName);
243 } catch (MissingManifestResourceException) {
249 public virtual string this [string key]
252 if (provider.EnableLocalization) {
253 string val = GetImplicitResourceString (key);
255 val = GetExplicitResourceString (key, null, true);
259 if (attributes != null) return attributes [key];
264 if (attributes == null) attributes = new NameValueCollection ();
265 attributes [key] = value;
269 object ICloneable.Clone ()
271 return Clone (false);
274 public virtual SiteMapNode Clone ()
276 return Clone (false);
279 public virtual SiteMapNode Clone (bool cloneParentNodes)
281 SiteMapNode node = new SiteMapNode ();
282 node.provider = provider;
286 node.description = description;
288 node.roles = new ArrayList (roles);
289 if (attributes != null)
290 node.attributes = new NameValueCollection (attributes);
291 if (cloneParentNodes && ParentNode != null)
292 node.parent = (SiteMapNode) ParentNode.Clone (true);
296 public override bool Equals (object ob)
298 SiteMapNode node = ob as SiteMapNode;
299 if (node == null) return false;
301 if (node.key != key ||
303 node.title != title ||
304 node.description != description) {
308 if (roles == null || node.roles == null) {
309 if (roles != node.roles)
313 if (roles.Count != node.roles.Count)
316 foreach (object role in roles)
317 if (!node.roles.Contains (role)) return false;
319 if (attributes == null || node.attributes == null) {
320 if (attributes != node.attributes)
324 if (attributes.Count != node.attributes.Count)
327 foreach (string k in attributes)
328 if (attributes[k] != node.attributes[k])
334 public override int GetHashCode ()
336 return (key + url + title + description).GetHashCode ();
339 void CheckWritable ()
342 throw new InvalidOperationException ("Can't modify read-only node");
345 #region Field Accessors
347 protected NameValueCollection Attributes {
348 get { return attributes; }
349 set { CheckWritable (); attributes = value; }
353 public virtual string Description {
357 if (provider.EnableLocalization) {
358 ret = GetImplicitResourceString ("description");
360 ret = GetExplicitResourceString ("description", description, true);
364 return ret != null ? ret : String.Empty;
366 set { CheckWritable (); description = value; }
369 [LocalizableAttribute (true)]
370 public virtual string Title {
374 if (provider.EnableLocalization) {
375 ret = GetImplicitResourceString ("title");
377 ret = GetExplicitResourceString ("title", title, true);
381 return ret != null ? ret : String.Empty;
383 set { CheckWritable (); title = value; }
386 public virtual string Url {
387 get { return url != null ? url : ""; }
388 set { CheckWritable (); url = value; }
392 get { return roles; }
393 set { CheckWritable (); roles = value; }
396 public bool ReadOnly {
397 get { return readOnly; }
398 set { readOnly = value; }
401 public string ResourceKey {
402 get { return resourceKey; }
405 throw new InvalidOperationException ("The node is read-only.");
410 public string Key { get { return key; } }
411 public SiteMapProvider Provider { get { return provider; } }
415 #region INavigateUIData
416 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
417 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
422 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
423 object System.Web.UI.IHierarchyData.Item { get { return this; } }
424 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
425 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
428 #region INavigateUIData
429 string INavigateUIData.Name { get { return Title; } }
430 string INavigateUIData.NavigateUrl { get { return Url; } }
431 string INavigateUIData.Value { get { return Title; } }
435 SiteMapProvider provider;
441 NameValueCollection attributes;
442 NameValueCollection resourceKeys;
446 SiteMapNodeCollection childNodes;