2 // System.Web.SiteMapProvider
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;
35 using System.Configuration.Provider;
36 using System.Web.Util;
37 using System.Globalization;
39 namespace System.Web {
40 public abstract class SiteMapProvider : ProviderBase {
42 bool enableLocalization;
44 public void AddNode (SiteMapNode node)
49 public void AddNode (SiteMapNode node, SiteMapNode parentNode)
52 throw new ArgumentNullException ("node");
55 string url = node.Url;
56 if (url != null && url.Length > 0) {
59 if (UrlUtils.IsRelativeUrl (url))
60 url = UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
62 url = UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
64 if (FindSiteMapNode (url) != null)
65 throw new InvalidOperationException ();
67 UrlToNode [url] = node;
70 if (parentNode != null) {
71 NodeToParent [node] = parentNode;
72 if (NodeToChildren [parentNode] == null)
73 NodeToChildren [parentNode] = new SiteMapNodeCollection ();
75 ((SiteMapNodeCollection) NodeToChildren [parentNode]).Add (node);
80 Hashtable nodeToParent;
81 Hashtable NodeToParent {
83 if (nodeToParent == null) {
85 if (nodeToParent == null)
86 nodeToParent = new Hashtable ();
93 Hashtable nodeToChildren;
94 Hashtable NodeToChildren {
96 if (nodeToChildren == null) {
98 if (nodeToChildren == null)
99 nodeToChildren = new Hashtable ();
102 return nodeToChildren;
107 Hashtable UrlToNode {
109 if (urlToNode == null) {
111 if (urlToNode == null) {
112 urlToNode = new Hashtable (
113 new CaseInsensitiveHashCodeProvider (),
114 new CaseInsensitiveComparer ()
123 protected virtual void Clear ()
126 if (urlToNode != null)
128 if (nodeToChildren != null)
129 nodeToChildren.Clear ();
130 if (nodeToParent != null)
131 nodeToParent.Clear ();
135 public virtual SiteMapNode FindSiteMapNode (string rawUrl)
138 throw new ArgumentNullException ("rawUrl");
140 if (rawUrl.Length > 0) {
142 rawUrl = UrlUtils.ResolveVirtualPathFromAppAbsolute (rawUrl);
143 return (SiteMapNode) UrlToNode [rawUrl];
148 public virtual SiteMapNodeCollection GetChildNodes (SiteMapNode node)
151 throw new ArgumentNullException ("node");
154 SiteMapNodeCollection ret = (SiteMapNodeCollection) NodeToChildren [node];
157 return SiteMapNodeCollection.ReadOnly (ret);
162 public virtual SiteMapNode GetParentNode(SiteMapNode node) {
164 throw new ArgumentNullException ("node");
166 return (SiteMapNode) NodeToParent [node];
169 public void RemoveNode (SiteMapNode node)
173 throw new ArgumentNullException("node");
176 SiteMapNode parent = (SiteMapNode) NodeToParent [node];
177 if (NodeToParent.Contains (node))
178 NodeToParent.Remove (node);
180 if (node.Url != null && node.Url.Length > 0 && UrlToNode.Contains (node.Url))
181 UrlToNode.Remove (node.Url);
183 if (parent != null) {
184 SiteMapNodeCollection siblings = (SiteMapNodeCollection) NodeToChildren [node];
185 if (siblings != null && siblings.Contains (node))
186 siblings.Remove (node);
191 public override void Initialize (string name, NameValueCollection attributes)
193 if (attributes != null)
194 description = attributes ["description"];
198 public virtual SiteMapNode CurrentNode {
202 if (HttpContext.Current != null) {
203 ret = this.FindSiteMapNode (HttpContext.Current.Request.RawUrl);
205 ret = this.FindSiteMapNode (HttpContext.Current.Request.Path);
215 public virtual string Description {
216 get { return description != null ? description : "SiteMapProvider"; }
219 SiteMapProvider parentProvider;
220 public virtual SiteMapProvider ParentProvider {
221 get { return parentProvider; }
222 set { parentProvider = value; }
225 SiteMapProvider rootProviderCache;
226 public virtual SiteMapProvider RootProvider {
228 if (rootProviderCache == null) {
230 if (rootProviderCache == null) {
231 SiteMapProvider current = this;
232 while (current.ParentProvider != null)
233 current = current.ParentProvider;
235 rootProviderCache = current;
239 return rootProviderCache;
243 public bool EnableLocalization {
244 get { return enableLocalization; }
245 set { enableLocalization = value; }
248 public abstract SiteMapNode BuildSiteMap ();
249 public abstract SiteMapNode RootNode { get; }