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;
37 using System.Globalization;
40 using System.Web.Util;
45 public class XmlSiteMapProvider : StaticSiteMapProvider, IDisposable
47 static readonly char [] seperators = { ';', ',' };
50 SiteMapNode root = null;
51 FileSystemWatcher watcher;
53 protected internal override void AddNode (SiteMapNode node, SiteMapNode parentNode)
55 base.AddNode (node, parentNode);
58 protected virtual void AddProvider (string providerName, SiteMapNode parentNode)
60 throw new NotImplementedException ();
63 XmlNode FindStartingNode (string file, out bool enableLocalization)
65 XmlDocument d = new XmlDocument ();
68 XmlNode enloc = d.DocumentElement.Attributes ["enableLocalization"];
69 if (enloc != null && !String.IsNullOrEmpty (enloc.Value))
70 enableLocalization = (bool) Convert.ChangeType (enloc.Value, typeof (bool));
72 enableLocalization = false;
74 XmlNode nod = d.DocumentElement ["siteMapNode"];
76 throw new HttpException ("Invalid site map file: " + Path.GetFileName (file));
81 public override SiteMapNode BuildSiteMap ()
85 // Whenever you call AddNode, it tries to find dups, and will call this method
86 // Is this a bug in MS??
96 bool enableLocalization;
97 XmlNode node = FindStartingNode (file, out enableLocalization);
98 EnableLocalization = enableLocalization;
99 root = BuildSiteMapRecursive (node, EnableLocalization);
109 string GetNonEmptyOptionalAttribute (XmlNode n, string name)
111 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
114 string GetOptionalAttribute (XmlNode n, string name)
116 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true, true);
119 void PutInCollection (string name, string value, ref NameValueCollection coll)
121 PutInCollection (name, null, value, ref coll);
124 void PutInCollection (string name, string classKey, string value, ref NameValueCollection coll)
127 coll = new NameValueCollection ();
128 if (!String.IsNullOrEmpty (classKey))
129 coll.Add (name, classKey);
130 coll.Add (name, value);
133 bool GetAttributeLocalization (string value, out string resClass, out string resKey, out string resDefault)
139 if (String.IsNullOrEmpty (value))
141 string val = value.TrimStart (new char[] {' ', '\t'});
142 if (val.Length < 11 ||
143 String.Compare (val, 0, "$resources:", 0, 11, StringComparison.InvariantCultureIgnoreCase) != 0)
146 val = val.Substring (11);
149 string[] parts = val.Split (',');
150 if (parts.Length < 2)
152 resClass = parts [0].Trim ();
153 resKey = parts [1].Trim ();
154 if (parts.Length == 3)
155 resDefault = parts [2];
156 else if (parts.Length > 3)
157 resDefault = String.Join (",", parts, 2, parts.Length - 2);
162 void CollectLocalizationInfo (XmlNode xmlNode, ref string title, ref string description,
163 ref NameValueCollection attributes,
164 ref NameValueCollection explicitResourceKeys)
170 if (GetAttributeLocalization (title, out resClass, out resKey, out resDefault)) {
171 PutInCollection ("title", resClass, resKey, ref explicitResourceKeys);
175 if (GetAttributeLocalization (description, out resClass, out resKey, out resDefault)) {
176 PutInCollection ("description", resClass, resKey, ref explicitResourceKeys);
177 description = resDefault;
181 foreach (XmlNode att in xmlNode.Attributes) {
182 if (GetAttributeLocalization (att.Value, out resClass, out resKey, out resDefault)) {
183 PutInCollection (att.Name, resClass, resKey, ref explicitResourceKeys);
187 PutInCollection (att.Name, value, ref attributes);
191 SiteMapNode BuildSiteMapRecursive (XmlNode xmlNode, bool localize)
193 if (xmlNode.Name != "siteMapNode")
194 throw new ConfigurationException ("incorrect element name", xmlNode);
196 string provider = GetNonEmptyOptionalAttribute (xmlNode, "provider");
197 string siteMapFile = GetNonEmptyOptionalAttribute (xmlNode, "siteMapFile");
199 if (provider != null) {
200 foreach (SiteMapProvider smp in SiteMap.Providers) {
201 if (string.Equals(smp.Name,provider, StringComparison.InvariantCulture)) {
202 smp.ParentProvider = this;
203 return smp.GetRootNodeCore();
206 throw new ConfigurationException("Provider with name [" + provider + "] was not found.");
207 } else if (siteMapFile != null) {
208 bool enableLocalization;
209 XmlNode node = FindStartingNode (HttpContext.Current.Request.MapPath (siteMapFile),
210 out enableLocalization);
211 return BuildSiteMapRecursive (node, enableLocalization);
213 string url = GetOptionalAttribute (xmlNode, "url");
214 string title = GetOptionalAttribute (xmlNode, "title");
215 string description = GetOptionalAttribute (xmlNode, "description");
216 string keywords = GetOptionalAttribute (xmlNode, "keywords");
217 string roles = GetOptionalAttribute (xmlNode, "roles");
218 string implicitResourceKey = GetOptionalAttribute (xmlNode, "resourceKey");
220 ArrayList keywordsList = new ArrayList ();
221 if (keywords != null && keywords.Length > 0) {
222 foreach (string s in keywords.Split (seperators)) {
223 string ss = s.Trim ();
225 keywordsList.Add (ss);
229 ArrayList rolesList = new ArrayList ();
230 if (roles != null && roles.Length > 0) {
231 foreach (string s in roles.Split (seperators)) {
232 string ss = s.Trim ();
238 if (!string.IsNullOrEmpty (url)) {
239 if (UrlUtils.IsRelativeUrl (url))
240 url = UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
243 NameValueCollection attributes = null;
244 NameValueCollection explicitResourceKeys = null;
246 CollectLocalizationInfo (xmlNode, ref title, ref description, ref attributes,
247 ref explicitResourceKeys);
249 foreach (XmlNode att in xmlNode.Attributes)
250 PutInCollection (att.Name, att.Value, ref attributes);
252 string key = Guid.NewGuid ().ToString ();
253 SiteMapNode node = new SiteMapNode (this, key, url, title, description,
254 ArrayList.ReadOnly (rolesList),
256 explicitResourceKeys,
257 implicitResourceKey);
259 foreach (XmlNode child in xmlNode.ChildNodes) {
260 if (child.NodeType != XmlNodeType.Element)
262 AddNode (BuildSiteMapRecursive (child, EnableLocalization), node);
269 protected override void Clear ()
275 protected virtual void Dispose (bool disposing)
281 public void Dispose ()
286 public override SiteMapNode FindSiteMapNode (string rawUrl)
288 return base.FindSiteMapNode (rawUrl); // why did they override this method!?
291 public override SiteMapNode FindSiteMapNodeFromKey (string key)
293 return base.FindSiteMapNodeFromKey (key); // why did they override this method!?
296 public override void Initialize (string name, NameValueCollection attributes)
299 base.Initialize (name, attributes);
300 file = attributes ["siteMapFile"];
302 if (file == null && file.Length == 0)
303 throw new ArgumentException ("you must provide a file");
305 if (HttpContext.Current != null)
306 file = HttpContext.Current.Request.MapPath (file, HttpRuntime.AppDomainAppVirtualPath, false);
308 throw new InvalidOperationException ("HttpContext is missing");
310 if (File.Exists (file)) {
311 watcher = new FileSystemWatcher ();
312 watcher.NotifyFilter |= NotifyFilters.Size;
313 watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
314 watcher.Filter = Path.GetFileName (file);
315 watcher.Changed += new FileSystemEventHandler (OnFileChanged);
316 watcher.EnableRaisingEvents = true;
320 protected override void RemoveNode (SiteMapNode node)
322 base.RemoveNode (node);
325 [MonoTODO ("Not implemented")]
326 protected virtual void RemoveProvider (string providerName)
328 throw new NotImplementedException ();
331 void OnFileChanged (object sender, FileSystemEventArgs args)
336 public override SiteMapNode RootNode {
343 protected internal override SiteMapNode GetRootNodeCore ()
345 return BuildSiteMap ();