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;
38 using System.Collections.Generic;
40 namespace System.Web.UI.WebControls
42 [PersistChildrenAttribute (false)]
43 [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
44 [ParseChildrenAttribute (true)]
45 public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
47 static string[] emptyNames = new string[] { "DefaultView" };
49 SiteMapProvider provider;
51 public virtual ICollection GetViewNames ()
56 IList IListSource.GetList () {
60 bool IListSource.ContainsListCollection {
61 get { return ContainsListCollection; }
64 public virtual IList GetList ()
66 return ListSourceHelper.GetList (this);
69 [BrowsableAttribute (false)]
70 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
71 public virtual bool ContainsListCollection {
72 get { return ListSourceHelper.ContainsListCollection (this); }
75 DataSourceView IDataSource.GetView (string viewName) {
76 return GetView (viewName);
79 ICollection IDataSource.GetViewNames () {
80 return GetViewNames ();
83 event EventHandler IDataSource.DataSourceChanged {
84 add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
85 remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
88 [BrowsableAttribute (false)]
89 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
90 public SiteMapProvider Provider {
92 if (provider == null) {
93 if (this.SiteMapProvider.Length == 0) {
94 provider = SiteMap.Provider;
96 throw new HttpException ("There is no default provider configured for the site.");
98 provider = SiteMap.Providers [this.SiteMapProvider];
100 throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
107 OnDataSourceChanged (EventArgs.Empty);
111 [DefaultValueAttribute ("")]
112 public virtual string SiteMapProvider {
113 get { return ViewState.GetString ("SiteMapProvider", ""); }
115 ViewState ["SiteMapProvider"] = value;
116 OnDataSourceChanged (EventArgs.Empty);
120 [DefaultValueAttribute ("")]
121 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
122 [UrlPropertyAttribute]
123 public virtual string StartingNodeUrl {
124 get { return ViewState.GetString ("StartingNodeUrl", ""); }
126 ViewState ["StartingNodeUrl"] = value;
127 OnDataSourceChanged (EventArgs.Empty);
132 public virtual int StartingNodeOffset {
133 get { return ViewState.GetInt ("StartingNodeOffset", 0); }
135 ViewState ["StartingNodeOffset"] = value;
136 OnDataSourceChanged (EventArgs.Empty);
140 [DefaultValueAttribute (false)]
141 public virtual bool StartFromCurrentNode {
142 get { return ViewState.GetBool ("StartFromCurrentNode", false); }
144 ViewState ["StartFromCurrentNode"] = value;
145 OnDataSourceChanged (EventArgs.Empty);
149 [DefaultValueAttribute (true)]
150 public virtual bool ShowStartingNode {
151 get { return ViewState.GetBool ("ShowStartingNode", true); }
153 ViewState ["ShowStartingNode"] = value;
154 OnDataSourceChanged (EventArgs.Empty);
158 public virtual DataSourceView GetView (string viewName)
160 SiteMapNode node = GetStartNode (viewName);
162 return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
163 else if (ShowStartingNode)
164 return new SiteMapDataSourceView (this, viewName, node);
166 return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
169 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
171 SiteMapNode node = GetStartNode (viewPath);
173 return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
174 else if (ShowStartingNode || node == null)
175 return new SiteMapHierarchicalDataSourceView (node);
177 return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
180 [MonoTODO ("handle StartNodeOffsets > 0")]
181 SiteMapNode GetStartNode (string viewPath)
183 SiteMapNode starting_node;
185 if (viewPath != null && viewPath.Length != 0) {
186 string url = MapUrl (StartingNodeUrl);
187 return Provider.FindSiteMapNode (url);
188 } else if (StartFromCurrentNode) {
189 if (StartingNodeUrl.Length != 0)
190 throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
191 starting_node = SiteMap.CurrentNode;
192 } else if (StartingNodeUrl.Length != 0) {
193 string url = MapUrl (StartingNodeUrl);
194 SiteMapNode node = Provider.FindSiteMapNode (url);
195 if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
197 starting_node = node;
199 starting_node = Provider.RootNode;
201 if (starting_node == null)
202 return Provider.RootNode;
205 if (StartingNodeOffset < 0) {
206 for (i = StartingNodeOffset; i < 0; i ++) {
207 if (starting_node.ParentNode == null)
209 starting_node = starting_node.ParentNode;
211 } else if (StartingNodeOffset > 0) {
212 List<SiteMapNode> pathCurrentToStartingNode = new List<SiteMapNode> ();
213 SiteMapNode tmpNode = Provider.CurrentNode;
214 while (tmpNode != null && tmpNode != starting_node) {
215 pathCurrentToStartingNode.Insert (0, tmpNode);
216 tmpNode = tmpNode.ParentNode;
218 if (tmpNode == starting_node &&
219 StartingNodeOffset <= pathCurrentToStartingNode.Count) {
220 // The requested node is in the same subtree as the starting_node
221 // try to advance on this path.
222 starting_node = pathCurrentToStartingNode [StartingNodeOffset - 1];
226 return starting_node;
229 string MapUrl (string url)
231 if (String.IsNullOrEmpty (url))
233 if (UrlUtils.IsRelativeUrl (url))
234 return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
236 return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);