2 // System.Web.UI.WebControls.XmlDataSource
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Chris Toshok (toshok@ximian.com)
9 // (C) 2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Specialized;
39 using System.ComponentModel;
41 using System.Web.Caching;
42 using System.Security.Permissions;
44 namespace System.Web.UI.WebControls {
47 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
50 [DesignerAttribute ("System.Web.UI.Design.WebControls.XmlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
51 [DefaultProperty ("DataFile")]
52 [DefaultEvent ("Transforming")]
53 [ParseChildren (true)]
54 [PersistChildren (false)]
55 [WebSysDescription ("Connect to an XML file.")]
56 // [WebSysDisplayName ("XML file")]
57 public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
59 private string _data = string.Empty;
60 private string _transform = string.Empty;
61 private string _xpath = string.Empty;
62 private string _dataFile = string.Empty;
63 private string _transformFile = string.Empty;
64 private string _cacheKeyDependency = string.Empty;
65 private bool _enableCaching = true;
66 private int _cacheDuration = 0;
67 private DataSourceCacheExpiry _cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
69 event EventHandler IDataSource.DataSourceChanged {
70 add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
71 remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
74 static object EventTransforming = new object ();
75 public event EventHandler Transforming {
76 add { Events.AddHandler (EventTransforming, value); }
77 remove { Events.RemoveHandler (EventTransforming, value); }
80 protected virtual void OnTransforming (EventArgs e)
82 EventHandler eh = Events [EventTransforming] as EventHandler;
87 XmlDocument xmlDocument;
88 public XmlDocument GetXmlDocument ()
90 if (xmlDocument == null && EnableCaching)
91 xmlDocument = GetXmlDocumentFromCache ();
93 if (xmlDocument == null) {
94 xmlDocument = LoadXmlDocument ();
101 [MonoTODO ("schema")]
102 XmlDocument LoadXmlDocument ()
104 XmlDocument document = LoadFileOrData (DataFile, Data);
106 if (TransformFile == "" && Transform == "")
109 XslTransform xslTransform = new XslTransform ();
110 XmlDocument xsl = LoadFileOrData (TransformFile, Transform);
111 xslTransform.Load (xsl);
113 OnTransforming (EventArgs.Empty);
115 XmlDocument transofrResult = new XmlDocument ();
116 transofrResult.Load (xslTransform.Transform (document, TransformArgumentList));
118 return transofrResult;
121 XmlDocument LoadFileOrData (string filename, string data)
123 XmlDocument document = new XmlDocument ();
125 document.Load (MapPathSecure (filename));
127 document.LoadXml (data);
132 XmlDocument GetXmlDocumentFromCache ()
134 if (DataCache != null)
135 return (XmlDocument) DataCache [GetDataKey ()];
142 return TemplateSourceDirectory + "_" + Page.ToString () + "_" + ID;
149 if (HttpContext.Current != null)
150 return HttpContext.Current.Cache;
161 if (DataCache == null)
164 if (DataCache [GetDataKey ()] != null)
165 DataCache.Remove (GetDataKey ());
167 DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
168 TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
170 if (CacheDuration > 0) {
171 if (CacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
172 absoluteExpiration = DateTime.Now.AddSeconds (CacheDuration);
174 slidindExpiraion = new TimeSpan (CacheDuration * 10000);
177 DataCache.Add (GetDataKey (), xmlDocument,
178 new CacheDependency (new string [] { }, new string [] { CacheKeyDependency }),
179 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
185 throw new InvalidOperationException ();
187 xmlDocument.Save (MapPathSecure (DataFile));
192 return Transform == "" && TransformFile == "" && DataFile != "";
196 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
198 XmlNode doc = this.GetXmlDocument ();
199 XmlNodeList ret = null;
201 if (viewPath != "") {
202 XmlNode n = doc.SelectSingleNode (viewPath);
205 } else if (XPath != "") {
206 ret = doc.SelectNodes (XPath);
208 ret = doc.ChildNodes;
211 return new XmlHierarchicalDataSourceView (ret);
214 IList IListSource.GetList ()
216 return ListSourceHelper.GetList (this);
219 bool IListSource.ContainsListCollection {
220 get { return ListSourceHelper.ContainsListCollection (this); }
223 DataSourceView IDataSource.GetView (string viewName)
226 viewName = "DefaultView";
228 return new XmlDataSourceView (this, viewName);
231 ICollection IDataSource.GetViewNames ()
233 return new string [] { "DefaultView" };
237 //[TypeConverter (typeof(DataSourceCacheDurationConverter))]
239 public virtual int CacheDuration {
241 return _cacheDuration;
244 _cacheDuration = value;
248 [DefaultValue (DataSourceCacheExpiry.Absolute)]
250 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
252 return _cacheExpirationPolicy;
255 _cacheExpirationPolicy = value;
261 public virtual string CacheKeyDependency {
263 return _cacheKeyDependency;
266 _cacheKeyDependency = value;
270 [DefaultValue (true)]
272 public virtual bool EnableCaching {
274 return _enableCaching;
277 _enableCaching = value;
282 [PersistenceMode (PersistenceMode.InnerProperty)]
283 [WebSysDescription ("Inline XML data.")]
284 [WebCategory ("Data")]
285 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
286 // [TypeConverter (typeof(MultilineStringConverter))]
287 public virtual string Data {
288 get { return _data; }
290 if (_data != value) {
293 OnDataSourceChanged(EventArgs.Empty);
298 [DefaultValueAttribute ("")]
299 [EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
300 public virtual string DataFile {
301 get { return _dataFile; }
303 if (_dataFile != value) {
306 OnDataSourceChanged(EventArgs.Empty);
311 XsltArgumentList transformArgumentList;
313 [BrowsableAttribute (false)]
314 public virtual XsltArgumentList TransformArgumentList {
315 get { return transformArgumentList; }
316 set { transformArgumentList = value; }
319 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
320 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
321 [DefaultValueAttribute ("")]
322 // [TypeConverterAttribute (typeof(System.ComponentModel.MultilineStringConverter))]
323 public virtual string Transform {
324 get { return _transform; }
326 if (_transform != value) {
329 OnDataSourceChanged(EventArgs.Empty);
334 [EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
335 [DefaultValueAttribute ("")]
336 public virtual string TransformFile {
337 get { return _transformFile; }
339 if (_transformFile != value) {
340 _transformFile = value;
342 OnDataSourceChanged(EventArgs.Empty);
347 [DefaultValueAttribute ("")]
348 public virtual string XPath {
349 get { return _xpath; }
351 if (_xpath != value) {
353 OnDataSourceChanged(EventArgs.Empty);