SiteMapDataSource.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // System.Web.UI.WebControls.SiteMapDataSource.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.Web.UI;
  34. using System.Web.Util;
  35. using System.ComponentModel;
  36. using System.Collections.Generic;
  37. namespace System.Web.UI.WebControls
  38. {
  39. [PersistChildrenAttribute (false)]
  40. [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [ParseChildrenAttribute (true)]
  42. [ToolboxBitmap ("")]
  43. public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
  44. {
  45. static string[] emptyNames = new string[] { "DefaultView" };
  46. SiteMapProvider provider;
  47. public virtual ICollection GetViewNames ()
  48. {
  49. return emptyNames;
  50. }
  51. IList IListSource.GetList () {
  52. return GetList ();
  53. }
  54. bool IListSource.ContainsListCollection {
  55. get { return ContainsListCollection; }
  56. }
  57. public virtual IList GetList ()
  58. {
  59. return ListSourceHelper.GetList (this);
  60. }
  61. [BrowsableAttribute (false)]
  62. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  63. public virtual bool ContainsListCollection {
  64. get { return ListSourceHelper.ContainsListCollection (this); }
  65. }
  66. DataSourceView IDataSource.GetView (string viewName) {
  67. return GetView (viewName);
  68. }
  69. ICollection IDataSource.GetViewNames () {
  70. return GetViewNames ();
  71. }
  72. event EventHandler IDataSource.DataSourceChanged {
  73. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  74. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  75. }
  76. [BrowsableAttribute (false)]
  77. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  78. public SiteMapProvider Provider {
  79. get {
  80. if (provider == null) {
  81. if (this.SiteMapProvider.Length == 0) {
  82. provider = SiteMap.Provider;
  83. if (provider == null)
  84. throw new HttpException ("There is no default provider configured for the site.");
  85. } else {
  86. provider = SiteMap.Providers [this.SiteMapProvider];
  87. if (provider == null)
  88. throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
  89. }
  90. }
  91. return provider;
  92. }
  93. set {
  94. provider = value;
  95. OnDataSourceChanged (EventArgs.Empty);
  96. }
  97. }
  98. [DefaultValueAttribute ("")]
  99. public virtual string SiteMapProvider {
  100. get { return ViewState.GetString ("SiteMapProvider", ""); }
  101. set {
  102. ViewState ["SiteMapProvider"] = value;
  103. OnDataSourceChanged (EventArgs.Empty);
  104. }
  105. }
  106. [DefaultValueAttribute ("")]
  107. [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  108. [UrlPropertyAttribute]
  109. public virtual string StartingNodeUrl {
  110. get { return ViewState.GetString ("StartingNodeUrl", ""); }
  111. set {
  112. ViewState ["StartingNodeUrl"] = value;
  113. OnDataSourceChanged (EventArgs.Empty);
  114. }
  115. }
  116. [DefaultValue (0)]
  117. public virtual int StartingNodeOffset {
  118. get { return ViewState.GetInt ("StartingNodeOffset", 0); }
  119. set {
  120. ViewState ["StartingNodeOffset"] = value;
  121. OnDataSourceChanged (EventArgs.Empty);
  122. }
  123. }
  124. [DefaultValueAttribute (false)]
  125. public virtual bool StartFromCurrentNode {
  126. get { return ViewState.GetBool ("StartFromCurrentNode", false); }
  127. set {
  128. ViewState ["StartFromCurrentNode"] = value;
  129. OnDataSourceChanged (EventArgs.Empty);
  130. }
  131. }
  132. [DefaultValueAttribute (true)]
  133. public virtual bool ShowStartingNode {
  134. get { return ViewState.GetBool ("ShowStartingNode", true); }
  135. set {
  136. ViewState ["ShowStartingNode"] = value;
  137. OnDataSourceChanged (EventArgs.Empty);
  138. }
  139. }
  140. public virtual DataSourceView GetView (string viewName)
  141. {
  142. SiteMapNode node = GetStartNode (viewName);
  143. if (node == null)
  144. return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
  145. else if (ShowStartingNode)
  146. return new SiteMapDataSourceView (this, viewName, node);
  147. else
  148. return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
  149. }
  150. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  151. {
  152. SiteMapNode node = GetStartNode (viewPath);
  153. if (node == null)
  154. return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
  155. else if (ShowStartingNode || node == null)
  156. return new SiteMapHierarchicalDataSourceView (node);
  157. else
  158. return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
  159. }
  160. [MonoTODO ("handle StartNodeOffsets > 0")]
  161. SiteMapNode GetStartNode (string viewPath)
  162. {
  163. SiteMapNode starting_node;
  164. if (viewPath != null && viewPath.Length != 0) {
  165. string url = MapUrl (StartingNodeUrl);
  166. return Provider.FindSiteMapNode (url);
  167. } else if (StartFromCurrentNode) {
  168. if (StartingNodeUrl.Length != 0)
  169. throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
  170. starting_node = SiteMap.CurrentNode;
  171. } else if (StartingNodeUrl.Length != 0) {
  172. string url = MapUrl (StartingNodeUrl);
  173. SiteMapNode node = Provider.FindSiteMapNode (url);
  174. if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
  175. starting_node = node;
  176. } else
  177. starting_node = Provider.RootNode;
  178. if (starting_node == null)
  179. return null;
  180. int i;
  181. if (StartingNodeOffset < 0) {
  182. for (i = StartingNodeOffset; i < 0; i ++) {
  183. if (starting_node.ParentNode == null)
  184. break;
  185. starting_node = starting_node.ParentNode;
  186. }
  187. } else if (StartingNodeOffset > 0) {
  188. List<SiteMapNode> pathCurrentToStartingNode = new List<SiteMapNode> ();
  189. SiteMapNode tmpNode = Provider.CurrentNode;
  190. while (tmpNode != null && tmpNode != starting_node) {
  191. pathCurrentToStartingNode.Insert (0, tmpNode);
  192. tmpNode = tmpNode.ParentNode;
  193. }
  194. if (tmpNode == starting_node &&
  195. StartingNodeOffset <= pathCurrentToStartingNode.Count) {
  196. // The requested node is in the same subtree as the starting_node
  197. // try to advance on this path.
  198. starting_node = pathCurrentToStartingNode [StartingNodeOffset - 1];
  199. }
  200. }
  201. return starting_node;
  202. }
  203. string MapUrl (string url)
  204. {
  205. if (String.IsNullOrEmpty (url))
  206. return String.Empty;
  207. if (UrlUtils.IsRelativeUrl (url))
  208. return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
  209. else
  210. return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
  211. }
  212. }
  213. }
  214. #endif