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) {}
45 public SiteMapNode (SiteMapProvider provider, string key, string url) : this (provider, key, url, 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) {}
47 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description) : this (provider, key, url, title, description, null, null, null) {}
48 public SiteMapNode (SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection resourceKeys)
51 throw new ArgumentNullException ("provider");
53 this.provider = provider;
57 this.description = description;
59 this.attributes = attributes;
60 this.resourceKeys = resourceKeys;
63 public SiteMapDataSourceView GetDataSourceView (SiteMapDataSource owner, string viewName)
65 return new SiteMapDataSourceView (owner, viewName, this);
68 public SiteMapHierarchicalDataSourceView GetHierarchicalDataSourceView ()
70 return new SiteMapHierarchicalDataSourceView (this);
74 public bool IsAccessibleToUser (System.Web.HttpContext ctx)
76 throw new NotImplementedException ();
79 public override string ToString()
84 public virtual bool HasChildNodes { get { return ChildNodes != null && ChildNodes.Count != 0; } }
86 public SiteMapNodeCollection GetAllNodes ()
88 SiteMapNodeCollection ret;
90 ret = new SiteMapNodeCollection ();
91 GetAllNodesRecursive (ret);
92 return SiteMapNodeCollection.ReadOnly (ret);
95 void GetAllNodesRecursive(SiteMapNodeCollection c)
97 SiteMapNodeCollection childNodes = this.ChildNodes;
99 if (childNodes.Count > 0) {
100 childNodes.AddRange (childNodes);
101 foreach (SiteMapNode n in childNodes)
102 n.GetAllNodesRecursive (c);
107 public virtual bool IsDescendantOf (SiteMapNode node)
109 for (SiteMapNode n = ParentNode; n != null; n = n.ParentNode)
110 if (n == node) return true;
115 public virtual SiteMapNode NextSibling {
117 IList siblings = this.SiblingNodes;
118 if (siblings == null)
121 int pos = siblings.IndexOf (this);
122 if (pos >= 0 && pos < siblings.Count - 1)
123 return (SiteMapNode) siblings [pos + 1];
129 public virtual SiteMapNode PreviousSibling {
131 IList siblings = this.SiblingNodes;
132 if (siblings == null)
135 int pos = siblings.IndexOf (this);
136 if (pos > 0 && pos < siblings.Count)
137 return (SiteMapNode) siblings [pos - 1];
143 public virtual SiteMapNode ParentNode {
145 if (parent != null) return parent;
147 SiteMapProvider provider = this.provider;
150 parent = provider.GetParentNode (this);
154 provider = provider.ParentProvider;
155 } while (provider != null);
165 public virtual SiteMapNodeCollection ChildNodes {
166 get { return provider.GetChildNodes (this); }
167 set { CheckWritable (); }
170 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
172 SiteMapNodeCollection SiblingNodes {
174 if (ParentNode != null)
175 return ParentNode.ChildNodes;
182 protected string GetExplicitResourceString (string attributeName, bool b)
188 protected string GetImplicitResourceString (string attributeName)
193 [MonoTODO ("resource string?")]
194 public string this [string key]
198 if (provider.EnableLocalization) {
199 val = GetExplicitResourceString (key, true);
200 if (val == null) val = GetImplicitResourceString (key);
202 if (val != null) return null;
203 if (attributes != null) return attributes [key];
208 object ICloneable.Clone ()
210 return Clone (false);
213 public virtual SiteMapNode Clone ()
215 return Clone (false);
218 public virtual SiteMapNode Clone (bool cloneParentNodes)
220 SiteMapNode node = new SiteMapNode ();
221 node.provider = provider;
225 node.description = description;
226 node.roles = new ArrayList (roles);
227 node.attributes = new NameValueCollection (attributes);
228 if (cloneParentNodes && ParentNode != null)
229 node.parent = (SiteMapNode) ParentNode.Clone (true);
233 public override bool Equals (object ob)
235 SiteMapNode node = ob as SiteMapNode;
236 if (node == null) return false;
238 if (node.key != key ||
240 node.title != title ||
241 node.description != description) {
245 if ((roles == null || node.roles == null) && (roles != node.roles)) return false;
246 if (roles.Count != node.roles.Count) return false;
248 foreach (object role in roles)
249 if (!node.roles.Contains (role)) return false;
251 if ((attributes == null || node.attributes == null) && (attributes != node.attributes)) return false;
252 if (attributes.Count != node.attributes.Count) return false;
254 foreach (string k in attributes)
255 if (attributes [k] != node.attributes [k]) return false;
260 public override int GetHashCode ()
262 return (key + url + title + description).GetHashCode ();
265 void CheckWritable ()
268 throw new InvalidOperationException ("Can't modify read-only node");
271 #region Field Accessors
273 public virtual NameValueCollection Attributes {
274 get { return attributes; }
275 set { CheckWritable (); attributes = value; }
278 public virtual string Description {
279 get { return description != null ? description : ""; }
280 set { CheckWritable (); description = value; }
283 [LocalizableAttribute (true)]
284 public virtual string Title {
285 get { return title != null ? title : ""; }
286 set { CheckWritable (); title = value; }
289 public virtual string Url {
290 get { return url != null ? url : ""; }
291 set { CheckWritable (); url = value; }
294 public virtual IList Roles {
295 get { return roles; }
296 set { CheckWritable (); roles = value; }
299 public bool ReadOnly {
300 get { return readOnly; }
301 set { readOnly = value; }
304 [MonoTODO ("Do somethig with this")]
305 public string ResourceKey {
306 get { return resourceKey; }
307 set { resourceKey = value; }
310 public string Key { get { return key; } }
311 public SiteMapProvider Provider { get { return provider; } }
315 #region INavigateUIData
316 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
317 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetParent ()
319 if (ParentNode == null) return null;
320 return SiteMapNodeCollection.ReadOnly (new SiteMapNodeCollection (ParentNode));
323 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
324 object System.Web.UI.IHierarchyData.Item { get { return this; } }
325 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
326 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
329 #region INavigateUIData
330 string INavigateUIData.Name { get { return Title; } }
331 string INavigateUIData.NavigateUrl { get { return Url; } }
332 string INavigateUIData.Value { get { return Title; } }
336 SiteMapProvider provider;
342 NameValueCollection attributes;
343 NameValueCollection resourceKeys;