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) : this (provider, key, null, null, null, null, null, null, null) {}
45 public SiteMapNode (SiteMapProvider provider, string key, string url) : this (provider, key, url, null, null, null, null, null, null) {}
46 public SiteMapNode (SiteMapProvider provider, string key, string url, string title) : this (provider, key, url, title, null, null, null, null, null) {}
47 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description) : this (provider, key, url, title, description, null, null, null, null) {}
48 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys, string implicitResourceKey)
51 throw new ArgumentNullException ("provider");
53 throw new ArgumentNullException ("key");
55 this.provider = provider;
59 this.description = description;
61 this.attributes = attributes;
62 this.resourceKeys = explicitResourceKeys;
63 this.implicitResourceKey = implicitResourceKey;
66 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
68 return new SiteMapDataSourceView (owner, viewName, this);
71 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
73 return new SiteMapHierarchicalDataSourceView (this);
76 public virtual bool IsAccessibleToUser (System.Web.HttpContext ctx)
78 return provider.IsAccessibleToUser (ctx, this);
81 public override string ToString()
86 public virtual bool HasChildNodes {
87 get { return ChildNodes != null && ChildNodes.Count != 0; }
90 public SiteMapNodeCollection GetAllNodes ()
92 SiteMapNodeCollection ret;
94 ret = new SiteMapNodeCollection ();
95 GetAllNodesRecursive (ret);
96 return SiteMapNodeCollection.ReadOnly (ret);
99 void GetAllNodesRecursive(SiteMapNodeCollection c)
101 SiteMapNodeCollection childNodes = this.ChildNodes;
103 if (childNodes.Count > 0) {
104 childNodes.AddRange (childNodes);
105 foreach (SiteMapNode n in childNodes)
106 n.GetAllNodesRecursive (c);
111 public virtual bool IsDescendantOf (SiteMapNode node)
113 for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
114 if (n == node) return true;
119 public virtual SiteMapNode NextSibling {
121 IList siblings = this.SiblingNodes;
122 if (siblings == null)
125 int pos = siblings.IndexOf (this);
126 if (pos >= 0 && pos < siblings.Count - 1)
127 return (SiteMapNode) siblings [pos + 1];
133 public virtual SiteMapNode PreviousSibling {
135 IList siblings = this.SiblingNodes;
136 if (siblings == null)
139 int pos = siblings.IndexOf (this);
140 if (pos > 0 && pos < siblings.Count)
141 return (SiteMapNode) siblings [pos - 1];
147 public virtual SiteMapNode ParentNode {
149 if (parent != null) return parent;
151 SiteMapProvider provider = this.provider;
154 parent = provider.GetParentNode (this);
158 provider = provider.ParentProvider;
159 } while (provider != null);
168 public virtual SiteMapNodeCollection ChildNodes {
170 if (childNodes != null) return childNodes;
171 return provider.GetChildNodes (this);
179 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
181 SiteMapNodeCollection SiblingNodes {
183 if (ParentNode != null)
184 return ParentNode.ChildNodes;
190 [MonoTODO("Not implemented, always returns null")]
191 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
196 [MonoTODO("Not implemented, always returns null")]
197 protected string GetImplicitResourceString (string attributeName)
202 public virtual string this [string key]
206 if (provider.EnableLocalization) {
207 val = GetExplicitResourceString (key, null, true);
208 if (val == null) val = GetImplicitResourceString (key);
210 if (val != null) return null;
211 if (attributes != null) return attributes [key];
216 if (attributes == null) attributes = new NameValueCollection ();
217 attributes [key] = value;
221 object ICloneable.Clone ()
223 return Clone (false);
226 public virtual SiteMapNode Clone ()
228 return Clone (false);
231 public virtual SiteMapNode Clone (bool cloneParentNodes)
233 SiteMapNode node = new SiteMapNode ();
234 node.provider = provider;
238 node.description = description;
240 node.roles = new ArrayList (roles);
241 if (attributes != null)
242 node.attributes = new NameValueCollection (attributes);
243 if (cloneParentNodes && ParentNode != null)
244 node.parent = (SiteMapNode) ParentNode.Clone (true);
248 public override bool Equals (object ob)
250 SiteMapNode node = ob as SiteMapNode;
251 if (node == null) return false;
253 if (node.key != key ||
255 node.title != title ||
256 node.description != description) {
260 if (roles == null || node.roles == null) {
261 if (roles != node.roles)
265 if (roles.Count != node.roles.Count)
268 foreach (object role in roles)
269 if (!node.roles.Contains (role)) return false;
271 if (attributes == null || node.attributes == null) {
272 if (attributes != node.attributes)
276 if (attributes.Count != node.attributes.Count)
279 foreach (string k in attributes)
280 if (attributes[k] != node.attributes[k])
286 public override int GetHashCode ()
288 return (key + url + title + description).GetHashCode ();
291 void CheckWritable ()
294 throw new InvalidOperationException ("Can't modify read-only node");
297 #region Field Accessors
299 protected NameValueCollection Attributes {
300 get { return attributes; }
301 set { CheckWritable (); attributes = value; }
305 public virtual string Description {
306 get { return description != null ? description : ""; }
307 set { CheckWritable (); description = value; }
310 [LocalizableAttribute (true)]
311 public virtual string Title {
312 get { return title != null ? title : ""; }
313 set { CheckWritable (); title = value; }
316 public virtual string Url {
317 get { return url != null ? url : ""; }
318 set { CheckWritable (); url = value; }
322 get { return roles; }
323 set { CheckWritable (); roles = value; }
326 public bool ReadOnly {
327 get { return readOnly; }
328 set { readOnly = value; }
331 [MonoTODO ("Do somethig with this")]
332 public string ResourceKey {
333 get { return resourceKey; }
334 set { resourceKey = value; }
337 public string Key { get { return key; } }
338 public SiteMapProvider Provider { get { return provider; } }
342 #region INavigateUIData
343 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
344 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
349 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
350 object System.Web.UI.IHierarchyData.Item { get { return this; } }
351 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
352 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
355 #region INavigateUIData
356 string INavigateUIData.Name { get { return Title; } }
357 string INavigateUIData.NavigateUrl { get { return Url; } }
358 string INavigateUIData.Value { get { return Title; } }
362 SiteMapProvider provider;
368 NameValueCollection attributes;
369 NameValueCollection resourceKeys;
373 string implicitResourceKey;
374 SiteMapNodeCollection childNodes;