2 // System.Web.XmlSiteMapProvider
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;
36 using System.Configuration;
39 using System.Web.Util;
44 public class XmlSiteMapProvider : StaticSiteMapProvider, IDisposable
46 static readonly char [] seperators = { ';', ',' };
49 SiteMapNode root = null;
50 FileSystemWatcher watcher;
52 public override SiteMapNode BuildSiteMap ()
56 // Whenever you call AddNode, it tries to find dups, and will call this method
57 // Is this a bug in MS??
66 XmlDocument d = new XmlDocument ();
69 XmlNode nod = d.DocumentElement ["siteMapNode"];
71 throw new HttpException ("Invalid site map file: " + Path.GetFileName (file));
73 root = BuildSiteMapRecursive (nod);
83 string GetNonEmptyOptionalAttribute (XmlNode n, string name)
85 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
88 string GetOptionalAttribute (XmlNode n, string name)
90 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true, true);
94 SiteMapNode BuildSiteMapRecursive (XmlNode xmlNode)
96 if (xmlNode.Name != "siteMapNode")
97 throw new ConfigurationException ("incorrect element name", xmlNode);
99 string provider = GetNonEmptyOptionalAttribute (xmlNode, "provider");
100 string siteMapFile = GetNonEmptyOptionalAttribute (xmlNode, "siteMapFile");
102 if (provider != null) {
103 throw new NotImplementedException ();
104 } else if (siteMapFile != null) {
105 throw new NotImplementedException ();
108 string url = GetOptionalAttribute (xmlNode, "url");
109 string title = GetOptionalAttribute (xmlNode, "title");
110 string description = GetOptionalAttribute (xmlNode, "description");
111 string keywords = GetOptionalAttribute (xmlNode, "keywords");
112 string roles = GetOptionalAttribute (xmlNode, "roles");
114 ArrayList keywordsList = new ArrayList ();
115 if (keywords != null && keywords.Length > 0) {
116 foreach (string s in keywords.Split (seperators)) {
117 string ss = s.Trim ();
119 keywordsList.Add (ss);
123 ArrayList rolesList = new ArrayList ();
124 if (roles != null && roles.Length > 0) {
125 foreach (string s in roles.Split (seperators)) {
126 string ss = s.Trim ();
132 SiteMapNode node = new SiteMapNode (this, url, url, title, description,
133 /*ArrayList.ReadOnly (keywordsList), */ArrayList.ReadOnly (rolesList), null,
134 null, null); // TODO what do they want for attributes
136 foreach (XmlNode child in xmlNode.ChildNodes) {
137 if (child.NodeType != XmlNodeType.Element)
139 AddNode (BuildSiteMapRecursive (child), node);
146 protected override void Clear ()
152 public void Dispose ()
158 public override SiteMapNode FindSiteMapNode (string rawUrl)
160 return base.FindSiteMapNode (rawUrl); // why did they override this method!?
163 public override void Initialize (string name, NameValueCollection attributes)
166 base.Initialize (name, attributes);
167 file = attributes ["siteMapFile"];
169 if (file == null && file.Length == 0)
170 throw new ArgumentException ("you must provide a file");
172 if (UrlUtils.IsRelativeUrl (file))
173 file = Path.Combine(HttpRuntime.AppDomainAppPath, file);
175 file = UrlUtils.ResolvePhysicalPathFromAppAbsolute (file);
177 if (File.Exists (file)) {
178 watcher = new FileSystemWatcher ();
179 watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
180 watcher.Filter = Path.GetFileName (file);
181 watcher.Changed += new FileSystemEventHandler (OnFileChanged);
182 watcher.EnableRaisingEvents = true;
186 void OnFileChanged (object sender, FileSystemEventArgs args)
191 public override SiteMapNode RootNode {
198 protected internal override SiteMapNode GetRootNodeCore ()
200 return BuildSiteMap ();