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.
31 using System.Collections;
32 using System.Collections.Specialized;
35 using System.Web.UI.WebControls;
36 using System.ComponentModel;
37 using System.Resources;
38 using System.Security.Principal;
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 {
96 SiteMapNodeCollection childNodes = ChildNodes;
97 return childNodes != null && childNodes.Count > 0;
101 public SiteMapNodeCollection GetAllNodes ()
103 SiteMapNodeCollection ret;
105 ret = new SiteMapNodeCollection ();
106 GetAllNodesRecursive (ret);
107 return SiteMapNodeCollection.ReadOnly (ret);
110 void GetAllNodesRecursive(SiteMapNodeCollection c)
112 SiteMapNodeCollection childNodes = this.ChildNodes;
114 if (childNodes != null && childNodes.Count > 0) {
115 c.AddRange (childNodes);
116 foreach (SiteMapNode n in childNodes)
117 n.GetAllNodesRecursive (c);
122 public virtual bool IsDescendantOf (SiteMapNode node)
124 for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
125 if (n == node) return true;
130 public virtual SiteMapNode NextSibling {
132 IList siblings = this.SiblingNodes;
133 if (siblings == null)
136 int pos = siblings.IndexOf (this);
137 if (pos >= 0 && pos < siblings.Count - 1)
138 return (SiteMapNode) siblings [pos + 1];
144 public virtual SiteMapNode PreviousSibling {
146 IList siblings = this.SiblingNodes;
147 if (siblings == null)
150 int pos = siblings.IndexOf (this);
151 if (pos > 0 && pos < siblings.Count)
152 return (SiteMapNode) siblings [pos - 1];
158 public virtual SiteMapNode ParentNode {
160 if (parent != null) return parent;
162 SiteMapProvider provider = this.provider;
165 parent = provider.GetParentNode (this);
169 provider = provider.ParentProvider;
170 } while (provider != null);
179 public virtual SiteMapNodeCollection ChildNodes {
181 if (provider.SecurityTrimmingEnabled) {
182 IPrincipal p = HttpContext.Current.User;
183 if ((user == null && user != p) || user != null && user != p) {
185 childNodes = provider.GetChildNodes (this);
187 } else if (childNodes == null) {
188 childNodes = provider.GetChildNodes (this);
199 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
201 SiteMapNodeCollection SiblingNodes {
203 if (ParentNode != null)
204 return ParentNode.ChildNodes;
210 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
212 if (attributeName == null)
213 throw new ArgumentNullException ("attributeName");
215 if (resourceKeys != null){
216 string[] values = resourceKeys.GetValues (attributeName);
217 if (values != null && values.Length == 2) {
219 object o = HttpContext.GetGlobalResourceObject (values [0], values [1]);
223 catch (MissingManifestResourceException) {
226 if (throwIfNotFound && defaultValue == null)
227 throw new InvalidOperationException (String.Format ("The resource object with classname '{0}' and key '{1}' was not found.", values [0], values [1]));
234 protected string GetImplicitResourceString (string attributeName)
236 if (attributeName == null)
237 throw new ArgumentNullException ("attributeName");
239 string resourceKey = ResourceKey;
240 if (String.IsNullOrEmpty (resourceKey))
244 object o = HttpContext.GetGlobalResourceObject (provider.ResourceKey, resourceKey + "." + attributeName);
247 } catch (MissingManifestResourceException) {
253 public virtual string this [string key]
256 if (provider.EnableLocalization) {
257 string val = GetImplicitResourceString (key);
259 val = GetExplicitResourceString (key, null, true);
263 if (attributes != null) return attributes [key];
268 if (attributes == null) attributes = new NameValueCollection ();
269 attributes [key] = value;
273 object ICloneable.Clone ()
275 return Clone (false);
278 public virtual SiteMapNode Clone ()
280 return Clone (false);
283 public virtual SiteMapNode Clone (bool cloneParentNodes)
285 SiteMapNode node = new SiteMapNode ();
286 node.provider = provider;
290 node.description = description;
292 node.roles = new ArrayList (roles);
293 if (attributes != null)
294 node.attributes = new NameValueCollection (attributes);
295 if (cloneParentNodes && ParentNode != null)
296 node.parent = (SiteMapNode) ParentNode.Clone (true);
300 public override bool Equals (object ob)
302 SiteMapNode node = ob as SiteMapNode;
303 if (node == null) return false;
305 if (node.key != key ||
307 node.title != title ||
308 node.description != description) {
312 if (roles == null || node.roles == null) {
313 if (roles != node.roles)
317 if (roles.Count != node.roles.Count)
320 foreach (object role in roles)
321 if (!node.roles.Contains (role)) return false;
323 if (attributes == null || node.attributes == null) {
324 if (attributes != node.attributes)
328 if (attributes.Count != node.attributes.Count)
331 foreach (string k in attributes)
332 if (attributes[k] != node.attributes[k])
338 public override int GetHashCode ()
340 return (key + url + title + description).GetHashCode ();
343 void CheckWritable ()
346 throw new InvalidOperationException ("Can't modify read-only node");
349 #region Field Accessors
351 protected NameValueCollection Attributes {
352 get { return attributes; }
353 set { CheckWritable (); attributes = value; }
357 public virtual string Description {
361 if (provider.EnableLocalization) {
362 ret = GetImplicitResourceString ("description");
364 ret = GetExplicitResourceString ("description", description, true);
368 return ret != null ? ret : String.Empty;
370 set { CheckWritable (); description = value; }
373 [LocalizableAttribute (true)]
374 public virtual string Title {
378 if (provider.EnableLocalization) {
379 ret = GetImplicitResourceString ("title");
381 ret = GetExplicitResourceString ("title", title, true);
385 return ret != null ? ret : String.Empty;
387 set { CheckWritable (); title = value; }
390 public virtual string Url {
391 get { return url != null ? url : ""; }
392 set { CheckWritable (); url = value; }
396 get { return roles; }
397 set { CheckWritable (); roles = value; }
400 public bool ReadOnly {
401 get { return readOnly; }
402 set { readOnly = value; }
405 public string ResourceKey {
406 get { return resourceKey; }
409 throw new InvalidOperationException ("The node is read-only.");
414 public string Key { get { return key; } }
415 public SiteMapProvider Provider { get { return provider; } }
419 #region INavigateUIData
420 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
421 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
426 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
427 object System.Web.UI.IHierarchyData.Item { get { return this; } }
428 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
429 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
432 #region INavigateUIData
433 string INavigateUIData.Name { get { return Title; } }
434 string INavigateUIData.NavigateUrl { get { return Url; } }
435 string INavigateUIData.Value { get { return Title; } }
439 SiteMapProvider provider;
445 NameValueCollection attributes;
446 NameValueCollection resourceKeys;
450 SiteMapNodeCollection childNodes;