2006-02-01 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / SiteMap.cs
1 //
2 // System.Web.SiteMap
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Configuration.Provider;
36 using System.Configuration;
37 using System.Web.Configuration;
38
39 namespace System.Web {
40         public static class SiteMap {
41         
42                 private static void Init ()
43                 {
44                         lock (locker) {
45                                 if (provider == null) {
46                                         SiteMapSection section = (SiteMapSection) WebConfigurationManager.GetSection ("system.web/siteMap");
47                                         providers = new SiteMapProviderCollection ();
48                                         
49                                         if (section.Enabled) {
50                                                 foreach (ProviderSettings prov in section.Providers) {
51                                                         Type t = Type.GetType (prov.Type);
52                                                         if (t == null)
53                                                                 throw new ConfigurationException ("Cannot find type: " + prov.Type);
54                                                         if (!typeof(SiteMapProvider).IsAssignableFrom (t))
55                                                                 throw new ConfigurationException ("The provided type is not a SiteMapProvider subclass: " + prov.Type);
56                                                         
57                                                         SiteMapProvider pr = (SiteMapProvider) Activator.CreateInstance (t);
58                                                         pr.Initialize (prov.Name, prov.Parameters);
59                                                         
60                                                         if (provider == null || prov.Name == section.DefaultProvider)
61                                                                 provider = pr;
62
63                                                         providers.Add (pr);
64                                                 }
65                                         }
66                                         
67                                         if (providers.Count == 0) {
68                                                 provider = new XmlSiteMapProvider ();
69                                                 NameValueCollection attributes = new NameValueCollection ();
70                                                 attributes.Add ("siteMapFile", "Web.sitemap");
71                                                 provider.Initialize ("AspNetXmlSiteMapProvider", attributes);
72                                                 providers.Add (provider);
73                                         }
74                                 }
75                         }
76                 }
77                 
78                 public static SiteMapNode CurrentNode { 
79                         get { return Provider.CurrentNode; }
80                 }
81                 public static SiteMapNode RootNode { 
82                         get { return Provider.RootNode; }
83                 }
84                 
85                 public static SiteMapProvider Provider {
86                         get {
87                                 Init ();
88                                 return provider;
89                         }
90                 }
91                 public static SiteMapProviderCollection Providers {
92                         get {
93                                 Init ();
94                                 return providers;
95                         }
96                 }
97                 
98                 public static event SiteMapResolveEventHandler SiteMapResolve {
99                         add { Provider.SiteMapResolve += value; }
100                         remove { Provider.SiteMapResolve -= value; }
101                 }
102
103                 [MonoTODO ("By now it always return 'true'")]
104                 public static bool Enabled {
105                         get { return true; }
106                 }
107
108                 static SiteMapProvider provider;
109                 static SiteMapProviderCollection providers;
110                 static object locker = new object ();
111         }
112 }
113 #endif
114