2004-05-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 #if NET_2_0
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.Text;
14 using System.Xml;
15 using System.Xml.Xsl;
16 using System.ComponentModel;
17 using System.IO;
18
19 namespace System.Web.UI.WebControls {
20         public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
21
22                 
23                 event EventHandler IDataSource.DataSourceChanged {
24                         add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
25                         remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
26                 }
27                 
28                 static object EventTransforming = new object ();
29                 public event EventHandler Transforming {
30                         add { Events.AddHandler (EventTransforming, value); }
31                         remove { Events.RemoveHandler (EventTransforming, value); }
32                 }
33                 
34                 protected virtual void OnTransforming (EventArgs e)
35                 {
36                         EventHandler eh = Events [EventTransforming] as EventHandler;
37                         if (eh != null)
38                                 eh (this, e);
39                 }
40                 
41                 XmlDataDocument xmlDataDocument;
42                 public XmlDataDocument GetXmlDataDocument ()
43                 {
44                         if (xmlDataDocument == null) {
45                                 xmlDataDocument = new XmlDataDocument ();
46                                 LoadXmlDataDocument (xmlDataDocument);
47                         }
48                         return xmlDataDocument;
49                 }
50                 
51                 [MonoTODO ("XSLT, schema")]
52                 void LoadXmlDataDocument (XmlDataDocument document)
53                 {
54                         if (Transform == "" && TransformFile == "") {
55                                 if (DataFile != "")
56                                         document.Load (MapPathSecure (DataFile));
57                                 else
58                                         document.LoadXml (Data);
59                         } else {
60                                 throw new NotImplementedException ("XSLT transform not implemented");
61                         }
62                 }
63
64                 public void Save ()
65                 {
66                         if (!CanBeSaved)
67                                 throw new InvalidOperationException ();
68                         
69                         xmlDataDocument.Save (MapPathSecure (DataFile));
70                 }
71                 
72                 bool CanBeSaved {
73                         get {
74                                 return !ReadOnly && Transform == "" && TransformFile == "" && DataFile != "";
75                         }
76                 }
77                 
78                 [MonoTODO]
79                 protected override void LoadViewState (object savedState)
80                 {
81                         base.LoadViewState (savedState);
82                 }
83                 
84                 [MonoTODO]
85                 protected override object SaveViewState ()
86                 {
87                         return base.SaveViewState ();
88                 }
89                 
90                 [MonoTODO]
91                 protected override void TrackViewState ()
92                 {
93                         base.TrackViewState ();
94                 }
95                 
96                 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
97                 {
98                         XmlNode doc = this.GetXmlDataDocument ();
99                         XmlNodeList ret = null;
100                         
101                         if (viewPath != "") {
102                                 XmlNode n = doc.SelectSingleNode (viewPath);
103                                 if (n != null)
104                                         ret = n.ChildNodes;
105                         } else if (XPath != "") {
106                                 ret = doc.SelectNodes (XPath);
107                         } else {
108                                 ret = doc.ChildNodes;
109                         }
110                         
111                         return new XmlHierarchicalDataSourceView (ret);
112                 }
113                 
114                 IList IListSource.GetList ()
115                 {
116                         return ListSourceHelper.GetList (this);
117                 }
118                 
119                 bool IListSource.ContainsListCollection {
120                         get { return ListSourceHelper.ContainsListCollection (this); }
121                 }
122                 
123                 DataSourceView IDataSource.GetView (string viewName)
124                 {
125                         if (viewName == "")
126                                 viewName = "DefaultView";
127                         
128                         return new XmlDataSourceView (this, viewName, GetXmlDataDocument ().SelectNodes (XPath != "" ? XPath : "."));
129                 }
130                 
131                 ICollection IDataSource.GetViewNames ()
132                 {
133                         return new string [] { "DefaultView" };
134                 }
135                 
136                 public virtual bool AutoSave {
137                         get {
138                                 object ret = ViewState ["AutoSave"];
139                                 return ret != null ? (bool)ret : true;
140                         }
141                         set {
142                                 ViewState ["AutoSave"] = value;
143                         }
144                 }
145                 
146                 // TODO: stub these apis
147                 //protected virtual FileDataSourceCache Cache { get; }
148                 //public virtual int CacheDuration { get; set; }
149                 //public virtual DataSourceCacheExpiry CacheExpirationPolicy { get; set; }
150                 //public virtual string CacheKeyDependency { get; set; }
151                 //public virtual bool EnableCaching { get; set; }
152                 public virtual string Data {
153                         get {
154                                 string ret = ViewState ["Data"] as string;
155                                 return ret != null ? ret : "";
156                         }
157                         set {
158                                 if (Data != value) {
159                                         ViewState ["Data"] = value;
160                                         xmlDataDocument = null;
161                                         OnDataSourceChanged(EventArgs.Empty);
162                                 }
163                         }
164                 }
165                 
166                 public virtual string DataFile {
167                         get {
168                                 string ret = ViewState ["DataFile"] as string;
169                                 return ret != null ? ret : "";
170                         }
171                         set {
172                                 if (DataFile != value) {
173                                         ViewState ["DataFile"] = value;
174                                         xmlDataDocument = null;
175                                         OnDataSourceChanged(EventArgs.Empty);
176                                 }
177                         }
178                 }
179                 
180                 public virtual bool ReadOnly {
181                         get {
182                                 object ret = ViewState ["ReadOnly"];
183                                 return ret != null ? (bool)ret : true;
184                         }
185                         set {
186                                 ViewState ["ReadOnly"] = value;
187                         }
188                 }
189                 
190                 public virtual string Schema {
191                         get {
192                                 string ret = ViewState ["Schema"] as string;
193                                 return ret != null ? ret : "";
194                         }
195                         set {
196                                 if (Schema != value) {
197                                         ViewState ["Schema"] = value;
198                                         xmlDataDocument = null;
199                                         OnDataSourceChanged(EventArgs.Empty);
200                                 }
201                         }
202                 }
203                 
204                 public virtual string SchemaFile {
205                         get {
206                                 string ret = ViewState ["SchemaFile"] as string;
207                                 return ret != null ? ret : "";
208                         }
209                         set {
210                                 if (SchemaFile != value) {
211                                         ViewState ["SchemaFile"] = value;
212                                         xmlDataDocument = null;
213                                         OnDataSourceChanged(EventArgs.Empty);
214                                 }
215                         }
216                 }
217                 
218                 XsltArgumentList transformArgumentList;
219                 public virtual XsltArgumentList TransformArgumentList {
220                         get { return transformArgumentList; }
221                         set { transformArgumentList = value; }
222                 }
223                 
224                 public virtual string Transform {
225                         get {
226                                 string ret = ViewState ["Transform"] as string;
227                                 return ret != null ? ret : "";
228                         }
229                         set {
230                                 if (Transform != value) {
231                                         ViewState ["Transform"] = value;
232                                         xmlDataDocument = null;
233                                         OnDataSourceChanged(EventArgs.Empty);
234                                 }
235                         }
236                 }
237                 
238                 public virtual string TransformFile {
239                         get {
240                                 string ret = ViewState ["TransformFile"] as string;
241                                 return ret != null ? ret : "";
242                         }
243                         set {
244                                 if (TransformFile != value) {
245                                         ViewState ["TransformFile"] = value;
246                                         xmlDataDocument = null;
247                                         OnDataSourceChanged(EventArgs.Empty);
248                                 }
249                         }
250                 }
251                 
252                 public virtual string XPath {
253                         get {
254                                 string ret = ViewState ["XPath"] as string;
255                                 return ret != null ? ret : "";
256                         }
257                         set {
258                                 if (XPath != value) {
259                                         ViewState ["XPath"] = value;
260                                         OnDataSourceChanged(EventArgs.Empty);
261                                 }
262                         }
263                 }
264         }
265 }
266 #endif
267