New test.
[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.Text;
38 using System.Xml;
39 using System.Web.Util;
40 using System.IO;
41
42 namespace System.Web
43 {
44         public class XmlSiteMapProvider : StaticSiteMapProvider, IDisposable
45         {
46                 static readonly char [] seperators = { ';', ',' };
47                 bool building;
48                 string file;
49                 SiteMapNode root = null;
50 #if !TARGET_JVM // Java platform does not support file notifications
51                 FileSystemWatcher watcher;
52 #endif
53
54                 protected internal override void AddNode (SiteMapNode node, SiteMapNode parentNode)
55                 {
56                         base.AddNode (node, parentNode);
57                 }
58
59                 protected virtual void AddProvider (string providerName, SiteMapNode parentNode)
60                 {
61                         throw new NotImplementedException ();
62                 }
63
64                 public override SiteMapNode BuildSiteMap ()
65                 {
66                         if (root != null)
67                                 return root;
68                         // Whenever you call AddNode, it tries to find dups, and will call this method
69                         // Is this a bug in MS??
70                         if (building)
71                                 return null;
72                         
73                         lock (this) {
74                                 try {
75                                         building = true;
76                                         if (root != null)
77                                                 return root;
78                                         XmlDocument d = new XmlDocument ();
79                                         d.Load (file);
80                                         
81                                         XmlNode nod = d.DocumentElement ["siteMapNode"];
82                                         if (nod == null)
83                                                 throw new HttpException ("Invalid site map file: " + Path.GetFileName (file));
84                                                 
85                                         root = BuildSiteMapRecursive (nod);
86                                                 
87                                         AddNode (root);
88                                 } finally {
89                                         building = false;
90                                 }
91                                 return root;
92                         }
93                 }
94                 
95                 string GetNonEmptyOptionalAttribute (XmlNode n, string name)
96                 {
97                         return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
98                 }
99                 
100                 string GetOptionalAttribute (XmlNode n, string name)
101                 {
102                         return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true, true);
103                 }
104                 
105                 SiteMapNode BuildSiteMapRecursive (XmlNode xmlNode)
106                 {
107                         if (xmlNode.Name != "siteMapNode")
108                                 throw new ConfigurationException ("incorrect element name", xmlNode);
109                         
110                         string provider = GetNonEmptyOptionalAttribute (xmlNode, "provider");
111                         string siteMapFile = GetNonEmptyOptionalAttribute (xmlNode, "siteMapFile");
112                         
113                         if (provider != null) {
114                                 throw new NotImplementedException ();
115                         } else if (siteMapFile != null) {
116                                 throw new NotImplementedException ();
117                         } else {
118
119                                 string url = GetOptionalAttribute (xmlNode, "url");
120                                 string title = GetOptionalAttribute (xmlNode, "title");
121                                 string description = GetOptionalAttribute (xmlNode, "description");
122                                 string keywords = GetOptionalAttribute (xmlNode, "keywords");
123                                 string roles = GetOptionalAttribute (xmlNode, "roles");
124                                 
125                                 ArrayList keywordsList = new ArrayList ();
126                                 if (keywords != null && keywords.Length > 0) {
127                                         foreach (string s in keywords.Split (seperators)) {
128                                                 string ss = s.Trim ();
129                                                 if (ss.Length > 0)
130                                                         keywordsList.Add (ss);
131                                         }
132                                 }
133                                 
134                                 ArrayList rolesList = new ArrayList ();
135                                 if (roles != null && roles.Length > 0) {
136                                         foreach (string s in roles.Split (seperators)) {
137                                                 string ss = s.Trim ();
138                                                 if (ss.Length > 0)
139                                                         rolesList.Add (ss);
140                                         }
141                                 }
142
143                                 if (!string.IsNullOrEmpty (url)) {
144                                         if (UrlUtils.IsRelativeUrl (url))
145                                                 url = UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
146                                 }
147                                 
148                                 SiteMapNode node = new SiteMapNode (this, url, url, title, description,
149                                         /*ArrayList.ReadOnly (keywordsList), */ArrayList.ReadOnly (rolesList), null,
150                                         null, null); // TODO what do they want for attributes
151                                         
152                                 foreach (XmlNode child in xmlNode.ChildNodes) {
153                                         if (child.NodeType != XmlNodeType.Element)
154                                                 continue;
155                                         AddNode (BuildSiteMapRecursive (child), node);
156                                 }
157                                 
158                                 return node;
159                         }
160                 }
161
162                 protected override void Clear ()
163                 {
164                         base.Clear ();
165                         root = null;
166                 }
167
168                 protected virtual void Dispose (bool disposing)
169                 {
170 #if !TARGET_JVM // Java platform does not support file notifications
171                         if (disposing)
172                                 watcher.Dispose ();
173 #endif
174                 }
175
176                 public void Dispose ()
177                 {
178                         Dispose (true);
179                 }
180                 
181                 public override SiteMapNode FindSiteMapNode (string rawUrl)
182                 {
183                         return base.FindSiteMapNode (rawUrl); // why did they override this method!?
184                 }
185
186                 public override SiteMapNode FindSiteMapNodeFromKey (string key)
187                 {
188                         return base.FindSiteMapNodeFromKey (key); // why did they override this method!?
189                 }
190
191                 public override void Initialize (string name, NameValueCollection attributes)
192                 {
193
194                         base.Initialize (name, attributes);
195                         file = attributes ["siteMapFile"];
196
197                         if (file == null && file.Length == 0)
198                                 throw new ArgumentException ("you must provide a file");
199                         
200                         if (UrlUtils.IsRelativeUrl (file))
201                                 file = Path.Combine(HttpRuntime.AppDomainAppPath, file);
202                         else
203                                 file = UrlUtils.ResolvePhysicalPathFromAppAbsolute (file);
204
205 #if !TARGET_JVM // Java platform does not support file notifications
206                         if (File.Exists (file)) {
207                                 watcher = new FileSystemWatcher ();
208                                 watcher.Path = Path.GetFullPath (Path.GetDirectoryName (file));
209                                 watcher.Filter = Path.GetFileName (file);
210                                 watcher.Changed += new FileSystemEventHandler (OnFileChanged);
211                                 watcher.EnableRaisingEvents = true;
212                         }
213 #endif
214                 }
215
216                 protected override void RemoveNode (SiteMapNode node)
217                 {
218                         base.RemoveNode (node);
219                 }
220
221                 [MonoTODO ("Not implemented")]
222                 protected virtual void RemoveProvider (string providerName)
223                 {
224                         throw new NotImplementedException ();
225                 }
226 #if !TARGET_JVM
227                 void OnFileChanged (object sender, FileSystemEventArgs args)
228                 {
229                         Clear ();
230                 }
231 #endif
232                 public override SiteMapNode RootNode {
233                         get {
234                                 BuildSiteMap ();
235                                 return root;
236                         }
237                 }
238                 
239                 protected internal override SiteMapNode GetRootNodeCore ()
240                 {
241                         return BuildSiteMap ();
242                 }
243         }
244
245 }
246 #endif
247