**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web / XmlSiteMapProvider.cs
blob69632fef25464d8a8d127f48d021ad3a95f456d7
1 //
2 // System.Web.XmlSiteMapProvider
3 //
4 // Authors:
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
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.
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Configuration;
35 using System.Text;
36 using System.Xml;
37 using System.Web.Util;
38 using System.IO;
40 namespace System.Web {
41 public class XmlSiteMapProvider : SiteMapProvider, IDisposable {
42 static readonly char [] seperators = { ';', ',' };
43 bool building;
45 public override SiteMapNode BuildSiteMap ()
47 if (root != null)
48 return root;
49 // Whenever you call AddNode, it tries to find dups, and will call this method
50 // Is this a bug in MS??
51 if (building)
52 return null;
54 lock (this) {
55 building = true;
56 if (root != null)
57 return root;
58 XmlDocument d = new XmlDocument ();
59 d.Load (file);
61 root = BuildSiteMapRecursive (d.SelectSingleNode ("/siteMap/siteMapNode"));
62 AddNode (root);
63 building = false;
64 return root;
68 string GetNonEmptyOptionalAttribute (XmlNode n, string name)
70 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true);
73 string GetOptionalAttribute (XmlNode n, string name)
75 return System.Web.Configuration.HandlersUtil.ExtractAttributeValue (name, n, true, true);
78 [MonoTODO]
79 SiteMapNode BuildSiteMapRecursive (XmlNode xmlNode)
82 if (xmlNode.Name != "siteMapNode")
83 throw new ConfigurationException ("incorrect element name", xmlNode);
85 string provider = GetNonEmptyOptionalAttribute (xmlNode, "provider");
86 string siteMapFile = GetNonEmptyOptionalAttribute (xmlNode, "siteMapFile");
88 if (provider != null) {
89 throw new NotImplementedException ();
90 } else if (siteMapFile != null) {
91 throw new NotImplementedException ();
92 } else {
94 string url = GetOptionalAttribute (xmlNode, "url");
95 string title = GetOptionalAttribute (xmlNode, "title");
96 string description = GetOptionalAttribute (xmlNode, "description");
97 string keywords = GetOptionalAttribute (xmlNode, "keywords");
98 string roles = GetOptionalAttribute (xmlNode, "roles");
100 ArrayList keywordsList = new ArrayList ();
101 if (keywords != null) {
102 foreach (string s in keywords.Split (seperators)) {
103 string ss = s.Trim ();
104 if (ss.Length > 0)
105 keywordsList.Add (ss);
109 ArrayList rolesList = new ArrayList ();
110 if (roles != null) {
111 foreach (string s in roles.Split (seperators)) {
112 string ss = s.Trim ();
113 if (ss.Length > 0)
114 rolesList.Add (ss);
118 SiteMapNode node = new SiteMapNode (this, url, title, description,
119 ArrayList.ReadOnly (keywordsList), ArrayList.ReadOnly (rolesList),
120 null); // TODO what do they want for attributes
122 foreach (XmlNode child in xmlNode.ChildNodes) {
123 if (child.NodeType != XmlNodeType.Element)
124 continue;
125 AddNode (BuildSiteMapRecursive (child), node);
128 return node;
132 protected override void Clear ()
134 base.Clear ();
135 root = null;
138 [MonoTODO]
139 public void Dispose ()
141 // what do i do?
144 [MonoTODO]
145 public override SiteMapNode FindSiteMapNode (string rawUrl)
147 return base.FindSiteMapNode (rawUrl); // why did they override this method!?
150 public override void Initialize (string name, NameValueCollection attributes)
153 base.Initialize (name, attributes);
154 file = attributes ["siteMapFile"];
156 if (file == null && file.Length == 0)
157 throw new ArgumentException ("you must provide a file");
159 if (UrlUtils.IsRelativeUrl (file))
160 file = Path.Combine(HttpRuntime.AppDomainAppPath, file);
161 else
162 file = UrlUtils.ResolvePhysicalPathFromAppAbsolute (file);
165 public override SiteMapNode RootNode {
166 get {
167 BuildSiteMap ();
168 return root;
172 string file;
173 SiteMapNode root = null;
177 #endif