SiteMapDataSource.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. namespace System.Web.UI.WebControls
  36. {
  37. [PersistChildrenAttribute (false)]
  38. [DesignerAttribute ("System.Web.UI.Design.WebControls.SiteMapDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. [ParseChildrenAttribute (true)]
  40. public class SiteMapDataSource : HierarchicalDataSourceControl, IDataSource, IListSource
  41. {
  42. static string[] emptyNames = new string[] { string.Empty };
  43. SiteMapProvider provider;
  44. public virtual ICollection GetViewNames ()
  45. {
  46. return emptyNames;
  47. }
  48. public virtual IList GetList ()
  49. {
  50. return ListSourceHelper.GetList (this);
  51. }
  52. [BrowsableAttribute (false)]
  53. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  54. public virtual bool ContainsListCollection {
  55. get { return ListSourceHelper.ContainsListCollection (this); }
  56. }
  57. event EventHandler IDataSource.DataSourceChanged {
  58. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  59. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  60. }
  61. [BrowsableAttribute (false)]
  62. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  63. public SiteMapProvider Provider {
  64. get {
  65. if (provider == null) {
  66. if (this.SiteMapProvider.Length == 0) {
  67. provider = SiteMap.Provider;
  68. if (provider == null)
  69. throw new HttpException ("There is no default provider configured for the site.");
  70. } else {
  71. provider = SiteMap.Providers [this.SiteMapProvider];
  72. if (provider == null)
  73. throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
  74. }
  75. }
  76. return provider;
  77. }
  78. set {
  79. provider = value;
  80. OnDataSourceChanged (EventArgs.Empty);
  81. }
  82. }
  83. [DefaultValueAttribute ("")]
  84. public virtual string SiteMapProvider {
  85. get {
  86. object o = ViewState ["SiteMapProvider"];
  87. if (o != null) return (string) o;
  88. else return string.Empty;
  89. }
  90. set {
  91. ViewState ["SiteMapProvider"] = value;
  92. OnDataSourceChanged (EventArgs.Empty);
  93. }
  94. }
  95. [DefaultValueAttribute ("")]
  96. [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  97. [UrlPropertyAttribute]
  98. public virtual string StartingNodeUrl {
  99. get {
  100. object o = ViewState ["StartingNodeUrl"];
  101. if (o != null) return (string) o;
  102. else return string.Empty;
  103. }
  104. set {
  105. ViewState ["StartingNodeUrl"] = value;
  106. OnDataSourceChanged (EventArgs.Empty);
  107. }
  108. }
  109. [DefaultValueAttribute (false)]
  110. public virtual bool StartFromCurrentNode {
  111. get {
  112. object o = ViewState ["StartFromCurrentNode"];
  113. if (o != null) return (bool) o;
  114. else return false;
  115. }
  116. set {
  117. ViewState ["StartFromCurrentNode"] = value;
  118. OnDataSourceChanged (EventArgs.Empty);
  119. }
  120. }
  121. [DefaultValueAttribute (true)]
  122. public virtual bool ShowStartingNode {
  123. get {
  124. object o = ViewState ["ShowStartingNode"];
  125. if (o != null) return (bool) o;
  126. else return true;
  127. }
  128. set {
  129. ViewState ["ShowStartingNode"] = value;
  130. OnDataSourceChanged (EventArgs.Empty);
  131. }
  132. }
  133. public virtual DataSourceView GetView (string viewName)
  134. {
  135. SiteMapNode node = GetStartNode (viewName);
  136. if (node == null)
  137. return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
  138. else if (ShowStartingNode)
  139. return new SiteMapDataSourceView (this, viewName, node);
  140. else
  141. return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
  142. }
  143. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  144. {
  145. SiteMapNode node = GetStartNode (viewPath);
  146. if (node == null)
  147. return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
  148. else if (ShowStartingNode || node == null)
  149. return new SiteMapHierarchicalDataSourceView (node);
  150. else
  151. return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
  152. }
  153. SiteMapNode GetStartNode (string viewPath)
  154. {
  155. if (viewPath != null && viewPath.Length != 0) {
  156. string url = MapUrl (StartingNodeUrl);
  157. return Provider.FindSiteMapNode (url);
  158. }
  159. else if (StartFromCurrentNode) {
  160. if (StartingNodeUrl.Length != 0)
  161. throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
  162. return Provider.CurrentNode;
  163. }
  164. else if (StartingNodeUrl.Length != 0) {
  165. string url = MapUrl (StartingNodeUrl);
  166. SiteMapNode node = Provider.FindSiteMapNode (url);
  167. if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
  168. return node;
  169. }
  170. else
  171. return Provider.RootNode;
  172. }
  173. string MapUrl (string url)
  174. {
  175. if (UrlUtils.IsRelativeUrl (url))
  176. return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
  177. else
  178. return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
  179. }
  180. }
  181. }
  182. #endif