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;
39 namespace System.Web {
40 public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
42 private SiteMapNode () {}
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) {}
53 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description,
54 IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys,
55 string implicitResourceKey)
58 throw new ArgumentNullException ("provider");
60 throw new ArgumentNullException ("key");
62 this.provider = provider;
66 this.description = description;
68 this.attributes = attributes;
69 this.resourceKeys = explicitResourceKeys;
70 this.implicitResourceKey = implicitResourceKey;
73 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
75 return new SiteMapDataSourceView (owner, viewName, this);
78 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
80 return new SiteMapHierarchicalDataSourceView (this);
83 public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
85 return provider.IsAccessibleToUser (ctx, this);
88 public override string ToString()
93 public virtual bool HasChildNodes {
94 get { return ChildNodes != null && ChildNodes.Count != 0; }
97 public SiteMapNodeCollection GetAllNodes ()
99 SiteMapNodeCollection ret;
101 ret = new SiteMapNodeCollection ();
102 GetAllNodesRecursive (ret);
103 return SiteMapNodeCollection.ReadOnly (ret);
106 void GetAllNodesRecursive(SiteMapNodeCollection c)
108 SiteMapNodeCollection childNodes = this.ChildNodes;
110 if (childNodes.Count > 0) {
111 childNodes.AddRange (childNodes);
112 foreach (SiteMapNode n in childNodes)
113 n.GetAllNodesRecursive (c);
118 public virtual bool IsDescendantOf (SiteMapNode node)
120 for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
121 if (n == node) return true;
126 public virtual SiteMapNode NextSibling {
128 IList siblings = this.SiblingNodes;
129 if (siblings == null)
132 int pos = siblings.IndexOf (this);
133 if (pos >= 0 && pos < siblings.Count - 1)
134 return (SiteMapNode) siblings [pos + 1];
140 public virtual SiteMapNode PreviousSibling {
142 IList siblings = this.SiblingNodes;
143 if (siblings == null)
146 int pos = siblings.IndexOf (this);
147 if (pos > 0 && pos < siblings.Count)
148 return (SiteMapNode) siblings [pos - 1];
154 public virtual SiteMapNode ParentNode {
156 if (parent != null) return parent;
158 SiteMapProvider provider = this.provider;
161 parent = provider.GetParentNode (this);
165 provider = provider.ParentProvider;
166 } while (provider != null);
175 public virtual SiteMapNodeCollection ChildNodes {
177 if (childNodes != null) return childNodes;
178 return provider.GetChildNodes (this);
186 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
188 SiteMapNodeCollection SiblingNodes {
190 if (ParentNode != null)
191 return ParentNode.ChildNodes;
197 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
199 if (attributeName == null)
200 throw new ArgumentNullException ("attributeName");
202 if (resourceKeys != null){
203 string v = resourceKeys [attributeName];
208 if (throwIfNotFound && defaultValue == null)
209 throw new InvalidOperationException ();
214 [MonoTODO("Currently returns the argument passed")]
215 protected string GetImplicitResourceString (string attributeName)
217 if (attributeName == null)
218 throw new ArgumentNullException ("attributeName");
220 return attributeName;
223 public virtual string this [string key]
227 if (provider.EnableLocalization) {
228 val = GetExplicitResourceString (key, null, true);
229 if (val == null) val = GetImplicitResourceString (key);
231 if (val != null) return null;
232 if (attributes != null) return attributes [key];
237 if (attributes == null) attributes = new NameValueCollection ();
238 attributes [key] = value;
242 object ICloneable.Clone ()
244 return Clone (false);
247 public virtual SiteMapNode Clone ()
249 return Clone (false);
252 public virtual SiteMapNode Clone (bool cloneParentNodes)
254 SiteMapNode node = new SiteMapNode ();
255 node.provider = provider;
259 node.description = description;
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);
269 public override bool Equals (object ob)
271 SiteMapNode node = ob as SiteMapNode;
272 if (node == null) return false;
274 if (node.key != key ||
276 node.title != title ||
277 node.description != description) {
281 if (roles == null || node.roles == null) {
282 if (roles != node.roles)
286 if (roles.Count != node.roles.Count)
289 foreach (object role in roles)
290 if (!node.roles.Contains (role)) return false;
292 if (attributes == null || node.attributes == null) {
293 if (attributes != node.attributes)
297 if (attributes.Count != node.attributes.Count)
300 foreach (string k in attributes)
301 if (attributes[k] != node.attributes[k])
307 public override int GetHashCode ()
309 return (key + url + title + description).GetHashCode ();
312 void CheckWritable ()
315 throw new InvalidOperationException ("Can't modify read-only node");
318 #region Field Accessors
320 protected NameValueCollection Attributes {
321 get { return attributes; }
322 set { CheckWritable (); attributes = value; }
326 public virtual string Description {
327 get { return description != null ? description : ""; }
328 set { CheckWritable (); description = value; }
331 [LocalizableAttribute (true)]
332 public virtual string Title {
333 get { return title != null ? title : ""; }
334 set { CheckWritable (); title = value; }
337 public virtual string Url {
338 get { return url != null ? url : ""; }
339 set { CheckWritable (); url = value; }
343 get { return roles; }
344 set { CheckWritable (); roles = value; }
347 public bool ReadOnly {
348 get { return readOnly; }
349 set { readOnly = value; }
352 public string ResourceKey {
353 get { return resourceKey; }
354 set { resourceKey = value; }
357 public string Key { get { return key; } }
358 public SiteMapProvider Provider { get { return provider; } }
362 #region INavigateUIData
363 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
364 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
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"; } }
375 #region INavigateUIData
376 string INavigateUIData.Name { get { return Title; } }
377 string INavigateUIData.NavigateUrl { get { return Url; } }
378 string INavigateUIData.Value { get { return Title; } }
382 SiteMapProvider provider;
388 NameValueCollection attributes;
389 NameValueCollection resourceKeys;
393 string implicitResourceKey;
394 SiteMapNodeCollection childNodes;