2 // System.Web.UI.WebControls.XmlDataSource
5 // Ben Maurer (bmaurer@users.sourceforge.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
37 using System.ComponentModel;
40 namespace System.Web.UI.WebControls {
41 public class XmlDataSource : HierarchicalDataSourceControl, IDataSource, IListSource {
44 event EventHandler IDataSource.DataSourceChanged {
45 add { ((IHierarchicalDataSource)this).DataSourceChanged += value; }
46 remove { ((IHierarchicalDataSource)this).DataSourceChanged -= value; }
49 static object EventTransforming = new object ();
50 public event EventHandler Transforming {
51 add { Events.AddHandler (EventTransforming, value); }
52 remove { Events.RemoveHandler (EventTransforming, value); }
55 protected virtual void OnTransforming (EventArgs e)
57 EventHandler eh = Events [EventTransforming] as EventHandler;
62 XmlDataDocument xmlDataDocument;
63 public XmlDataDocument GetXmlDataDocument ()
65 if (xmlDataDocument == null) {
66 xmlDataDocument = new XmlDataDocument ();
67 LoadXmlDataDocument (xmlDataDocument);
69 return xmlDataDocument;
72 [MonoTODO ("XSLT, schema")]
73 void LoadXmlDataDocument (XmlDataDocument document)
75 if (Transform == "" && TransformFile == "") {
77 document.Load (MapPathSecure (DataFile));
79 document.LoadXml (Data);
81 throw new NotImplementedException ("XSLT transform not implemented");
88 throw new InvalidOperationException ();
90 xmlDataDocument.Save (MapPathSecure (DataFile));
95 return !ReadOnly && Transform == "" && TransformFile == "" && DataFile != "";
100 protected override void LoadViewState (object savedState)
102 base.LoadViewState (savedState);
106 protected override object SaveViewState ()
108 return base.SaveViewState ();
112 protected override void TrackViewState ()
114 base.TrackViewState ();
117 protected override HierarchicalDataSourceView GetHierarchicalView (string viewPath)
119 XmlNode doc = this.GetXmlDataDocument ();
120 XmlNodeList ret = null;
122 if (viewPath != "") {
123 XmlNode n = doc.SelectSingleNode (viewPath);
126 } else if (XPath != "") {
127 ret = doc.SelectNodes (XPath);
129 ret = doc.ChildNodes;
132 return new XmlHierarchicalDataSourceView (ret);
135 IList IListSource.GetList ()
137 return ListSourceHelper.GetList (this);
140 bool IListSource.ContainsListCollection {
141 get { return ListSourceHelper.ContainsListCollection (this); }
144 DataSourceView IDataSource.GetView (string viewName)
147 viewName = "DefaultView";
149 return new XmlDataSourceView (this, viewName, GetXmlDataDocument ().SelectNodes (XPath != "" ? XPath : "."));
152 ICollection IDataSource.GetViewNames ()
154 return new string [] { "DefaultView" };
157 public virtual bool AutoSave {
159 object ret = ViewState ["AutoSave"];
160 return ret != null ? (bool)ret : true;
163 ViewState ["AutoSave"] = value;
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 {
175 string ret = ViewState ["Data"] as string;
176 return ret != null ? ret : "";
180 ViewState ["Data"] = value;
181 xmlDataDocument = null;
182 OnDataSourceChanged(EventArgs.Empty);
187 public virtual string DataFile {
189 string ret = ViewState ["DataFile"] as string;
190 return ret != null ? ret : "";
193 if (DataFile != value) {
194 ViewState ["DataFile"] = value;
195 xmlDataDocument = null;
196 OnDataSourceChanged(EventArgs.Empty);
201 public virtual bool ReadOnly {
203 object ret = ViewState ["ReadOnly"];
204 return ret != null ? (bool)ret : true;
207 ViewState ["ReadOnly"] = value;
211 public virtual string Schema {
213 string ret = ViewState ["Schema"] as string;
214 return ret != null ? ret : "";
217 if (Schema != value) {
218 ViewState ["Schema"] = value;
219 xmlDataDocument = null;
220 OnDataSourceChanged(EventArgs.Empty);
225 public virtual string SchemaFile {
227 string ret = ViewState ["SchemaFile"] as string;
228 return ret != null ? ret : "";
231 if (SchemaFile != value) {
232 ViewState ["SchemaFile"] = value;
233 xmlDataDocument = null;
234 OnDataSourceChanged(EventArgs.Empty);
239 XsltArgumentList transformArgumentList;
240 public virtual XsltArgumentList TransformArgumentList {
241 get { return transformArgumentList; }
242 set { transformArgumentList = value; }
245 public virtual string Transform {
247 string ret = ViewState ["Transform"] as string;
248 return ret != null ? ret : "";
251 if (Transform != value) {
252 ViewState ["Transform"] = value;
253 xmlDataDocument = null;
254 OnDataSourceChanged(EventArgs.Empty);
259 public virtual string TransformFile {
261 string ret = ViewState ["TransformFile"] as string;
262 return ret != null ? ret : "";
265 if (TransformFile != value) {
266 ViewState ["TransformFile"] = value;
267 xmlDataDocument = null;
268 OnDataSourceChanged(EventArgs.Empty);
273 public virtual string XPath {
275 string ret = ViewState ["XPath"] as string;
276 return ret != null ? ret : "";
279 if (XPath != value) {
280 ViewState ["XPath"] = value;
281 OnDataSourceChanged(EventArgs.Empty);