bug fixes found by Test/System.Web.UI.WebControls/BoundFieldTest
[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.Security.Permissions;
42
43 namespace System.Web.UI.WebControls {
44
45         // CAS
46         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
47         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48         // attributes
49         [DesignerAttribute ("System.Web.UI.Design.WebControls.XmlDataSourceDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
50         [DefaultProperty ("DataFile")]
51         [DefaultEvent ("Transforming")]
52         [ParseChildren (true)]
53         [PersistChildren (false)]
54         [WebSysDescription ("Connect to an XML file.")]
55 //      [WebSysDisplayName ("XML file")]
56         public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
57
58                 
59                 event EventHandler IDataSource.DataSourceChanged {
60                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
61                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
62                 }
63                 
64                 static object EventTransforming = new object ();
65                 public event EventHandler Transforming {
66                         add { Events.AddHandler (EventTransforming, value); }
67                         remove { Events.RemoveHandler (EventTransforming, value); }
68                 }
69                 
70                 protected virtual void OnTransforming (EventArgs e)
71                 {
72                         EventHandler eh = Events [EventTransforming] as EventHandler;
73                         if (eh != null)
74                                 eh (this, e);
75                 }
76                 
77                 XmlDocument xmlDocument;
78                 public XmlDocument GetXmlDocument ()
79                 {
80                         if (xmlDocument == null) {
81                                 xmlDocument = new XmlDocument ();
82                                 LoadXmlDocument (xmlDocument);
83                         }
84                         return xmlDocument;
85                 }
86                 
87                 [MonoTODO ("XSLT, schema")]
88                 void LoadXmlDocument (XmlDocument document)
89                 {
90                         if (Transform == "" && TransformFile == "") {
91                                 if (DataFile != "")
92                                         document.Load (MapPathSecure (DataFile));
93                                 else
94                                         document.LoadXml (Data);
95                         } else {
96                                 throw new NotImplementedException ("XSLT transform not implemented");
97                         }
98                 }
99
100                 public void Save ()
101                 {
102                         if (!CanBeSaved)
103                                 throw new InvalidOperationException ();
104                         
105                         xmlDocument.Save (MapPathSecure (DataFile));
106                 }
107                 
108                 bool CanBeSaved {
109                         get {
110                                 return Transform == "" && TransformFile == "" && DataFile != "";
111                         }
112                 }
113                 
114                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
115                 {
116                         XmlNode doc = this.GetXmlDocument ();
117                         XmlNodeList ret = null;
118                         
119                         if (viewPath != "") {
120                                 XmlNode n = doc.SelectSingleNode (viewPath);
121                                 if (n != null)
122                                         ret = n.ChildNodes;
123                         } else if (XPath != "") {
124                                 ret = doc.SelectNodes (XPath);
125                         } else {
126                                 ret = doc.ChildNodes;
127                         }
128                         
129                         return new XmlHierarchicalDataSourceView (ret);
130                 }
131                 
132                 IList IListSource.GetList ()
133                 {
134                         return ListSourceHelper.GetList (this);
135                 }
136                 
137                 bool IListSource.ContainsListCollection {
138                         get { return ListSourceHelper.ContainsListCollection (this); }
139                 }
140                 
141                 DataSourceView IDataSource.GetView (string viewName)
142                 {
143                         if (viewName == "")
144                                 viewName = "DefaultView";
145                         
146                         return new XmlDataSourceView (this, viewName);
147                 }
148                 
149                 ICollection IDataSource.GetViewNames ()
150                 {
151                         return new string [] { "DefaultView" };
152                 }
153                 
154                 [DefaultValue (0)]
155                 //[TypeConverter (typeof(DataSourceCacheDurationConverter))]
156                 [MonoTODO]
157                 public virtual int CacheDuration {
158                         get {
159                                 throw new NotImplementedException ();
160                         }
161                         set {
162                                 throw new NotImplementedException ();
163                         }
164                 }
165
166                 [DefaultValue (DataSourceCacheExpiry.Absolute)]
167                 [MonoTODO]
168                 public virtual DataSourceCacheExpiry CacheExpirationPolicy {
169                         get {
170                                 throw new NotImplementedException ();
171                         }
172                         set {
173                                 throw new NotImplementedException ();
174                         }
175                 }
176
177                 [DefaultValue ("")]
178                 [MonoTODO]
179                 public virtual string CacheKeyDependency {
180                         get {
181                                 throw new NotImplementedException ();
182                         }
183                         set {
184                                 throw new NotImplementedException ();
185                         }
186                 }
187
188                 [DefaultValue (true)]
189                 [MonoTODO]
190                 public virtual bool EnableCaching {
191                         get {
192                                 throw new NotImplementedException ();
193                         }
194                         set {
195                                 throw new NotImplementedException ();
196                         }
197                 }
198
199                 [DefaultValue ("")]
200                 [PersistenceMode (PersistenceMode.InnerProperty)]
201                 [WebSysDescription ("Inline XML data.")]
202                 [WebCategory ("Data")]
203                 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
204 //              [TypeConverter (typeof(MultilineStringConverter))]
205                 public virtual string Data {
206                         get {
207                                 string ret = ViewState ["Data"] as string;
208                                 return ret != null ? ret : "";
209                         }
210                         set {
211                                 if (Data != value) {
212                                         ViewState ["Data"] = value;
213                                         xmlDocument = null;
214                                         OnDataSourceChanged(EventArgs.Empty);
215                                 }
216                         }
217                 }
218                 
219                 [DefaultValueAttribute ("")]
220                 [EditorAttribute ("System.Web.UI.Design.XmlDataFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
221                 public virtual string DataFile {
222                         get {
223                                 string ret = ViewState ["DataFile"] as string;
224                                 return ret != null ? ret : "";
225                         }
226                         set {
227                                 if (DataFile != value) {
228                                         ViewState ["DataFile"] = value;
229                                         xmlDocument = null;
230                                         OnDataSourceChanged(EventArgs.Empty);
231                                 }
232                         }
233                 }
234                 
235                 XsltArgumentList transformArgumentList;
236                 
237                 [BrowsableAttribute (false)]
238                 public virtual XsltArgumentList TransformArgumentList {
239                         get { return transformArgumentList; }
240                         set { transformArgumentList = value; }
241                 }
242                 
243                 [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
244                 [EditorAttribute ("System.ComponentModel.Design.MultilineStringEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
245                 [DefaultValueAttribute ("")]
246 //              [TypeConverterAttribute (typeof(System.ComponentModel.MultilineStringConverter))]
247                 public virtual string Transform {
248                         get {
249                                 string ret = ViewState ["Transform"] as string;
250                                 return ret != null ? ret : "";
251                         }
252                         set {
253                                 if (Transform != value) {
254                                         ViewState ["Transform"] = value;
255                                         xmlDocument = null;
256                                         OnDataSourceChanged(EventArgs.Empty);
257                                 }
258                         }
259                 }
260                 
261                 [EditorAttribute ("System.Web.UI.Design.XslTransformFileEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
262                 [DefaultValueAttribute ("")]
263                 public virtual string TransformFile {
264                         get {
265                                 string ret = ViewState ["TransformFile"] as string;
266                                 return ret != null ? ret : "";
267                         }
268                         set {
269                                 if (TransformFile != value) {
270                                         ViewState ["TransformFile"] = value;
271                                         xmlDocument = null;
272                                         OnDataSourceChanged(EventArgs.Empty);
273                                 }
274                         }
275                 }
276                 
277                 [DefaultValueAttribute ("")]
278                 public virtual string XPath {
279                         get {
280                                 string ret = ViewState ["XPath"] as string;
281                                 return ret != null ? ret : "";
282                         }
283                         set {
284                                 if (XPath != value) {
285                                         ViewState ["XPath"] = value;
286                                         OnDataSourceChanged(EventArgs.Empty);
287                                 }
288                         }
289                 }
290         }
291 }
292 #endif
293