2 // System.Web.UI.WebControls.SiteMapDataSource.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
36 using System.Web.Util;
37 using System.ComponentModel;
39 namespace System.Web.UI.WebControls
41 [PersistChildrenAttribute (false)]
42 [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [ParseChildrenAttribute (true)]
44 public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
46 static string[] emptyNames = new string[] { string.Empty };
48 SiteMapProvider provider;
50 public virtual ICollection GetViewNames ()
55 public IList GetList ()
57 return ListSourceHelper.GetList (this);
60 [BrowsableAttribute (false)]
\r
61 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
62 public bool ContainsListCollection {
63 get { return ListSourceHelper.ContainsListCollection (this); }
66 event EventHandler IDataSource.DataSourceChanged {
67 add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
68 remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
71 [BrowsableAttribute (false)]
\r
72 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
\r
73 public SiteMapProvider Provider {
75 if (provider == null) {
76 if (this.SiteMapProvider.Length == 0) {
77 provider = SiteMap.Provider;
79 throw new HttpException ("There is no default provider configured for the site.");
81 provider = SiteMap.Providers [this.SiteMapProvider];
83 throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
90 OnDataSourceChanged (EventArgs.Empty);
94 [DefaultValueAttribute ("")]
\r
95 public string SiteMapProvider {
97 object o = ViewState ["SiteMapProvider"];
98 if (o != null) return (string) o;
99 else return string.Empty;
102 ViewState ["SiteMapProvider"] = value;
103 OnDataSourceChanged (EventArgs.Empty);
107 [DefaultValueAttribute ("")]
\r
108 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
\r
109 [UrlPropertyAttribute]
\r
110 public string StartingNodeUrl {
112 object o = ViewState ["StartingNodeUrl"];
113 if (o != null) return (string) o;
114 else return string.Empty;
117 ViewState ["StartingNodeUrl"] = value;
118 OnDataSourceChanged (EventArgs.Empty);
122 [DefaultValueAttribute (false)]
\r
123 public bool StartFromCurrentNode {
125 object o = ViewState ["StartFromCurrentNode"];
126 if (o != null) return (bool) o;
130 ViewState ["StartFromCurrentNode"] = value;
131 OnDataSourceChanged (EventArgs.Empty);
135 [DefaultValueAttribute (true)]
\r
136 public bool ShowStartingNode {
138 object o = ViewState ["ShowStartingNode"];
139 if (o != null) return (bool) o;
143 ViewState ["ShowStartingNode"] = value;
144 OnDataSourceChanged (EventArgs.Empty);
148 public DataSourceView GetView (string viewName)
150 SiteMapNode node = GetStartNode (viewName);
152 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
153 else if (ShowStartingNode)
154 return new SiteMapDataSourceView (this, viewName, node);
156 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
159 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
161 SiteMapNode node = GetStartNode (viewPath);
163 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
164 else if (ShowStartingNode || node == null)
165 return new SiteMapHierarchicalDataSourceView (node);
167 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
170 SiteMapNode GetStartNode (string viewPath)
172 if (viewPath != null && viewPath.Length != 0) {
173 string url = MapUrl (StartingNodeUrl);
174 return Provider.FindSiteMapNode (url);
176 else if (StartFromCurrentNode) {
177 if (StartingNodeUrl.Length != 0)
178 throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
179 return Provider.CurrentNode;
181 else if (StartingNodeUrl.Length != 0) {
182 string url = MapUrl (StartingNodeUrl);
183 SiteMapNode node = Provider.FindSiteMapNode (url);
184 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
188 return Provider.RootNode;
191 string MapUrl (string url)
193 if (UrlUtils.IsRelativeUrl (url))
194 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
196 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);