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;
68 static readonly string [] emptyNames = new string [] { "DefaultView" };
70 event EventHandler IDataSource.DataSourceChanged {
71 add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
72 remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
75 static object EventTransforming = new object ();
76 public event EventHandler Transforming {
77 add { Events.AddHandler (EventTransforming, value); }
78 remove { Events.RemoveHandler (EventTransforming, value); }
81 protected virtual void OnTransforming (EventArgs e)
83 EventHandler eh = Events [EventTransforming] as EventHandler;
88 XmlDocument xmlDocument;
89 public XmlDocument GetXmlDocument ()
91 if (xmlDocument == null && EnableCaching)
92 xmlDocument = GetXmlDocumentFromCache ();
94 if (xmlDocument == null) {
95 xmlDocument = LoadXmlDocument ();
102 [MonoTODO ("schema")]
103 XmlDocument LoadXmlDocument ()
105 XmlDocument document = LoadFileOrData (DataFile, Data);
107 if (TransformFile == "" && Transform == "")
110 XslTransform xslTransform = new XslTransform ();
111 XmlDocument xsl = LoadFileOrData (TransformFile, Transform);
112 xslTransform.Load (xsl);
114 OnTransforming (EventArgs.Empty);
116 XmlDocument transofrResult = new XmlDocument ();
117 transofrResult.Load (xslTransform.Transform (document, TransformArgumentList));
119 return transofrResult;
122 XmlDocument LoadFileOrData (string filename, string data)
124 XmlDocument document = new XmlDocument ();
126 document.Load (MapPathSecure (filename));
128 document.LoadXml (data);
133 XmlDocument GetXmlDocumentFromCache ()
135 if (DataCache != null)
136 return (XmlDocument) DataCache [GetDataKey ()];
143 return TemplateSourceDirectory + "_" + Page.ToString () + "_" + ID;
150 if (HttpContext.Current != null)
151 return HttpContext.Current.InternalCache;
162 if (DataCache == null)
165 if (DataCache [GetDataKey ()] != null)
166 DataCache.Remove (GetDataKey ());
168 DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
169 TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
171 if (CacheDuration > 0) {
172 if (CacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
173 absoluteExpiration = DateTime.Now.AddSeconds (CacheDuration);
175 slidindExpiraion = new TimeSpan (CacheDuration * 10000);
178 CacheDependency dependency = null;
179 if (CacheKeyDependency.Length > 0)
180 dependency = new CacheDependency (new string [] { }, new string [] { CacheKeyDependency });
182 dependency = new CacheDependency (new string [] { }, new string [] { });
184 DataCache.Add (GetDataKey (), xmlDocument, dependency,
185 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
191 throw new InvalidOperationException ();
193 xmlDocument.Save (MapPathSecure (DataFile));
198 return Transform == "" && TransformFile == "" && DataFile != "";
202 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
204 XmlNode doc = this.GetXmlDocument ();
205 XmlNodeList ret = null;
207 if (viewPath != "") {
208 XmlNode n = doc.SelectSingleNode (viewPath);
211 } else if (XPath != "") {
212 ret = doc.SelectNodes (XPath);
214 ret = doc.ChildNodes;
217 return new XmlHierarchicalDataSourceView (ret);
220 IList IListSource.GetList ()
222 return ListSourceHelper.GetList (this);
225 bool IListSource.ContainsListCollection {
226 get { return ListSourceHelper.ContainsListCollection (this); }
229 DataSourceView IDataSource.GetView (string viewName)
232 viewName = "DefaultView";
234 return new XmlDataSourceView (this, viewName);
237 ICollection IDataSource.GetViewNames ()
243 //[TypeConverter (typeof(DataSourceCacheDurationConverter))]
244 public virtual int CacheDuration {
246 return _cacheDuration;
249 _cacheDuration = value;
253 [DefaultValue (DataSourceCacheExpiry.Absolute)]
254 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
256 return _cacheExpirationPolicy;
259 _cacheExpirationPolicy = value;
264 public virtual string CacheKeyDependency {
266 return _cacheKeyDependency;
269 _cacheKeyDependency = value;
273 [DefaultValue (true)]
274 public virtual bool EnableCaching {
276 return _enableCaching;
279 _enableCaching = value;
284 [PersistenceMode (PersistenceMode.InnerProperty)]
285 [WebSysDescription ("Inline XML data.")]
286 [WebCategory ("Data")]
287 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
288 // [TypeConverter (typeof(MultilineStringConverter))]
289 public virtual string Data {
290 get { return _data; }
292 if (_data != value) {
295 OnDataSourceChanged(EventArgs.Empty);
300 [DefaultValueAttribute ("")]
301 [EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
302 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
303 public virtual string DataFile {
304 get { return _dataFile; }
306 if (_dataFile != value) {
309 OnDataSourceChanged(EventArgs.Empty);
314 XsltArgumentList transformArgumentList;
316 [BrowsableAttribute (false)]
317 public virtual XsltArgumentList TransformArgumentList {
318 get { return transformArgumentList; }
319 set { transformArgumentList = value; }
322 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
323 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
324 [DefaultValueAttribute ("")]
325 // [TypeConverterAttribute (typeof(System.ComponentModel.MultilineStringConverter))]
326 public virtual string Transform {
327 get { return _transform; }
329 if (_transform != value) {
332 OnDataSourceChanged(EventArgs.Empty);
337 [EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
338 [DefaultValueAttribute ("")]
339 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
340 public virtual string TransformFile {
341 get { return _transformFile; }
343 if (_transformFile != value) {
344 _transformFile = value;
346 OnDataSourceChanged(EventArgs.Empty);
351 [DefaultValueAttribute ("")]
352 public virtual string XPath {
353 get { return _xpath; }
355 if (_xpath != value) {
357 OnDataSourceChanged(EventArgs.Empty);