XmlDataSource.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // System.Web.UI.WebControls.XmlDataSource
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  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.Collections;
  31. using System.Collections.Specialized;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Xml.Xsl;
  35. using System.ComponentModel;
  36. using System.IO;
  37. namespace System.Web.UI.WebControls {
  38. public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
  39. event EventHandler IDataSource.DataSourceChanged {
  40. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  41. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  42. }
  43. static object EventTransforming = new object ();
  44. public event EventHandler Transforming {
  45. add { Events.AddHandler (EventTransforming, value); }
  46. remove { Events.RemoveHandler (EventTransforming, value); }
  47. }
  48. protected virtual void OnTransforming (EventArgs e)
  49. {
  50. EventHandler eh = Events [EventTransforming] as EventHandler;
  51. if (eh != null)
  52. eh (this, e);
  53. }
  54. XmlDataDocument xmlDataDocument;
  55. public XmlDataDocument GetXmlDataDocument ()
  56. {
  57. if (xmlDataDocument == null) {
  58. xmlDataDocument = new XmlDataDocument ();
  59. LoadXmlDataDocument (xmlDataDocument);
  60. }
  61. return xmlDataDocument;
  62. }
  63. [MonoTODO ("XSLT, schema")]
  64. void LoadXmlDataDocument (XmlDataDocument document)
  65. {
  66. if (Transform == "" && TransformFile == "") {
  67. if (DataFile != "")
  68. document.Load (MapPathSecure (DataFile));
  69. else
  70. document.LoadXml (Data);
  71. } else {
  72. throw new NotImplementedException ("XSLT transform not implemented");
  73. }
  74. }
  75. public void Save ()
  76. {
  77. if (!CanBeSaved)
  78. throw new InvalidOperationException ();
  79. xmlDataDocument.Save (MapPathSecure (DataFile));
  80. }
  81. bool CanBeSaved {
  82. get {
  83. return !ReadOnly && Transform == "" && TransformFile == "" && DataFile != "";
  84. }
  85. }
  86. [MonoTODO]
  87. protected override void LoadViewState (object savedState)
  88. {
  89. base.LoadViewState (savedState);
  90. }
  91. [MonoTODO]
  92. protected override object SaveViewState ()
  93. {
  94. return base.SaveViewState ();
  95. }
  96. [MonoTODO]
  97. protected override void TrackViewState ()
  98. {
  99. base.TrackViewState ();
  100. }
  101. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  102. {
  103. XmlNode doc = this.GetXmlDataDocument ();
  104. XmlNodeList ret = null;
  105. if (viewPath != "") {
  106. XmlNode n = doc.SelectSingleNode (viewPath);
  107. if (n != null)
  108. ret = n.ChildNodes;
  109. } else if (XPath != "") {
  110. ret = doc.SelectNodes (XPath);
  111. } else {
  112. ret = doc.ChildNodes;
  113. }
  114. return new XmlHierarchicalDataSourceView (ret);
  115. }
  116. IList IListSource.GetList ()
  117. {
  118. return ListSourceHelper.GetList (this);
  119. }
  120. bool IListSource.ContainsListCollection {
  121. get { return ListSourceHelper.ContainsListCollection (this); }
  122. }
  123. DataSourceView IDataSource.GetView (string viewName)
  124. {
  125. if (viewName == "")
  126. viewName = "DefaultView";
  127. return new XmlDataSourceView (this, viewName, GetXmlDataDocument ().SelectNodes (XPath != "" ? XPath : "."));
  128. }
  129. ICollection IDataSource.GetViewNames ()
  130. {
  131. return new string [] { "DefaultView" };
  132. }
  133. public virtual bool AutoSave {
  134. get {
  135. object ret = ViewState ["AutoSave"];
  136. return ret != null ? (bool)ret : true;
  137. }
  138. set {
  139. ViewState ["AutoSave"] = value;
  140. }
  141. }
  142. // TODO: stub these apis
  143. //protected virtual FileDataSourceCache Cache { get; }
  144. //public virtual int CacheDuration { get; set; }
  145. //public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
  146. //public virtual string CacheKeyDependency { get; set; }
  147. //public virtual bool EnableCaching { get; set; }
  148. public virtual string Data {
  149. get {
  150. string ret = ViewState ["Data"] as string;
  151. return ret != null ? ret : "";
  152. }
  153. set {
  154. if (Data != value) {
  155. ViewState ["Data"] = value;
  156. xmlDataDocument = null;
  157. OnDataSourceChanged(EventArgs.Empty);
  158. }
  159. }
  160. }
  161. public virtual string DataFile {
  162. get {
  163. string ret = ViewState ["DataFile"] as string;
  164. return ret != null ? ret : "";
  165. }
  166. set {
  167. if (DataFile != value) {
  168. ViewState ["DataFile"] = value;
  169. xmlDataDocument = null;
  170. OnDataSourceChanged(EventArgs.Empty);
  171. }
  172. }
  173. }
  174. public virtual bool ReadOnly {
  175. get {
  176. object ret = ViewState ["ReadOnly"];
  177. return ret != null ? (bool)ret : true;
  178. }
  179. set {
  180. ViewState ["ReadOnly"] = value;
  181. }
  182. }
  183. public virtual string Schema {
  184. get {
  185. string ret = ViewState ["Schema"] as string;
  186. return ret != null ? ret : "";
  187. }
  188. set {
  189. if (Schema != value) {
  190. ViewState ["Schema"] = value;
  191. xmlDataDocument = null;
  192. OnDataSourceChanged(EventArgs.Empty);
  193. }
  194. }
  195. }
  196. public virtual string SchemaFile {
  197. get {
  198. string ret = ViewState ["SchemaFile"] as string;
  199. return ret != null ? ret : "";
  200. }
  201. set {
  202. if (SchemaFile != value) {
  203. ViewState ["SchemaFile"] = value;
  204. xmlDataDocument = null;
  205. OnDataSourceChanged(EventArgs.Empty);
  206. }
  207. }
  208. }
  209. XsltArgumentList transformArgumentList;
  210. public virtual XsltArgumentList TransformArgumentList {
  211. get { return transformArgumentList; }
  212. set { transformArgumentList = value; }
  213. }
  214. public virtual string Transform {
  215. get {
  216. string ret = ViewState ["Transform"] as string;
  217. return ret != null ? ret : "";
  218. }
  219. set {
  220. if (Transform != value) {
  221. ViewState ["Transform"] = value;
  222. xmlDataDocument = null;
  223. OnDataSourceChanged(EventArgs.Empty);
  224. }
  225. }
  226. }
  227. public virtual string TransformFile {
  228. get {
  229. string ret = ViewState ["TransformFile"] as string;
  230. return ret != null ? ret : "";
  231. }
  232. set {
  233. if (TransformFile != value) {
  234. ViewState ["TransformFile"] = value;
  235. xmlDataDocument = null;
  236. OnDataSourceChanged(EventArgs.Empty);
  237. }
  238. }
  239. }
  240. public virtual string XPath {
  241. get {
  242. string ret = ViewState ["XPath"] as string;
  243. return ret != null ? ret : "";
  244. }
  245. set {
  246. if (XPath != value) {
  247. ViewState ["XPath"] = value;
  248. OnDataSourceChanged(EventArgs.Empty);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. #endif