2 // System.Web.SiteMapProvider
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Lluis Sanchez Gual (lluis@novell.com)
9 // (C) 2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Specialized;
37 using System.Configuration.Provider;
38 using System.Web.Util;
39 using System.Globalization;
41 namespace System.Web {
42 public abstract class SiteMapProvider : ProviderBase {
44 bool enableLocalization;
45 SiteMapProvider parentProvider;
46 SiteMapProvider rootProviderCache;
47 bool securityTrimming;
48 object resolveLock = new Object();
51 protected virtual void AddNode (SiteMapNode node)
56 internal protected virtual void AddNode (SiteMapNode node, SiteMapNode parentNode)
59 throw new ArgumentNullException ("node");
62 public virtual SiteMapNode FindSiteMapNode (HttpContext context)
65 throw new ArgumentNullException ("context");
67 SiteMapNode ret = this.FindSiteMapNode (context.Request.RawUrl);
69 ret = this.FindSiteMapNode (context.Request.Path);
73 public abstract SiteMapNode FindSiteMapNode (string rawUrl);
75 public virtual SiteMapNode FindSiteMapNodeFromKey (string key)
78 throw new ArgumentNullException ("key");
83 public abstract SiteMapNodeCollection GetChildNodes (SiteMapNode node);
85 public abstract SiteMapNode GetParentNode (SiteMapNode node);
87 public virtual SiteMapNode GetCurrentNodeAndHintAncestorNodes (int upLevel)
89 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
94 public virtual SiteMapNode GetCurrentNodeAndHintNeighborhoodNodes (int upLevel, int downLevel)
96 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
97 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
102 public virtual SiteMapNode GetParentNodeRelativeToCurrentNodeAndHintDownFromParent (int walkupLevels, int relativeDepthFromWalkup)
104 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
105 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
107 SiteMapNode node = GetCurrentNodeAndHintAncestorNodes (walkupLevels);
108 for (int n=0; n<walkupLevels && node != null; n++)
109 node = GetParentNode (node);
111 if (node == null) return null;
113 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
117 public virtual SiteMapNode GetParentNodeRelativeToNodeAndHintDownFromParent (SiteMapNode node, int walkupLevels, int relativeDepthFromWalkup)
119 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
120 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
121 if (node == null) throw new ArgumentNullException ("node");
123 HintAncestorNodes (node, walkupLevels);
124 for (int n=0; n<walkupLevels && node != null; n++)
125 node = GetParentNode (node);
127 if (node == null) return null;
129 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
133 protected internal abstract SiteMapNode GetRootNodeCore ();
135 protected static SiteMapNode GetRootNodeCoreFromProvider (SiteMapProvider provider)
137 return provider.GetRootNodeCore ();
140 public virtual void HintAncestorNodes (SiteMapNode node, int upLevel)
142 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
143 if (node == null) throw new ArgumentNullException ("node");
146 public virtual void HintNeighborhoodNodes (SiteMapNode node, int upLevel, int downLevel)
148 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
149 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
150 if (node == null) throw new ArgumentNullException ("node");
153 public virtual void RemoveNode (SiteMapNode node)
156 throw new ArgumentNullException("node");
159 public override void Initialize (string name, NameValueCollection attributes)
161 base.Initialize (name, attributes);
162 if (attributes["securityTrimmingEnabled"] != null)
163 securityTrimming = (bool) Convert.ChangeType (attributes ["securityTrimmingEnabled"], typeof(bool));
167 public virtual bool IsAccessibleToUser (HttpContext context, SiteMapNode node)
172 public virtual SiteMapNode CurrentNode {
174 if (HttpContext.Current != null) {
175 SiteMapNode ret = ResolveSiteMapNode (HttpContext.Current);
176 if (ret != null) return ret;
177 return FindSiteMapNode (HttpContext.Current);
183 public virtual SiteMapProvider ParentProvider {
184 get { return parentProvider; }
185 set { parentProvider = value; }
188 public virtual SiteMapProvider RootProvider {
191 if (rootProviderCache == null) {
192 SiteMapProvider current = this;
193 while (current.ParentProvider != null)
194 current = current.ParentProvider;
196 rootProviderCache = current;
199 return rootProviderCache;
203 protected SiteMapNode ResolveSiteMapNode (HttpContext context)
205 SiteMapResolveEventArgs args = new SiteMapResolveEventArgs (context, this);
206 if (SiteMapResolve != null) {
208 if (resolving) return null;
210 SiteMapNode r = SiteMapResolve (this, args);
219 public bool EnableLocalization {
220 get { return enableLocalization; }
221 set { enableLocalization = value; }
224 public bool SecurityTrimmingEnabled {
225 get { return securityTrimming; }
228 public virtual SiteMapNode RootNode {
230 SiteMapNode node = GetRootNodeCore ();
231 if (IsAccessibleToUser (HttpContext.Current, node))
238 public event SiteMapResolveEventHandler SiteMapResolve;