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
: ISiteMapProvider
, IProvider
{
42 public void AddNode (SiteMapNode node
)
47 public void AddNode (SiteMapNode node
, SiteMapNode parentNode
)
50 throw new ArgumentNullException ("node");
53 string url
= node
.Url
;
54 if (url
!= null && url
.Length
> 0) {
57 if (UrlUtils
.IsRelativeUrl (url
))
58 url
= UrlUtils
.Combine (HttpRuntime
.AppDomainAppVirtualPath
, url
);
60 url
= UrlUtils
.ResolveVirtualPathFromAppAbsolute (url
);
62 if (FindSiteMapNode (url
) != null)
63 throw new InvalidOperationException ();
65 UrlToNode
[url
] = node
;
68 if (parentNode
!= null) {
69 NodeToParent
[node
] = parentNode
;
70 if (NodeToChildren
[parentNode
] == null)
71 NodeToChildren
[parentNode
] = new SiteMapNodeCollection ();
73 ((SiteMapNodeCollection
) NodeToChildren
[parentNode
]).Add (node
);
78 Hashtable nodeToParent
;
79 Hashtable NodeToParent
{
81 if (nodeToParent
== null) {
83 if (nodeToParent
== null)
84 nodeToParent
= new Hashtable ();
91 Hashtable nodeToChildren
;
92 Hashtable NodeToChildren
{
94 if (nodeToChildren
== null) {
96 if (nodeToChildren
== null)
97 nodeToChildren
= new Hashtable ();
100 return nodeToChildren
;
105 Hashtable UrlToNode
{
107 if (urlToNode
== null) {
109 if (urlToNode
== null) {
110 urlToNode
= new Hashtable (
111 new CaseInsensitiveHashCodeProvider (),
112 new CaseInsensitiveComparer ()
121 protected virtual void Clear ()
124 if (urlToNode
!= null)
126 if (nodeToChildren
!= null)
127 nodeToChildren
.Clear ();
128 if (nodeToParent
!= null)
129 nodeToParent
.Clear ();
133 public virtual SiteMapNode
FindSiteMapNode (string rawUrl
)
136 throw new ArgumentNullException ("rawUrl");
138 if (rawUrl
.Length
> 0) {
140 rawUrl
= UrlUtils
.ResolveVirtualPathFromAppAbsolute (rawUrl
);
141 return (SiteMapNode
) UrlToNode
[rawUrl
];
146 public virtual SiteMapNodeCollection
GetChildNodes (SiteMapNode node
)
149 throw new ArgumentNullException ("node");
152 SiteMapNodeCollection ret
= (SiteMapNodeCollection
) NodeToChildren
[node
];
155 return SiteMapNodeCollection
.ReadOnly (ret
);
160 public virtual SiteMapNode
GetParentNode(SiteMapNode node
) {
162 throw new ArgumentNullException ("node");
164 return (SiteMapNode
) NodeToParent
[node
];
167 public void RemoveNode (SiteMapNode node
)
171 throw new ArgumentNullException("node");
174 SiteMapNode parent
= (SiteMapNode
) NodeToParent
[node
];
175 if (NodeToParent
.Contains (node
))
176 NodeToParent
.Remove (node
);
178 if (node
.Url
!= null && node
.Url
.Length
> 0 && UrlToNode
.Contains (node
.Url
))
179 UrlToNode
.Remove (node
.Url
);
181 if (parent
!= null) {
182 SiteMapNodeCollection siblings
= (SiteMapNodeCollection
) NodeToChildren
[node
];
183 if (siblings
!= null && siblings
.Contains (node
))
184 siblings
.Remove (node
);
189 public virtual void Initialize (string name
, NameValueCollection attributes
)
192 if (attributes
!= null)
193 description
= attributes
["description"];
197 public virtual SiteMapNode CurrentNode
{
201 if (HttpContext
.Current
!= null) {
202 ret
= this.FindSiteMapNode (HttpContext
.Current
.Request
.RawUrl
);
204 ret
= this.FindSiteMapNode (HttpContext
.Current
.Request
.Path
);
214 public virtual string Description
{
215 get { return description != null ? description : "SiteMapProvider"; }
219 public virtual string Name
{
223 ISiteMapProvider parentProvider
;
224 public virtual ISiteMapProvider ParentProvider
{
225 get { return parentProvider; }
226 set { parentProvider = value; }
229 ISiteMapProvider rootProviderCache
;
230 public virtual ISiteMapProvider RootProvider
{
232 if (rootProviderCache
== null) {
234 if (rootProviderCache
== null) {
235 ISiteMapProvider current
= this;
236 while (current
.ParentProvider
!= null)
237 current
= current
.ParentProvider
;
239 rootProviderCache
= current
;
243 return rootProviderCache
;
247 public abstract SiteMapNode
BuildSiteMap ();
248 public abstract SiteMapNode RootNode { get; }