SiteMapDataSource.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.Web.UI;
  33. using System.Web.Util;
  34. using System.ComponentModel;
  35. using System.Collections.Generic;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [PersistChildrenAttribute (false)]
  39. [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. [ParseChildrenAttribute (true)]
  41. public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
  42. {
  43. static string[] emptyNames = new string[] { "DefaultView" };
  44. SiteMapProvider provider;
  45. public virtual ICollection GetViewNames ()
  46. {
  47. return emptyNames;
  48. }
  49. IList IListSource.GetList () {
  50. return GetList ();
  51. }
  52. bool IListSource.ContainsListCollection {
  53. get { return ContainsListCollection; }
  54. }
  55. public virtual IList GetList ()
  56. {
  57. return ListSourceHelper.GetList (this);
  58. }
  59. [BrowsableAttribute (false)]
  60. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  61. public virtual bool ContainsListCollection {
  62. get { return ListSourceHelper.ContainsListCollection (this); }
  63. }
  64. DataSourceView IDataSource.GetView (string viewName) {
  65. return GetView (viewName);
  66. }
  67. ICollection IDataSource.GetViewNames () {
  68. return GetViewNames ();
  69. }
  70. event EventHandler IDataSource.DataSourceChanged {
  71. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  72. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  73. }
  74. [BrowsableAttribute (false)]
  75. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  76. public SiteMapProvider Provider {
  77. get {
  78. if (provider == null) {
  79. if (this.SiteMapProvider.Length == 0) {
  80. provider = SiteMap.Provider;
  81. if (provider == null)
  82. throw new HttpException ("There is no default provider configured for the site.");
  83. } else {
  84. provider = SiteMap.Providers [this.SiteMapProvider];
  85. if (provider == null)
  86. throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
  87. }
  88. }
  89. return provider;
  90. }
  91. set {
  92. provider = value;
  93. OnDataSourceChanged (EventArgs.Empty);
  94. }
  95. }
  96. [DefaultValueAttribute ("")]
  97. public virtual string SiteMapProvider {
  98. get { return ViewState.GetString ("SiteMapProvider", ""); }
  99. set {
  100. ViewState ["SiteMapProvider"] = value;
  101. OnDataSourceChanged (EventArgs.Empty);
  102. }
  103. }
  104. [DefaultValueAttribute ("")]
  105. [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  106. [UrlPropertyAttribute]
  107. public virtual string StartingNodeUrl {
  108. get { return ViewState.GetString ("StartingNodeUrl", ""); }
  109. set {
  110. ViewState ["StartingNodeUrl"] = value;
  111. OnDataSourceChanged (EventArgs.Empty);
  112. }
  113. }
  114. [DefaultValue (0)]
  115. public virtual int StartingNodeOffset {
  116. get { return ViewState.GetInt ("StartingNodeOffset", 0); }
  117. set {
  118. ViewState ["StartingNodeOffset"] = value;
  119. OnDataSourceChanged (EventArgs.Empty);
  120. }
  121. }
  122. [DefaultValueAttribute (false)]
  123. public virtual bool StartFromCurrentNode {
  124. get { return ViewState.GetBool ("StartFromCurrentNode", false); }
  125. set {
  126. ViewState ["StartFromCurrentNode"] = value;
  127. OnDataSourceChanged (EventArgs.Empty);
  128. }
  129. }
  130. [DefaultValueAttribute (true)]
  131. public virtual bool ShowStartingNode {
  132. get { return ViewState.GetBool ("ShowStartingNode", true); }
  133. set {
  134. ViewState ["ShowStartingNode"] = value;
  135. OnDataSourceChanged (EventArgs.Empty);
  136. }
  137. }
  138. public virtual DataSourceView GetView (string viewName)
  139. {
  140. SiteMapNode node = GetStartNode (viewName);
  141. if (node == null)
  142. return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
  143. else if (ShowStartingNode)
  144. return new SiteMapDataSourceView (this, viewName, node);
  145. else
  146. return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
  147. }
  148. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  149. {
  150. SiteMapNode node = GetStartNode (viewPath);
  151. if (node == null)
  152. return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
  153. else if (ShowStartingNode || node == null)
  154. return new SiteMapHierarchicalDataSourceView (node);
  155. else
  156. return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
  157. }
  158. [MonoTODO ("handle StartNodeOffsets > 0")]
  159. SiteMapNode GetStartNode (string viewPath)
  160. {
  161. SiteMapNode starting_node;
  162. if (viewPath != null && viewPath.Length != 0) {
  163. string url = MapUrl (StartingNodeUrl);
  164. return Provider.FindSiteMapNode (url);
  165. } else if (StartFromCurrentNode) {
  166. if (StartingNodeUrl.Length != 0)
  167. throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
  168. starting_node = SiteMap.CurrentNode;
  169. } else if (StartingNodeUrl.Length != 0) {
  170. string url = MapUrl (StartingNodeUrl);
  171. SiteMapNode node = Provider.FindSiteMapNode (url);
  172. if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
  173. starting_node = node;
  174. } else
  175. starting_node = Provider.RootNode;
  176. if (starting_node == null)
  177. return Provider.RootNode;
  178. int i;
  179. if (StartingNodeOffset < 0) {
  180. for (i = StartingNodeOffset; i < 0; i ++) {
  181. if (starting_node.ParentNode == null)
  182. break;
  183. starting_node = starting_node.ParentNode;
  184. }
  185. } else if (StartingNodeOffset > 0) {
  186. List<SiteMapNode> pathCurrentToStartingNode = new List<SiteMapNode> ();
  187. SiteMapNode tmpNode = Provider.CurrentNode;
  188. while (tmpNode != null && tmpNode != starting_node) {
  189. pathCurrentToStartingNode.Insert (0, tmpNode);
  190. tmpNode = tmpNode.ParentNode;
  191. }
  192. if (tmpNode == starting_node &&
  193. StartingNodeOffset <= pathCurrentToStartingNode.Count) {
  194. // The requested node is in the same subtree as the starting_node
  195. // try to advance on this path.
  196. starting_node = pathCurrentToStartingNode [StartingNodeOffset - 1];
  197. }
  198. }
  199. return starting_node;
  200. }
  201. string MapUrl (string url)
  202. {
  203. if (String.IsNullOrEmpty (url))
  204. return String.Empty;
  205. if (UrlUtils.IsRelativeUrl (url))
  206. return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
  207. else
  208. return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
  209. }
  210. }
  211. }
  212. #endif