ChangeLog: Updated.
[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 //
7 // (C) 2003 Ben Maurer
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Xml;
36 using System.Xml.Xsl;
37 using System.ComponentModel;
38 using System.IO;
39
40 namespace System.Web.UI.WebControls {
41         public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
42
43                 
44                 event EventHandler IDataSource.DataSourceChanged {
45                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
46                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
47                 }
48                 
49                 static object EventTransforming = new object ();
50                 public event EventHandler Transforming {
51                         add { Events.AddHandler (EventTransforming, value); }
52                         remove { Events.RemoveHandler (EventTransforming, value); }
53                 }
54                 
55                 protected virtual void OnTransforming (EventArgs e)
56                 {
57                         EventHandler eh = Events [EventTransforming] as EventHandler;
58                         if (eh != null)
59                                 eh (this, e);
60                 }
61                 
62                 XmlDataDocument xmlDataDocument;
63                 public XmlDataDocument GetXmlDataDocument ()
64                 {
65                         if (xmlDataDocument == null) {
66                                 xmlDataDocument = new XmlDataDocument ();
67                                 LoadXmlDataDocument (xmlDataDocument);
68                         }
69                         return xmlDataDocument;
70                 }
71                 
72                 [MonoTODO ("XSLT, schema")]
73                 void LoadXmlDataDocument (XmlDataDocument document)
74                 {
75                         if (Transform == "" && TransformFile == "") {
76                                 if (DataFile != "")
77                                         document.Load (MapPathSecure (DataFile));
78                                 else
79                                         document.LoadXml (Data);
80                         } else {
81                                 throw new NotImplementedException ("XSLT transform not implemented");
82                         }
83                 }
84
85                 public void Save ()
86                 {
87                         if (!CanBeSaved)
88                                 throw new InvalidOperationException ();
89                         
90                         xmlDataDocument.Save (MapPathSecure (DataFile));
91                 }
92                 
93                 bool CanBeSaved {
94                         get {
95                                 return !ReadOnly && Transform == "" && TransformFile == "" && DataFile != "";
96                         }
97                 }
98                 
99                 [MonoTODO]
100                 protected override void LoadViewState (object savedState)
101                 {
102                         base.LoadViewState (savedState);
103                 }
104                 
105                 [MonoTODO]
106                 protected override object SaveViewState ()
107                 {
108                         return base.SaveViewState ();
109                 }
110                 
111                 [MonoTODO]
112                 protected override void TrackViewState ()
113                 {
114                         base.TrackViewState ();
115                 }
116                 
117                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
118                 {
119                         XmlNode doc = this.GetXmlDataDocument ();
120                         XmlNodeList ret = null;
121                         
122                         if (viewPath != "") {
123                                 XmlNode n = doc.SelectSingleNode (viewPath);
124                                 if (n != null)
125                                         ret = n.ChildNodes;
126                         } else if (XPath != "") {
127                                 ret = doc.SelectNodes (XPath);
128                         } else {
129                                 ret = doc.ChildNodes;
130                         }
131                         
132                         return new XmlHierarchicalDataSourceView (ret);
133                 }
134                 
135                 IList IListSource.GetList ()
136                 {
137                         return ListSourceHelper.GetList (this);
138                 }
139                 
140                 bool IListSource.ContainsListCollection {
141                         get { return ListSourceHelper.ContainsListCollection (this); }
142                 }
143                 
144                 DataSourceView IDataSource.GetView (string viewName)
145                 {
146                         if (viewName == "")
147                                 viewName = "DefaultView";
148                         
149                         return new XmlDataSourceView (this, viewName, GetXmlDataDocument ().SelectNodes (XPath != "" ? XPath : "."));
150                 }
151                 
152                 ICollection IDataSource.GetViewNames ()
153                 {
154                         return new string [] { "DefaultView" };
155                 }
156                 
157                 public virtual bool AutoSave {
158                         get {
159                                 object ret = ViewState ["AutoSave"];
160                                 return ret != null ? (bool)ret : true;
161                         }
162                         set {
163                                 ViewState ["AutoSave"] = value;
164                         }
165                 }
166                 
167                 // TODO: stub these apis
168                 //protected virtual FileDataSourceCache Cache { get; }
169                 //public virtual int CacheDuration { get; set; }
170                 //public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
171                 //public virtual string CacheKeyDependency { get; set; }
172                 //public virtual bool EnableCaching { get; set; }
173                 public virtual string Data {
174                         get {
175                                 string ret = ViewState ["Data"] as string;
176                                 return ret != null ? ret : "";
177                         }
178                         set {
179                                 if (Data != value) {
180                                         ViewState ["Data"] = value;
181                                         xmlDataDocument = null;
182                                         OnDataSourceChanged(EventArgs.Empty);
183                                 }
184                         }
185                 }
186                 
187                 public virtual string DataFile {
188                         get {
189                                 string ret = ViewState ["DataFile"] as string;
190                                 return ret != null ? ret : "";
191                         }
192                         set {
193                                 if (DataFile != value) {
194                                         ViewState ["DataFile"] = value;
195                                         xmlDataDocument = null;
196                                         OnDataSourceChanged(EventArgs.Empty);
197                                 }
198                         }
199                 }
200                 
201                 public virtual bool ReadOnly {
202                         get {
203                                 object ret = ViewState ["ReadOnly"];
204                                 return ret != null ? (bool)ret : true;
205                         }
206                         set {
207                                 ViewState ["ReadOnly"] = value;
208                         }
209                 }
210                 
211                 public virtual string Schema {
212                         get {
213                                 string ret = ViewState ["Schema"] as string;
214                                 return ret != null ? ret : "";
215                         }
216                         set {
217                                 if (Schema != value) {
218                                         ViewState ["Schema"] = value;
219                                         xmlDataDocument = null;
220                                         OnDataSourceChanged(EventArgs.Empty);
221                                 }
222                         }
223                 }
224                 
225                 public virtual string SchemaFile {
226                         get {
227                                 string ret = ViewState ["SchemaFile"] as string;
228                                 return ret != null ? ret : "";
229                         }
230                         set {
231                                 if (SchemaFile != value) {
232                                         ViewState ["SchemaFile"] = value;
233                                         xmlDataDocument = null;
234                                         OnDataSourceChanged(EventArgs.Empty);
235                                 }
236                         }
237                 }
238                 
239                 XsltArgumentList transformArgumentList;
240                 public virtual XsltArgumentList TransformArgumentList {
241                         get { return transformArgumentList; }
242                         set { transformArgumentList = value; }
243                 }
244                 
245                 public virtual string Transform {
246                         get {
247                                 string ret = ViewState ["Transform"] as string;
248                                 return ret != null ? ret : "";
249                         }
250                         set {
251                                 if (Transform != value) {
252                                         ViewState ["Transform"] = value;
253                                         xmlDataDocument = null;
254                                         OnDataSourceChanged(EventArgs.Empty);
255                                 }
256                         }
257                 }
258                 
259                 public virtual string TransformFile {
260                         get {
261                                 string ret = ViewState ["TransformFile"] as string;
262                                 return ret != null ? ret : "";
263                         }
264                         set {
265                                 if (TransformFile != value) {
266                                         ViewState ["TransformFile"] = value;
267                                         xmlDataDocument = null;
268                                         OnDataSourceChanged(EventArgs.Empty);
269                                 }
270                         }
271                 }
272                 
273                 public virtual string XPath {
274                         get {
275                                 string ret = ViewState ["XPath"] as string;
276                                 return ret != null ? ret : "";
277                         }
278                         set {
279                                 if (XPath != value) {
280                                         ViewState ["XPath"] = value;
281                                         OnDataSourceChanged(EventArgs.Empty);
282                                 }
283                         }
284                 }
285         }
286 }
287 #endif
288