SiteMapDataSource.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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[] { "DefaultView" };
  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. DataSourceView IDataSource.GetView (string viewName) {
  58. return GetView (viewName);
  59. }
  60. ICollection IDataSource.GetViewNames () {
  61. return GetViewNames ();
  62. }
  63. event EventHandler IDataSource.DataSourceChanged {
  64. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  65. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  66. }
  67. [BrowsableAttribute (false)]
  68. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  69. public SiteMapProvider Provider {
  70. get {
  71. if (provider == null) {
  72. if (this.SiteMapProvider.Length == 0) {
  73. provider = SiteMap.Provider;
  74. if (provider == null)
  75. throw new HttpException ("There is no default provider configured for the site.");
  76. } else {
  77. provider = SiteMap.Providers [this.SiteMapProvider];
  78. if (provider == null)
  79. throw new HttpException ("SiteMap provider '" + this.SiteMapProvider + "' not found.");
  80. }
  81. }
  82. return provider;
  83. }
  84. set {
  85. provider = value;
  86. OnDataSourceChanged (EventArgs.Empty);
  87. }
  88. }
  89. [DefaultValueAttribute ("")]
  90. public virtual string SiteMapProvider {
  91. get { return ViewState.GetString ("SiteMapProvider", ""); }
  92. set {
  93. ViewState ["SiteMapProvider"] = value;
  94. OnDataSourceChanged (EventArgs.Empty);
  95. }
  96. }
  97. [DefaultValueAttribute ("")]
  98. [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  99. [UrlPropertyAttribute]
  100. public virtual string StartingNodeUrl {
  101. get { return ViewState.GetString ("StartingNodeUrl", ""); }
  102. set {
  103. ViewState ["StartingNodeUrl"] = value;
  104. OnDataSourceChanged (EventArgs.Empty);
  105. }
  106. }
  107. [DefaultValue (0)]
  108. public virtual int StartingNodeOffset {
  109. get { return ViewState.GetInt ("StartingNodeOffset", 0); }
  110. set { ViewState["StartingNodeOffset"] = value; }
  111. }
  112. [DefaultValueAttribute (false)]
  113. public virtual bool StartFromCurrentNode {
  114. get { return ViewState.GetBool ("StartFromCurrentNode", false); }
  115. set {
  116. ViewState ["StartFromCurrentNode"] = value;
  117. OnDataSourceChanged (EventArgs.Empty);
  118. }
  119. }
  120. [DefaultValueAttribute (true)]
  121. public virtual bool ShowStartingNode {
  122. get { return ViewState.GetBool ("ShowStartingNode", true); }
  123. set {
  124. ViewState ["ShowStartingNode"] = value;
  125. OnDataSourceChanged (EventArgs.Empty);
  126. }
  127. }
  128. public virtual DataSourceView GetView (string viewName)
  129. {
  130. SiteMapNode node = GetStartNode (viewName);
  131. if (node == null)
  132. return new SiteMapDataSourceView (this, viewName, SiteMapNodeCollection.EmptyList);
  133. else if (ShowStartingNode)
  134. return new SiteMapDataSourceView (this, viewName, node);
  135. else
  136. return new SiteMapDataSourceView (this, viewName, node.ChildNodes);
  137. }
  138. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  139. {
  140. SiteMapNode node = GetStartNode (viewPath);
  141. if (node == null)
  142. return new SiteMapHierarchicalDataSourceView (SiteMapNodeCollection.EmptyList);
  143. else if (ShowStartingNode || node == null)
  144. return new SiteMapHierarchicalDataSourceView (node);
  145. else
  146. return new SiteMapHierarchicalDataSourceView (node.ChildNodes);
  147. }
  148. [MonoTODO ("handle StartNodeOffsets > 0")]
  149. SiteMapNode GetStartNode (string viewPath)
  150. {
  151. SiteMapNode starting_node;
  152. if (viewPath != null && viewPath.Length != 0) {
  153. string url = MapUrl (StartingNodeUrl);
  154. return Provider.FindSiteMapNode (url);
  155. }
  156. else if (StartFromCurrentNode) {
  157. if (StartingNodeUrl.Length != 0)
  158. throw new InvalidOperationException ("StartingNodeUrl can't be set if StartFromCurrentNode is set to true.");
  159. starting_node = Provider.CurrentNode;
  160. }
  161. else if (StartingNodeUrl.Length != 0) {
  162. string url = MapUrl (StartingNodeUrl);
  163. SiteMapNode node = Provider.FindSiteMapNode (url);
  164. if (node == null) throw new ArgumentException ("Can't find a site map node for the url: " + StartingNodeUrl);
  165. starting_node = node;
  166. }
  167. else
  168. starting_node = Provider.RootNode;
  169. int i;
  170. if (StartingNodeOffset < 0) {
  171. for (i = StartingNodeOffset; i < 0; i ++) {
  172. if (starting_node.ParentNode == null)
  173. break;
  174. starting_node = starting_node.ParentNode;
  175. }
  176. }
  177. else if (StartingNodeOffset > 0) {
  178. }
  179. return starting_node;
  180. }
  181. string MapUrl (string url)
  182. {
  183. if (String.IsNullOrEmpty (url))
  184. return String.Empty;
  185. if (UrlUtils.IsRelativeUrl (url))
  186. return UrlUtils.Combine (HttpRuntime.AppDomainAppVirtualPath, url);
  187. else
  188. return UrlUtils.ResolveVirtualPathFromAppAbsolute (url);
  189. }
  190. }
  191. }
  192. #endif