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;
39 namespace System.Web {
40 public class SiteMapNode : IHierarchyData, INavigateUIData, ICloneable {
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.resourceKey = 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 != null && childNodes.Count > 0) {
111 c.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 internal SiteMapNodeCollection ChildNodesInternal {
177 if (childNodes == null)
178 childNodes = new SiteMapNodeCollection ();
183 public virtual SiteMapNodeCollection ChildNodes {
185 if (childNodes != null) return childNodes;
186 return provider.GetChildNodes (this);
194 public virtual SiteMapNode RootNode { get { return provider.RootProvider.RootNode; } }
196 SiteMapNodeCollection SiblingNodes {
198 if (ParentNode != null)
199 return ParentNode.ChildNodes;
205 protected string GetExplicitResourceString (string attributeName, string defaultValue, bool throwIfNotFound)
207 if (attributeName == null)
208 throw new ArgumentNullException ("attributeName");
210 if (resourceKeys != null){
211 string[] values = resourceKeys.GetValues (attributeName);
212 if (values != null && values.Length == 2) {
214 object o = HttpContext.GetGlobalResourceObject (values [0], values [1]);
218 catch (MissingManifestResourceException) {
221 if (throwIfNotFound && defaultValue == null)
222 throw new InvalidOperationException (String.Format ("The resource object with classname '{0}' and key '{1}' was not found.", values [0], values [1]));
229 protected string GetImplicitResourceString (string attributeName)
231 if (attributeName == null)
232 throw new ArgumentNullException ("attributeName");
234 string resourceKey = ResourceKey;
235 if (String.IsNullOrEmpty (resourceKey))
239 object o = HttpContext.GetGlobalResourceObject (provider.ResourceKey, resourceKey + "." + attributeName);
242 } catch (MissingManifestResourceException) {
248 public virtual string this [string key]
251 if (provider.EnableLocalization) {
252 string val = GetImplicitResourceString (key);
254 val = GetExplicitResourceString (key, null, true);
258 if (attributes != null) return attributes [key];
263 if (attributes == null) attributes = new NameValueCollection ();
264 attributes [key] = value;
268 object ICloneable.Clone ()
270 return Clone (false);
273 public virtual SiteMapNode Clone ()
275 return Clone (false);
278 public virtual SiteMapNode Clone (bool cloneParentNodes)
280 SiteMapNode node = new SiteMapNode ();
281 node.provider = provider;
285 node.description = description;
287 node.roles = new ArrayList (roles);
288 if (attributes != null)
289 node.attributes = new NameValueCollection (attributes);
290 if (cloneParentNodes && ParentNode != null)
291 node.parent = (SiteMapNode) ParentNode.Clone (true);
295 public override bool Equals (object ob)
297 SiteMapNode node = ob as SiteMapNode;
298 if (node == null) return false;
300 if (node.key != key ||
302 node.title != title ||
303 node.description != description) {
307 if (roles == null || node.roles == null) {
308 if (roles != node.roles)
312 if (roles.Count != node.roles.Count)
315 foreach (object role in roles)
316 if (!node.roles.Contains (role)) return false;
318 if (attributes == null || node.attributes == null) {
319 if (attributes != node.attributes)
323 if (attributes.Count != node.attributes.Count)
326 foreach (string k in attributes)
327 if (attributes[k] != node.attributes[k])
333 public override int GetHashCode ()
335 return (key + url + title + description).GetHashCode ();
338 void CheckWritable ()
341 throw new InvalidOperationException ("Can't modify read-only node");
344 #region Field Accessors
346 protected NameValueCollection Attributes {
347 get { return attributes; }
348 set { CheckWritable (); attributes = value; }
352 public virtual string Description {
356 if (provider.EnableLocalization) {
357 ret = GetImplicitResourceString ("description");
359 ret = GetExplicitResourceString ("description", description, true);
363 return ret != null ? ret : String.Empty;
365 set { CheckWritable (); description = value; }
368 [LocalizableAttribute (true)]
369 public virtual string Title {
373 if (provider.EnableLocalization) {
374 ret = GetImplicitResourceString ("title");
376 ret = GetExplicitResourceString ("title", title, true);
380 return ret != null ? ret : String.Empty;
382 set { CheckWritable (); title = value; }
385 public virtual string Url {
386 get { return url != null ? url : ""; }
387 set { CheckWritable (); url = value; }
391 get { return roles; }
392 set { CheckWritable (); roles = value; }
395 public bool ReadOnly {
396 get { return readOnly; }
397 set { readOnly = value; }
400 public string ResourceKey {
401 get { return resourceKey; }
404 throw new InvalidOperationException ("The node is read-only.");
409 public string Key { get { return key; } }
410 public SiteMapProvider Provider { get { return provider; } }
414 #region INavigateUIData
415 IHierarchicalEnumerable System.Web.UI.IHierarchyData.GetChildren () { return ChildNodes; }
416 IHierarchyData System.Web.UI.IHierarchyData.GetParent ()
421 bool System.Web.UI.IHierarchyData.HasChildren { get { return HasChildNodes; } }
422 object System.Web.UI.IHierarchyData.Item { get { return this; } }
423 string System.Web.UI.IHierarchyData.Path { get { return Url; } }
424 string System.Web.UI.IHierarchyData.Type { get { return "SiteMapNode"; } }
427 #region INavigateUIData
428 string INavigateUIData.Name { get { return Title; } }
429 string INavigateUIData.NavigateUrl { get { return Url; } }
430 string INavigateUIData.Value { get { return Title; } }
434 SiteMapProvider provider;
440 NameValueCollection attributes;
441 NameValueCollection resourceKeys;
445 SiteMapNodeCollection childNodes;