XmlDataSource.cs 7.5 KB

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