SiteMapDataSource.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 { ViewState["StartingNodeOffset"] = value; }
  118. }
  119. [DefaultValueAttribute (false)]
  120. public virtual bool StartFromCurrentNode {
  121. get { return ViewState.GetBool ("StartFromCurrentNode", false); }
  122. set {
  123. ViewState ["StartFromCurrentNode"] = value;
  124. OnDataSourceChanged (EventArgs.Empty);
  125. }
  126. }
  127. [DefaultValueAttribute (true)]
  128. public virtual bool ShowStartingNode {
  129. get { return ViewState.GetBool ("ShowStartingNode", true); }
  130. set {
  131. ViewState ["ShowStartingNode"] = value;
  132. OnDataSourceChanged (EventArgs.Empty);
  133. }
  134. }
  135. public virtual DataSourceView GetView (string viewName)
  136. {
  137. SiteMapNode node = GetStartNode (viewName);
  138. if (node == null)
  139. return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
  140. else if (ShowStartingNode)
  141. return new SiteMapDataSourceView (this, viewName, node);
  142. else
  143. return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
  144. }
  145. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  146. {
  147. SiteMapNode node = GetStartNode (viewPath);
  148. if (node == null)
  149. return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
  150. else if (ShowStartingNode || node == null)
  151. return new SiteMapHierarchicalDataSourceView (node);
  152. else
  153. return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
  154. }
  155. [MonoTODO ("handle StartNodeOffsets > 0")]
  156. SiteMapNode GetStartNode (string viewPath)
  157. {
  158. SiteMapNode starting_node;
  159. if (viewPath != null && viewPath.Length != 0) {
  160. string url = MapUrl (StartingNodeUrl);
  161. return Provider.FindSiteMapNode (url);
  162. } else if (StartFromCurrentNode) {
  163. if (StartingNodeUrl.Length != 0)
  164. throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
  165. starting_node = SiteMap.CurrentNode;
  166. } else if (StartingNodeUrl.Length != 0) {
  167. string url = MapUrl (StartingNodeUrl);
  168. SiteMapNode node = Provider.FindSiteMapNode (url);
  169. if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
  170. starting_node = node;
  171. } else
  172. starting_node = Provider.RootNode;
  173. if (starting_node == null)
  174. return Provider.RootNode;
  175. int i;
  176. if (StartingNodeOffset < 0) {
  177. for (i = StartingNodeOffset; i < 0; i ++) {
  178. if (starting_node.ParentNode == null)
  179. break;
  180. starting_node = starting_node.ParentNode;
  181. }
  182. } else if (StartingNodeOffset > 0) {
  183. List<SiteMapNode> pathCurrentToStartingNode = new List<SiteMapNode> ();
  184. SiteMapNode tmpNode = Provider.CurrentNode;
  185. while (tmpNode != null && tmpNode != starting_node) {
  186. pathCurrentToStartingNode.Insert (0, tmpNode);
  187. tmpNode = tmpNode.ParentNode;
  188. }
  189. if (tmpNode == starting_node &&
  190. StartingNodeOffset <= pathCurrentToStartingNode.Count) {
  191. // The requested node is in the same subtree as the starting_node
  192. // try to advance on this path.
  193. starting_node = pathCurrentToStartingNode [StartingNodeOffset - 1];
  194. }
  195. }
  196. return starting_node;
  197. }
  198. string MapUrl (string url)
  199. {
  200. if (String.IsNullOrEmpty (url))
  201. return String.Empty;
  202. if (UrlUtils.IsRelativeUrl (url))
  203. return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
  204. else
  205. return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
  206. }
  207. }
  208. }
  209. #endif