summaryrefslogtreecommitdiff
path: root/mcs/class/System.Web/System.Web.UI.WebControls/XmlDataSource.cs
blob: 26a0c00f315a0609a36dec1320354bb73a5a1c4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// System.Web.UI.WebControls.XmlDataSource
//
// Authors:
//	Ben Maurer (bmaurer@users.sourceforge.net)
//      Chris Toshok (toshok@ximian.com)
//
// (C) 2003 Ben Maurer
// (C) 2005 Novell, Inc (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if NET_2_0
using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.ComponentModel;
using System.IO;
using System.Web.Caching;
using System.Security.Permissions;

namespace System.Web.UI.WebControls {

	// CAS
	[AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	[AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	// attributes
	[DesignerAttribute ("System.Web.UI.Design.WebControls.XmlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
	[DefaultProperty ("DataFile")]
	[DefaultEvent ("Transforming")]
	[ParseChildren (true)]
	[PersistChildren (false)]
	[WebSysDescription ("Connect to an XML file.")]
//	[WebSysDisplayName ("XML file")]
	[ToolboxBitmap ("")]
	public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {

		string _data = string.Empty;
		string _transform = string.Empty;
		string _xpath = string.Empty;
		string _dataFile = string.Empty;
		string _transformFile = string.Empty;
		string _cacheKeyDependency = string.Empty;
		bool _enableCaching = true;
		int _cacheDuration = 0;
		bool _documentNeedsUpdate;
		
		DataSourceCacheExpiry _cacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
		static readonly string [] emptyNames = new string [] { "DefaultView" };
		
		event EventHandler IDataSource.DataSourceChanged {
			add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
			remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
		}
		
		static object EventTransforming = new object ();
		public event EventHandler Transforming {
			add { Events.AddHandler (EventTransforming, value); }
			remove { Events.RemoveHandler (EventTransforming, value); }
		}

		protected virtual void OnTransforming (EventArgs e)
		{
			EventHandler eh = Events [EventTransforming] as EventHandler;
			if (eh != null)
				eh (this, e);
		}
		
		XmlDocument xmlDocument;
		public XmlDocument GetXmlDocument ()
		{
			if (_documentNeedsUpdate)
				UpdateXml ();
			
			if (xmlDocument == null && EnableCaching)
				xmlDocument = GetXmlDocumentFromCache ();

			if (xmlDocument == null) {
				xmlDocument = LoadXmlDocument ();
				UpdateCache ();
			}

			return xmlDocument;
		}

		[MonoTODO ("schema")]
		XmlDocument LoadXmlDocument ()
		{
			XmlDocument document = LoadFileOrData (DataFile, Data);
			if (String.IsNullOrEmpty (TransformFile) && String.IsNullOrEmpty (Transform))
				return document;

			XslTransform xslTransform = new XslTransform ();
			XmlDocument xsl = LoadFileOrData (TransformFile, Transform);
			xslTransform.Load (xsl);

			OnTransforming (EventArgs.Empty);

			XmlDocument transofrResult = new XmlDocument ();
			transofrResult.Load (xslTransform.Transform (document, TransformArgumentList));

			return transofrResult;
		}

		XmlDocument LoadFileOrData (string filename, string data)
		{
			XmlDocument document = new XmlDocument ();
			if (!String.IsNullOrEmpty (filename)) {
				Uri uri;
				if (Uri.TryCreate (filename, UriKind.Absolute, out uri))
					document.Load (filename);
				else
					document.Load (MapPathSecure (filename));
			} else
				if (!String.IsNullOrEmpty (data))
					document.LoadXml (data);
			return document;
		}

		XmlDocument GetXmlDocumentFromCache ()
		{
			if (DataCache != null)
				return (XmlDocument) DataCache [GetDataKey ()];

			return null;
		}

		string GetDataKey ()
		{
#if NET_4_0
			if (String.IsNullOrEmpty (DataFile) && !String.IsNullOrEmpty (Data)) {
				string key = CacheKeyContext;
				if (!String.IsNullOrEmpty (key))
					return key;
			}
#endif
			Page page = Page;
			string p = page != null ? page.ToString () : "NullPage";
			
			return TemplateSourceDirectory + "_" + p + "_" + ID;
		}

		Cache DataCache
		{
			get
			{
				if (HttpContext.Current != null)
					return HttpContext.Current.InternalCache;

				return null;
			}
		}

		void UpdateCache ()
		{
			if (!EnableCaching)
				return;

			if (DataCache == null)
				return;

			string dataKey = GetDataKey ();
			if (DataCache [dataKey] != null)
				DataCache.Remove (dataKey);

			DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
			TimeSpan slidindExpiraion = Cache.NoSlidingExpiration;

			if (CacheDuration > 0) {
				if (CacheExpirationPolicy == DataSourceCacheExpiry.Absolute)
					absoluteExpiration = DateTime.Now.AddSeconds (CacheDuration);
				else
					slidindExpiraion = new TimeSpan (CacheDuration * 10000L);
			}

			CacheDependency dependency = null;
			if (CacheKeyDependency.Length > 0)
				dependency = new CacheDependency (new string [] { }, new string [] { CacheKeyDependency });
			else
				dependency = new CacheDependency (new string [] { }, new string [] { });

			DataCache.Add (dataKey, xmlDocument, dependency,
				absoluteExpiration, slidindExpiraion, CacheItemPriority.Default, null);
		}
		
		// If datafile changed, then DO NOT USE the cached data, but update it.
		void UpdateXml()
		{
			xmlDocument = LoadXmlDocument (); 
			UpdateCache ();
			_documentNeedsUpdate = false;
		}

		public void Save ()
		{
			if (!CanBeSaved)
				throw new InvalidOperationException ();

			if (xmlDocument != null)
				xmlDocument.Save (MapPathSecure (DataFile));
		}
		
		bool CanBeSaved {
			get {
				return Transform == String.Empty && TransformFile == String.Empty && DataFile != String.Empty;
			}
		}
		
		protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
		{
			XmlNode doc = this.GetXmlDocument ();
			XmlNodeList ret = null;
			
			if (!String.IsNullOrEmpty (viewPath)) {
				XmlNode n = doc.SelectSingleNode (viewPath);
				if (n != null)
					ret = n.ChildNodes;
			} else if (!String.IsNullOrEmpty (XPath)) {
				ret = doc.SelectNodes (XPath);
			} else {
				ret = doc.ChildNodes;
			}
			
			return new XmlHierarchicalDataSourceView (ret);
		}
		
		IList IListSource.GetList ()
		{
			return ListSourceHelper.GetList (this);
		}
		
		bool IListSource.ContainsListCollection {
			get { return ListSourceHelper.ContainsListCollection (this); }
		}
		
		DataSourceView IDataSource.GetView (string viewName)
		{
			if (String.IsNullOrEmpty (viewName))
				viewName = "DefaultView";
			
			return new XmlDataSourceView (this, viewName);
		}
		
		ICollection IDataSource.GetViewNames ()
		{
			return emptyNames;
		}
		
		[DefaultValue (0)]
		[TypeConverter (typeof(DataSourceCacheDurationConverter))]
		public virtual int CacheDuration {
			get {
				return _cacheDuration;
			}
			set {
				_cacheDuration = value;
			}
		}

		[DefaultValue (DataSourceCacheExpiry.Absolute)]
		public virtual DataSourceCacheExpiry CacheExpirationPolicy {
			get {
				return _cacheExpirationPolicy;
			}
			set {
				_cacheExpirationPolicy = value;
			}
		}

		[DefaultValue ("")]
		public virtual string CacheKeyDependency {
			get {
				return _cacheKeyDependency;
			}
			set {
				_cacheKeyDependency = value;
			}
		}

		[DefaultValue (true)]
		public virtual bool EnableCaching {
			get {
				return _enableCaching;
			}
			set {
				_enableCaching = value;
			}
		}

		[DefaultValue ("")]
		[PersistenceMode (PersistenceMode.InnerProperty)]
		[WebSysDescription ("Inline XML data.")]
		[WebCategory ("Data")]
		[EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
		[TypeConverter (typeof(MultilineStringConverter))]
		public virtual string Data {
			get { return _data; }
			set {
				if (_data != value) {
					_data = value;
					_documentNeedsUpdate = true;
					OnDataSourceChanged(EventArgs.Empty);
				}
			}
		}
		
		[DefaultValueAttribute ("")]
		[EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
		[MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
		public virtual string DataFile {
			get { return _dataFile; }
			set {
				if (_dataFile != value) {
					_dataFile = value;
					_documentNeedsUpdate = true;
					OnDataSourceChanged(EventArgs.Empty);
				}
			}
		}
		
		XsltArgumentList transformArgumentList;
		
		[BrowsableAttribute (false)]
		public virtual XsltArgumentList TransformArgumentList {
			get { return transformArgumentList; }
			set { transformArgumentList = value; }
		}
		
		[PersistenceModeAttribute (PersistenceMode.InnerProperty)]
		[EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
		[DefaultValueAttribute ("")]
		[TypeConverterAttribute (typeof(MultilineStringConverter))]
		public virtual string Transform {
			get { return _transform; }
			set {
				if (_transform != value) {
					_transform = value; 
					_documentNeedsUpdate = true;
					OnDataSourceChanged(EventArgs.Empty);
				}
			}
		}
		
		[EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
		[DefaultValueAttribute ("")]
		[MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
		public virtual string TransformFile {
			get { return _transformFile; }
			set {
				if (_transformFile != value) {
					_transformFile = value;
					_documentNeedsUpdate = true;
					OnDataSourceChanged(EventArgs.Empty);
				}
			}
		}
		
		[DefaultValueAttribute ("")]
		public virtual string XPath {
			get { return _xpath; }
			set {
				if (_xpath != value) {
					_xpath = value;
					OnDataSourceChanged(EventArgs.Empty);
				}
			}
		}
#if NET_4_0
		[DefaultValue ("")]
		public virtual string CacheKeyContext {
			get { return ViewState.GetString ("CacheKeyContext", String.Empty); }
			set { ViewState ["CacheKeyContext"] = value; }
		}
#endif
	}
}
#endif