2007-05-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / XmlSiteMapProvider.cs
1 //
2 // System.Web.XmlSiteMapProvider
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Configuration;
37 using System.Globalization;
38 using System.Text;
39 using System.Xml;
40 using System.Web.Util;
41 using System.IO;
42
43 namespace System.Web
44 {
45         public class XmlSiteMapProvider : StaticSiteMapProvider, IDisposable
46         {
47                 static readonly char [] seperators = { ';', ',' };
48                 bool building;
49                 string file;
50                 SiteMapNode root = null;
51                 FileSystemWatcher watcher;
52
53                 protected internal override void AddNode (SiteMapNode node, SiteMapNode parentNode)
54                 {
55                         base.AddNode (node, parentNode);
56                 }
57
58                 protected virtual void AddProvider (string providerName, SiteMapNode parentNode)
59                 {
60                         throw new NotImplementedException ();
61                 }
62
63                 XmlNode FindStartingNode (string file, out bool enableLocalization)
64                 {
65                         XmlDocument d = new XmlDocument ();
66                         d.Load (file);
67
68                         XmlNode enloc = d.DocumentElement.Attributes ["enableLocalization"];
69                         if (enloc != null && !String.IsNullOrEmpty (enloc.Value))
70                                 enableLocalization = (bool) Convert.ChangeType (enloc.Value, typeof (bool));
71                         else
72                                 enableLocalization = false;
73                                         
74                         XmlNode nod = d.DocumentElement ["siteMapNode"];
75                         if (nod == null)
76                                 throw new HttpException ("Invalid site map file: " + Path.GetFileName (file));
77
78                         return nod;
79                 }
80                 
81                 public override SiteMapNode BuildSiteMap ()
82                 {
83                         if (root != null)
84                                 return root;
85                         // Whenever you call AddNode, it tries to find dups, and will call this method
86                         // Is this a bug in MS??
87                         if (building)
88                                 return null;
89                         
90                         lock (this) {
91                                 try {
92                                         building = true;
93                                         if (root != null)
94                                                 return root;
95
96                                         bool enableLocalization;
97                                         XmlNode node = FindStartingNode (file, out enableLocalization);
98                                         EnableLocalization = enableLocalization;
99                                         root = BuildSiteMapRecursive (node, EnableLocalization);
100                                                 
101                                         AddNode (root);
102                                 } finally {
103                                         building = false;
104                                 }
105                                 return root;
106                         }
107                 }
108                 
109                 string GetNonEmptyOptionalAttribute (XmlNode n, string name)
110                 {
111                         return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
112                 }
113                 
114                 string GetOptionalAttribute (XmlNode n, string name)
115                 {
116                         return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true, true);
117                 }
118
119                 void PutInCollection (string name, string value, ref NameValueCollection coll)
120                 {
121                         PutInCollection (name, null, value, ref coll);
122                 }
123                 
124                 void PutInCollection (string name, string classKey, string value, ref NameValueCollection coll)
125                 {
126                         if (coll == null)
127                                 coll = new NameValueCollection ();
128                         if (!String.IsNullOrEmpty (classKey))
129                                 coll.Add (name, classKey);
130                         coll.Add (name, value);
131                 }
132
133                 bool GetAttributeLocalization (string value, out string resClass, out string resKey, out string resDefault)
134                 {
135                         resClass = null;
136                         resKey = null;
137                         resDefault = null;
138
139                         if (String.IsNullOrEmpty (value))
140                                 return false;
141                         string val = value.TrimStart (new char[] {' ', '\t'});
142                         if (val.Length < 11 ||
143                                 String.Compare (val, 0, "$resources:", 0, 11, StringComparison.InvariantCultureIgnoreCase) != 0)
144                                 return false;
145
146                         val = val.Substring (11);
147                         if (val.Length == 0)
148                                 return false;
149                         string[] parts = val.Split (',');
150                         if (parts.Length < 2)
151                                 return false;
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);
158
159                         return true;
160                 }
161                 
162                 void CollectLocalizationInfo (XmlNode xmlNode, ref string title, ref string description,
163                                               ref NameValueCollection attributes,
164                                               ref NameValueCollection explicitResourceKeys)
165                 {
166                         string resClass;
167                         string resKey;
168                         string resDefault;
169
170                         if (GetAttributeLocalization (title, out resClass, out resKey, out resDefault)) {
171                                 PutInCollection ("title", resClass, resKey, ref explicitResourceKeys);
172                                 title = resDefault;
173                         }
174                         
175                         if (GetAttributeLocalization (description, out resClass, out resKey, out resDefault)) {
176                                 PutInCollection ("description", resClass, resKey, ref explicitResourceKeys);
177                                 description = resDefault;
178                         }
179
180                         string value;
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);
184                                         value = resDefault;
185                                 } else
186                                         value = att.Value;
187                                 PutInCollection (att.Name, value, ref attributes);
188                         }
189                 }
190                 
191                 SiteMapNode BuildSiteMapRecursive (XmlNode xmlNode, bool localize)
192                 {
193                         if (xmlNode.Name != "siteMapNode")
194                                 throw new ConfigurationException ("incorrect element name", xmlNode);
195                         
196                         string provider = GetNonEmptyOptionalAttribute (xmlNode, "provider");
197                         string siteMapFile = GetNonEmptyOptionalAttribute (xmlNode, "siteMapFile");
198                         
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();
204                                         }
205                                 }
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);
212                         } else {
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");
219                                 
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 ();
224                                                 if (ss.Length > 0)
225                                                         keywordsList.Add (ss);
226                                         }
227                                 }
228                                 
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 ();
233                                                 if (ss.Length > 0)
234                                                         rolesList.Add (ss);
235                                         }
236                                 }
237
238                                 if (!string.IsNullOrEmpty (url)) {
239                                         if (UrlUtils.IsRelativeUrl (url))
240                                                 url = UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
241                                 }
242
243                                 NameValueCollection attributes = null;
244                                 NameValueCollection explicitResourceKeys = null;
245                                 if (localize)
246                                         CollectLocalizationInfo (xmlNode, ref title, ref description, ref attributes,
247                                                                  ref explicitResourceKeys);
248                                 else
249                                         foreach (XmlNode att in xmlNode.Attributes)
250                                                 PutInCollection (att.Name, att.Value, ref attributes);
251
252                                 string key = Guid.NewGuid ().ToString ();
253                                 SiteMapNode node = new SiteMapNode (this, key, url, title, description,
254                                                                     ArrayList.ReadOnly (rolesList),
255                                                                     attributes,
256                                                                     explicitResourceKeys,
257                                                                     implicitResourceKey);
258                                         
259                                 foreach (XmlNode child in xmlNode.ChildNodes) {
260                                         if (child.NodeType != XmlNodeType.Element)
261                                                 continue;
262                                         AddNode (BuildSiteMapRecursive (child, EnableLocalization), node);
263                                 }
264                                 
265                                 return node;
266                         }
267                 }
268
269                 protected override void Clear ()
270                 {
271                         base.Clear ();
272                         root = null;
273                 }
274
275                 protected virtual void Dispose (bool disposing)
276                 {
277                         if (disposing)
278                                 watcher.Dispose ();
279                 }
280
281                 public void Dispose ()
282                 {
283                         Dispose (true);
284                 }
285                 
286                 public override SiteMapNode FindSiteMapNode (string rawUrl)
287                 {
288                         return base.FindSiteMapNode (rawUrl); // why did they override this method!?
289                 }
290
291                 public override SiteMapNode FindSiteMapNodeFromKey (string key)
292                 {
293                         return base.FindSiteMapNodeFromKey (key); // why did they override this method!?
294                 }
295
296                 public override void Initialize (string name, NameValueCollection attributes)
297                 {
298
299                         base.Initialize (name, attributes);
300                         file = attributes ["siteMapFile"];
301
302                         if (file == null && file.Length == 0)
303                                 throw new ArgumentException ("you must provide a file");
304
305                         if (HttpContext.Current != null)
306                                 file = HttpContext.Current.Request.MapPath (file, HttpRuntime.AppDomainAppVirtualPath, false);
307                         else
308                                 throw new InvalidOperationException ("HttpContext is missing");
309
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;
317                         }
318                 }
319
320                 protected override void RemoveNode (SiteMapNode node)
321                 {
322                         base.RemoveNode (node);
323                 }
324
325                 [MonoTODO ("Not implemented")]
326                 protected virtual void RemoveProvider (string providerName)
327                 {
328                         throw new NotImplementedException ();
329                 }
330
331                 void OnFileChanged (object sender, FileSystemEventArgs args)
332                 {
333                         Clear ();
334                 }
335
336                 public override SiteMapNode RootNode {
337                         get {
338                                 BuildSiteMap ();
339                                 return root;
340                         }
341                 }
342                 
343                 protected internal override SiteMapNode GetRootNodeCore ()
344                 {
345                         return BuildSiteMap ();
346                 }
347         }
348
349 }
350 #endif
351