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 virtual IList GetList ()
57 return ListSourceHelper.GetList (this);
60 [BrowsableAttribute (false)]
61 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
62 public virtual 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)]
72 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
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 ("")]
95 public virtual string SiteMapProvider {
96 get { return ViewState.GetString ("SiteMapProvider", ""); }
98 ViewState ["SiteMapProvider"] = value;
99 OnDataSourceChanged (EventArgs.Empty);
103 [DefaultValueAttribute ("")]
104 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
105 [UrlPropertyAttribute]
106 public virtual string StartingNodeUrl {
107 get { return ViewState.GetString ("StartingNodeUrl", ""); }
109 ViewState ["StartingNodeUrl"] = value;
110 OnDataSourceChanged (EventArgs.Empty);
115 public virtual int StartingNodeOffset {
116 get { return ViewState.GetInt ("StartingNodeOffset", 0); }
117 set { ViewState["StartingNodeOffset"] = value; }
120 [DefaultValueAttribute (false)]
121 public virtual bool StartFromCurrentNode {
122 get { return ViewState.GetBool ("StartFromCurrentNode", false); }
124 ViewState ["StartFromCurrentNode"] = value;
125 OnDataSourceChanged (EventArgs.Empty);
129 [DefaultValueAttribute (true)]
130 public virtual bool ShowStartingNode {
131 get { return ViewState.GetBool ("ShowStartingNode", true); }
133 ViewState ["ShowStartingNode"] = value;
134 OnDataSourceChanged (EventArgs.Empty);
138 public virtual DataSourceView GetView (string viewName)
140 SiteMapNode node = GetStartNode (viewName);
142 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
143 else if (ShowStartingNode)
144 return new SiteMapDataSourceView (this, viewName, node);
146 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
149 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
151 SiteMapNode node = GetStartNode (viewPath);
153 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
154 else if (ShowStartingNode || node == null)
155 return new SiteMapHierarchicalDataSourceView (node);
157 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
160 [MonoTODO ("handle StartNodeOffsets > 0")]
161 SiteMapNode GetStartNode (string viewPath)
163 SiteMapNode starting_node;
165 if (viewPath != null && viewPath.Length != 0) {
166 string url = MapUrl (StartingNodeUrl);
167 return Provider.FindSiteMapNode (url);
169 else if (StartFromCurrentNode) {
170 if (StartingNodeUrl.Length != 0)
171 throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
172 starting_node = Provider.CurrentNode;
174 else if (StartingNodeUrl.Length != 0) {
175 string url = MapUrl (StartingNodeUrl);
176 SiteMapNode node = Provider.FindSiteMapNode (url);
177 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
179 starting_node = node;
182 starting_node = Provider.RootNode;
185 if (StartingNodeOffset < 0) {
186 for (i = StartingNodeOffset; i < 0; i ++) {
187 if (starting_node.ParentNode == null)
189 starting_node = starting_node.ParentNode;
192 else if (StartingNodeOffset > 0) {
195 return starting_node;
198 string MapUrl (string url)
200 if (UrlUtils.IsRelativeUrl (url))
201 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
203 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);