refactoring: Button, ImageButton and LinkButton are used insted internal DataControlB...
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / XmlDataSource.cs
1 //
2 // System.Web.UI.WebControls.XmlDataSource
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Chris Toshok (toshok@ximian.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.Xml;
38 using System.Xml.Xsl;
39 using System.ComponentModel;
40 using System.IO;
41 using System.Web.Caching;
42 using System.Security.Permissions;
43
44 namespace System.Web.UI.WebControls {
45
46         // CAS
47         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
49         // attributes
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 {
58
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                 
69                 event EventHandler IDataSource.DataSourceChanged {
70                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
71                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
72                 }
73                 
74                 static object EventTransforming = new object ();
75                 public event EventHandler Transforming {
76                         add { Events.AddHandler (EventTransforming, value); }
77                         remove { Events.RemoveHandler (EventTransforming, value); }
78                 }
79                 
80                 protected virtual void OnTransforming (EventArgs e)
81                 {
82                         EventHandler eh = Events [EventTransforming] as EventHandler;
83                         if (eh != null)
84                                 eh (this, e);
85                 }
86                 
87                 XmlDocument xmlDocument;
88                 public XmlDocument GetXmlDocument ()
89                 {
90                         if (xmlDocument == null && EnableCaching)
91                                 xmlDocument = GetXmlDocumentFromCache ();
92
93                         if (xmlDocument == null) {
94                                 xmlDocument = LoadXmlDocument ();
95                                 UpdateCache ();
96                         }
97
98                         return xmlDocument;
99                 }
100
101                 [MonoTODO ("schema")]
102                 XmlDocument LoadXmlDocument ()
103                 {
104                         XmlDocument document = LoadFileOrData (DataFile, Data);
105
106                         if (TransformFile == "" && Transform == "")
107                                 return document;
108
109                         XslTransform xslTransform = new XslTransform ();
110                         XmlDocument xsl = LoadFileOrData (TransformFile, Transform);
111                         xslTransform.Load (xsl);
112
113                         OnTransforming (EventArgs.Empty);
114
115                         XmlDocument transofrResult = new XmlDocument ();
116                         transofrResult.Load (xslTransform.Transform (document, TransformArgumentList));
117
118                         return transofrResult;
119                 }
120
121                 XmlDocument LoadFileOrData (string filename, string data)
122                 {
123                         XmlDocument document = new XmlDocument ();
124                         if (filename != "")
125                                 document.Load (MapPathSecure (filename));
126                         else
127                                 document.LoadXml (data);
128
129                         return document;
130                 }
131
132                 XmlDocument GetXmlDocumentFromCache ()
133                 {
134                         if (DataCache != null)
135                                 return (XmlDocument) DataCache [GetDataKey ()];
136
137                         return null;
138                 }
139
140                 string GetDataKey ()
141                 {
142                         return TemplateSourceDirectory + "_" + Page.ToString () + "_" + ID;
143                 }
144
145                 Cache DataCache
146                 {
147                         get
148                         {
149                                 if (HttpContext.Current != null)
150                                         return HttpContext.Current.Cache;
151
152                                 return null;
153                         }
154                 }
155
156                 void UpdateCache ()
157                 {
158                         if (!EnableCaching)
159                                 return;
160
161                         if (DataCache == null)
162                                 return;
163
164                         if (DataCache [GetDataKey ()] != null)
165                                 DataCache.Remove (GetDataKey ());
166
167                         DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
168                         TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;
169
170                         if (CacheDuration > 0) {
171                                 if (CacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
172                                         absoluteExpiration = DateTime.Now.AddSeconds (CacheDuration);
173                                 else
174                                         slidindExpiraion = new TimeSpan (CacheDuration * 10000);
175                         }
176
177                         DataCache.Add (GetDataKey (), xmlDocument,
178                                 new CacheDependency (new string [] { }, new string [] { CacheKeyDependency }),
179                                 absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
180                 }
181
182                 public void Save ()
183                 {
184                         if (!CanBeSaved)
185                                 throw new InvalidOperationException ();
186                         
187                         xmlDocument.Save (MapPathSecure (DataFile));
188                 }
189                 
190                 bool CanBeSaved {
191                         get {
192                                 return Transform == "" && TransformFile == "" && DataFile != "";
193                         }
194                 }
195                 
196                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
197                 {
198                         XmlNode doc = this.GetXmlDocument ();
199                         XmlNodeList ret = null;
200                         
201                         if (viewPath != "") {
202                                 XmlNode n = doc.SelectSingleNode (viewPath);
203                                 if (n != null)
204                                         ret = n.ChildNodes;
205                         } else if (XPath != "") {
206                                 ret = doc.SelectNodes (XPath);
207                         } else {
208                                 ret = doc.ChildNodes;
209                         }
210                         
211                         return new XmlHierarchicalDataSourceView (ret);
212                 }
213                 
214                 IList IListSource.GetList ()
215                 {
216                         return ListSourceHelper.GetList (this);
217                 }
218                 
219                 bool IListSource.ContainsListCollection {
220                         get { return ListSourceHelper.ContainsListCollection (this); }
221                 }
222                 
223                 DataSourceView IDataSource.GetView (string viewName)
224                 {
225                         if (viewName == "")
226                                 viewName = "DefaultView";
227                         
228                         return new XmlDataSourceView (this, viewName);
229                 }
230                 
231                 ICollection IDataSource.GetViewNames ()
232                 {
233                         return new string [] { "DefaultView" };
234                 }
235                 
236                 [DefaultValue (0)]
237                 //[TypeConverter (typeof(DataSourceCacheDurationConverter))]
238                 [MonoTODO]
239                 public virtual int CacheDuration {
240                         get {
241                                 return _cacheDuration;
242                         }
243                         set {
244                                 _cacheDuration = value;
245                         }
246                 }
247
248                 [DefaultValue (DataSourceCacheExpiry.Absolute)]
249                 [MonoTODO]
250                 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
251                         get {
252                                 return _cacheExpirationPolicy;
253                         }
254                         set {
255                                 _cacheExpirationPolicy = value;
256                         }
257                 }
258
259                 [DefaultValue ("")]
260                 [MonoTODO]
261                 public virtual string CacheKeyDependency {
262                         get {
263                                 return _cacheKeyDependency;
264                         }
265                         set {
266                                 _cacheKeyDependency = value;
267                         }
268                 }
269
270                 [DefaultValue (true)]
271                 [MonoTODO]
272                 public virtual bool EnableCaching {
273                         get {
274                                 return _enableCaching;
275                         }
276                         set {
277                                 _enableCaching = value;
278                         }
279                 }
280
281                 [DefaultValue ("")]
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; }
289                         set {
290                                 if (_data != value) {
291                                         _data = value;
292                                         xmlDocument = null;
293                                         OnDataSourceChanged(EventArgs.Empty);
294                                 }
295                         }
296                 }
297                 
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; }
302                         set {
303                                 if (_dataFile != value) {
304                                         _dataFile = value;
305                                         xmlDocument = null;
306                                         OnDataSourceChanged(EventArgs.Empty);
307                                 }
308                         }
309                 }
310                 
311                 XsltArgumentList transformArgumentList;
312                 
313                 [BrowsableAttribute (false)]
314                 public virtual XsltArgumentList TransformArgumentList {
315                         get { return transformArgumentList; }
316                         set { transformArgumentList = value; }
317                 }
318                 
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; }
325                         set {
326                                 if (_transform != value) {
327                                         _transform = value;
328                                         xmlDocument = null;
329                                         OnDataSourceChanged(EventArgs.Empty);
330                                 }
331                         }
332                 }
333                 
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; }
338                         set {
339                                 if (_transformFile != value) {
340                                         _transformFile = value;
341                                         xmlDocument = null;
342                                         OnDataSourceChanged(EventArgs.Empty);
343                                 }
344                         }
345                 }
346                 
347                 [DefaultValueAttribute ("")]
348                 public virtual string XPath {
349                         get { return _xpath; }
350                         set {
351                                 if (_xpath != value) {
352                                         _xpath = value;
353                                         OnDataSourceChanged(EventArgs.Empty);
354                                 }
355                         }
356                 }
357         }
358 }
359 #endif
360