XmlDataSource.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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.Drawing;
  35. using System.Text;
  36. using System.Xml;
  37. using System.Xml.Xsl;
  38. using System.ComponentModel;
  39. using System.IO;
  40. using System.Web.Caching;
  41. using System.Security.Permissions;
  42. namespace System.Web.UI.WebControls {
  43. // CAS
  44. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. // attributes
  47. [DesignerAttribute ("System.Web.UI.Design.WebControls.XmlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  48. [DefaultProperty ("DataFile")]
  49. [DefaultEvent ("Transforming")]
  50. [ParseChildren (true)]
  51. [PersistChildren (false)]
  52. [WebSysDescription ("Connect to an XML file.")]
  53. // [WebSysDisplayName ("XML file")]
  54. [ToolboxBitmap ("")]
  55. public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
  56. string _data = string.Empty;
  57. string _transform = string.Empty;
  58. string _xpath = string.Empty;
  59. string _dataFile = string.Empty;
  60. string _transformFile = string.Empty;
  61. string _cacheKeyDependency = string.Empty;
  62. bool _enableCaching = true;
  63. int _cacheDuration = 0;
  64. bool _documentNeedsUpdate;
  65. DataSourceCacheExpiry _cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
  66. static readonly string [] emptyNames = new string [] { "DefaultView" };
  67. event EventHandler IDataSource.DataSourceChanged {
  68. add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
  69. remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
  70. }
  71. static object EventTransforming = new object ();
  72. public event EventHandler Transforming {
  73. add { Events.AddHandler (EventTransforming, value); }
  74. remove { Events.RemoveHandler (EventTransforming, value); }
  75. }
  76. protected virtual void OnTransforming (EventArgs e)
  77. {
  78. EventHandler eh = Events [EventTransforming] as EventHandler;
  79. if (eh != null)
  80. eh (this, e);
  81. }
  82. XmlDocument xmlDocument;
  83. public XmlDocument GetXmlDocument ()
  84. {
  85. if (_documentNeedsUpdate)
  86. UpdateXml ();
  87. if (xmlDocument == null && EnableCaching)
  88. xmlDocument = GetXmlDocumentFromCache ();
  89. if (xmlDocument == null) {
  90. xmlDocument = LoadXmlDocument ();
  91. UpdateCache ();
  92. }
  93. return xmlDocument;
  94. }
  95. [MonoTODO ("schema")]
  96. XmlDocument LoadXmlDocument ()
  97. {
  98. XmlDocument document = LoadFileOrData (DataFile, Data);
  99. if (String.IsNullOrEmpty (TransformFile) && String.IsNullOrEmpty (Transform))
  100. return document;
  101. XslTransform xslTransform = new XslTransform ();
  102. XmlDocument xsl = LoadFileOrData (TransformFile, Transform);
  103. xslTransform.Load (xsl);
  104. OnTransforming (EventArgs.Empty);
  105. XmlDocument transofrResult = new XmlDocument ();
  106. transofrResult.Load (xslTransform.Transform (document, TransformArgumentList));
  107. return transofrResult;
  108. }
  109. XmlDocument LoadFileOrData (string filename, string data)
  110. {
  111. XmlDocument document = new XmlDocument ();
  112. if (!String.IsNullOrEmpty (filename)) {
  113. Uri uri;
  114. if (Uri.TryCreate (filename, UriKind.Absolute, out uri))
  115. document.Load (filename);
  116. else
  117. document.Load (MapPathSecure (filename));
  118. } else
  119. if (!String.IsNullOrEmpty (data))
  120. document.LoadXml (data);
  121. return document;
  122. }
  123. XmlDocument GetXmlDocumentFromCache ()
  124. {
  125. if (DataCache != null)
  126. return (XmlDocument) DataCache [GetDataKey ()];
  127. return null;
  128. }
  129. string GetDataKey ()
  130. {
  131. Page page = Page;
  132. string p = page != null ? page.ToString () : "NullPage";
  133. return TemplateSourceDirectory + "_" + p + "_" + ID;
  134. }
  135. Cache DataCache
  136. {
  137. get
  138. {
  139. if (HttpContext.Current != null)
  140. return HttpContext.Current.InternalCache;
  141. return null;
  142. }
  143. }
  144. void UpdateCache ()
  145. {
  146. if (!EnableCaching)
  147. return;
  148. if (DataCache == null)
  149. return;
  150. if (DataCache [GetDataKey ()] != null)
  151. DataCache.Remove (GetDataKey ());
  152. DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
  153. TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
  154. if (CacheDuration > 0) {
  155. if (CacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
  156. absoluteExpiration = DateTime.Now.AddSeconds (CacheDuration);
  157. else
  158. slidindExpiraion = new TimeSpan (CacheDuration * 10000L);
  159. }
  160. CacheDependency dependency = null;
  161. if (CacheKeyDependency.Length > 0)
  162. dependency = new CacheDependency (new string [] { }, new string [] { CacheKeyDependency });
  163. else
  164. dependency = new CacheDependency (new string [] { }, new string [] { });
  165. DataCache.Add (GetDataKey (), xmlDocument, dependency,
  166. absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
  167. }
  168. // If datafile changed, then DO NOT USE the cached data, but update it.
  169. void UpdateXml()
  170. {
  171. xmlDocument = LoadXmlDocument ();
  172. UpdateCache ();
  173. _documentNeedsUpdate = false;
  174. }
  175. public void Save ()
  176. {
  177. if (!CanBeSaved)
  178. throw new InvalidOperationException ();
  179. if (xmlDocument != null)
  180. xmlDocument.Save (MapPathSecure (DataFile));
  181. }
  182. bool CanBeSaved {
  183. get {
  184. return Transform == String.Empty && TransformFile == String.Empty && DataFile != String.Empty;
  185. }
  186. }
  187. protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
  188. {
  189. XmlNode doc = this.GetXmlDocument ();
  190. XmlNodeList ret = null;
  191. if (!String.IsNullOrEmpty (viewPath)) {
  192. XmlNode n = doc.SelectSingleNode (viewPath);
  193. if (n != null)
  194. ret = n.ChildNodes;
  195. } else if (!String.IsNullOrEmpty (XPath)) {
  196. ret = doc.SelectNodes (XPath);
  197. } else {
  198. ret = doc.ChildNodes;
  199. }
  200. return new XmlHierarchicalDataSourceView (ret);
  201. }
  202. IList IListSource.GetList ()
  203. {
  204. return ListSourceHelper.GetList (this);
  205. }
  206. bool IListSource.ContainsListCollection {
  207. get { return ListSourceHelper.ContainsListCollection (this); }
  208. }
  209. DataSourceView IDataSource.GetView (string viewName)
  210. {
  211. if (String.IsNullOrEmpty (viewName))
  212. viewName = "DefaultView";
  213. return new XmlDataSourceView (this, viewName);
  214. }
  215. ICollection IDataSource.GetViewNames ()
  216. {
  217. return emptyNames;
  218. }
  219. [DefaultValue (0)]
  220. [TypeConverter (typeof(DataSourceCacheDurationConverter))]
  221. public virtual int CacheDuration {
  222. get {
  223. return _cacheDuration;
  224. }
  225. set {
  226. _cacheDuration = value;
  227. }
  228. }
  229. [DefaultValue (DataSourceCacheExpiry.Absolute)]
  230. public virtual DataSourceCacheExpiry CacheExpirationPolicy {
  231. get {
  232. return _cacheExpirationPolicy;
  233. }
  234. set {
  235. _cacheExpirationPolicy = value;
  236. }
  237. }
  238. [DefaultValue ("")]
  239. public virtual string CacheKeyDependency {
  240. get {
  241. return _cacheKeyDependency;
  242. }
  243. set {
  244. _cacheKeyDependency = value;
  245. }
  246. }
  247. [DefaultValue (true)]
  248. public virtual bool EnableCaching {
  249. get {
  250. return _enableCaching;
  251. }
  252. set {
  253. _enableCaching = value;
  254. }
  255. }
  256. [DefaultValue ("")]
  257. [PersistenceMode (PersistenceMode.InnerProperty)]
  258. [WebSysDescription ("Inline XML data.")]
  259. [WebCategory ("Data")]
  260. [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  261. // [TypeConverter (typeof(MultilineStringConverter))]
  262. public virtual string Data {
  263. get { return _data; }
  264. set {
  265. if (_data != value) {
  266. _data = value;
  267. _documentNeedsUpdate = true;
  268. OnDataSourceChanged(EventArgs.Empty);
  269. }
  270. }
  271. }
  272. [DefaultValueAttribute ("")]
  273. [EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  274. [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
  275. public virtual string DataFile {
  276. get { return _dataFile; }
  277. set {
  278. if (_dataFile != value) {
  279. _dataFile = value;
  280. _documentNeedsUpdate = true;
  281. OnDataSourceChanged(EventArgs.Empty);
  282. }
  283. }
  284. }
  285. XsltArgumentList transformArgumentList;
  286. [BrowsableAttribute (false)]
  287. public virtual XsltArgumentList TransformArgumentList {
  288. get { return transformArgumentList; }
  289. set { transformArgumentList = value; }
  290. }
  291. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  292. [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  293. [DefaultValueAttribute ("")]
  294. // [TypeConverterAttribute (typeof(System.ComponentModel.MultilineStringConverter))]
  295. public virtual string Transform {
  296. get { return _transform; }
  297. set {
  298. if (_transform != value) {
  299. _transform = value;
  300. _documentNeedsUpdate = true;
  301. OnDataSourceChanged(EventArgs.Empty);
  302. }
  303. }
  304. }
  305. [EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  306. [DefaultValueAttribute ("")]
  307. [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
  308. public virtual string TransformFile {
  309. get { return _transformFile; }
  310. set {
  311. if (_transformFile != value) {
  312. _transformFile = value;
  313. _documentNeedsUpdate = true;
  314. OnDataSourceChanged(EventArgs.Empty);
  315. }
  316. }
  317. }
  318. [DefaultValueAttribute ("")]
  319. public virtual string XPath {
  320. get { return _xpath; }
  321. set {
  322. if (_xpath != value) {
  323. _xpath = value;
  324. OnDataSourceChanged(EventArgs.Empty);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. #endif