XmlDataSource.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Web.UI.WebControls.XmlDataSource
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Chris Toshok ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Xml;
  36. using System.Xml.Xsl;
  37. using System.ComponentModel;
  38. using System.IO;
  39. namespace System.Web.UI.WebControls {
  40. [DesignerAttribute ("System.Web.UI.Design.WebControls.XmlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [DefaultProperty ("DataFile")]
  42. [DefaultEvent ("Transforming")]
  43. [ParseChildren (true)]
  44. [PersistChildren (false)]
  45. [WebSysDescription ("Connect to an XML file.")]
  46. // [WebSysDisplayName ("XML file")]
  47. public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
  48. event EventHandler IDataSource.DataSourceChanged {
  49. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  50. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  51. }
  52. static object EventTransforming = new object ();
  53. public event EventHandler Transforming {
  54. add { Events.AddHandler (EventTransforming, value); }
  55. remove { Events.RemoveHandler (EventTransforming, value); }
  56. }
  57. protected virtual void OnTransforming (EventArgs e)
  58. {
  59. EventHandler eh = Events [EventTransforming] as EventHandler;
  60. if (eh != null)
  61. eh (this, e);
  62. }
  63. XmlDocument xmlDocument;
  64. public XmlDocument GetXmlDocument ()
  65. {
  66. if (xmlDocument == null) {
  67. xmlDocument = new XmlDocument ();
  68. LoadXmlDocument (xmlDocument);
  69. }
  70. return xmlDocument;
  71. }
  72. [MonoTODO ("XSLT, schema")]
  73. void LoadXmlDocument (XmlDocument document)
  74. {
  75. if (Transform == "" && TransformFile == "") {
  76. if (DataFile != "")
  77. document.Load (MapPathSecure (DataFile));
  78. else
  79. document.LoadXml (Data);
  80. } else {
  81. throw new NotImplementedException ("XSLT transform not implemented");
  82. }
  83. }
  84. public void Save ()
  85. {
  86. if (!CanBeSaved)
  87. throw new InvalidOperationException ();
  88. xmlDocument.Save (MapPathSecure (DataFile));
  89. }
  90. bool CanBeSaved {
  91. get {
  92. return Transform == "" && TransformFile == "" && DataFile != "";
  93. }
  94. }
  95. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  96. {
  97. XmlNode doc = this.GetXmlDocument ();
  98. XmlNodeList ret = null;
  99. if (viewPath != "") {
  100. XmlNode n = doc.SelectSingleNode (viewPath);
  101. if (n != null)
  102. ret = n.ChildNodes;
  103. } else if (XPath != "") {
  104. ret = doc.SelectNodes (XPath);
  105. } else {
  106. ret = doc.ChildNodes;
  107. }
  108. return new XmlHierarchicalDataSourceView (ret);
  109. }
  110. IList IListSource.GetList ()
  111. {
  112. return ListSourceHelper.GetList (this);
  113. }
  114. bool IListSource.ContainsListCollection {
  115. get { return ListSourceHelper.ContainsListCollection (this); }
  116. }
  117. DataSourceView IDataSource.GetView (string viewName)
  118. {
  119. if (viewName == "")
  120. viewName = "DefaultView";
  121. Console.WriteLine ("XPath = {0}", XPath);
  122. return new XmlDataSourceView (this, viewName, GetXmlDocument ().DocumentElement.SelectNodes (XPath != "" ? XPath : "./*"));
  123. }
  124. ICollection IDataSource.GetViewNames ()
  125. {
  126. return new string [] { "DefaultView" };
  127. }
  128. [DefaultValue (0)]
  129. //[TypeConverter (typeof(DataSourceCacheDurationConverter))]
  130. [MonoTODO]
  131. public virtual int CacheDuration {
  132. get {
  133. throw new NotImplementedException ();
  134. }
  135. set {
  136. throw new NotImplementedException ();
  137. }
  138. }
  139. [DefaultValue (DataSourceCacheExpiry.Absolute)]
  140. [MonoTODO]
  141. public virtual DataSourceCacheExpiry CacheExpirationPolicy {
  142. get {
  143. throw new NotImplementedException ();
  144. }
  145. set {
  146. throw new NotImplementedException ();
  147. }
  148. }
  149. [DefaultValue ("")]
  150. [MonoTODO]
  151. public virtual string CacheKeyDependency {
  152. get {
  153. throw new NotImplementedException ();
  154. }
  155. set {
  156. throw new NotImplementedException ();
  157. }
  158. }
  159. [DefaultValue (true)]
  160. [MonoTODO]
  161. public virtual bool EnableCaching {
  162. get {
  163. throw new NotImplementedException ();
  164. }
  165. set {
  166. throw new NotImplementedException ();
  167. }
  168. }
  169. [DefaultValue ("")]
  170. [PersistenceMode (PersistenceMode.InnerProperty)]
  171. [WebSysDescription ("Inline XML data.")]
  172. [WebCategory ("Data")]
  173. [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  174. // [TypeConverter (typeof(MultilineStringConverter))]
  175. public virtual string Data {
  176. get {
  177. string ret = ViewState ["Data"] as string;
  178. return ret != null ? ret : "";
  179. }
  180. set {
  181. if (Data != value) {
  182. ViewState ["Data"] = value;
  183. xmlDocument = null;
  184. OnDataSourceChanged(EventArgs.Empty);
  185. }
  186. }
  187. }
  188. [DefaultValueAttribute ("")]
  189. [EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  190. public virtual string DataFile {
  191. get {
  192. string ret = ViewState ["DataFile"] as string;
  193. return ret != null ? ret : "";
  194. }
  195. set {
  196. if (DataFile != value) {
  197. ViewState ["DataFile"] = value;
  198. xmlDocument = null;
  199. OnDataSourceChanged(EventArgs.Empty);
  200. }
  201. }
  202. }
  203. XsltArgumentList transformArgumentList;
  204. [BrowsableAttribute (false)]
  205. public virtual XsltArgumentList TransformArgumentList {
  206. get { return transformArgumentList; }
  207. set { transformArgumentList = value; }
  208. }
  209. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  210. [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  211. [DefaultValueAttribute ("")]
  212. // [TypeConverterAttribute (typeof(System.ComponentModel.MultilineStringConverter))]
  213. public virtual string Transform {
  214. get {
  215. string ret = ViewState ["Transform"] as string;
  216. return ret != null ? ret : "";
  217. }
  218. set {
  219. if (Transform != value) {
  220. ViewState ["Transform"] = value;
  221. xmlDocument = null;
  222. OnDataSourceChanged(EventArgs.Empty);
  223. }
  224. }
  225. }
  226. [EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  227. [DefaultValueAttribute ("")]
  228. public virtual string TransformFile {
  229. get {
  230. string ret = ViewState ["TransformFile"] as string;
  231. return ret != null ? ret : "";
  232. }
  233. set {
  234. if (TransformFile != value) {
  235. ViewState ["TransformFile"] = value;
  236. xmlDocument = null;
  237. OnDataSourceChanged(EventArgs.Empty);
  238. }
  239. }
  240. }
  241. [DefaultValueAttribute ("")]
  242. public virtual string XPath {
  243. get {
  244. string ret = ViewState ["XPath"] as string;
  245. return ret != null ? ret : "";
  246. }
  247. set {
  248. if (XPath != value) {
  249. ViewState ["XPath"] = value;
  250. OnDataSourceChanged(EventArgs.Empty);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. #endif