2002-11-30 Daniel Morgan <danmorg@sc.rr.com>
[mono.git] / mcs / class / System.Data / System.Xml / XmlDataDocument.cs
1 //
2 // mcs/class/System.Data/System.Xml/XmlDataDocument.cs
3 //
4 // Purpose: Provides a W3C XML DOM Document to interact with
5 //          relational data in a DataSet
6 //
7 // class: XmlDataDocument
8 // assembly: System.Data.dll
9 // namespace: System.Xml
10 //
11 // Author:
12 //     Daniel Morgan <danmorg@sc.rr.com>
13 //     Ville Palo <vi64pa@koti.soon.fi>
14 //
15 // (c)copyright 2002 Daniel Morgan
16 //
17 // XmlDataDocument is included within the Mono Class Library.
18 //
19
20 using System;
21 using System.Data;
22 using System.IO;
23 using System.Text;
24 using System.Xml.XPath;
25 using System.Collections;
26
27 namespace System.Xml {
28
29         public class XmlDataDocument : XmlDocument {
30
31                 #region Fields
32
33                 private DataSet dataSet;
34                 private bool isReadOnly = false;
35
36                 private int dataRowID = 1;
37                 private ArrayList dataRowIDList = new ArrayList ();
38
39                 #endregion // Fields
40
41                 #region Constructors
42
43                 public XmlDataDocument() {
44                         dataSet = new DataSet();                        
45                 }
46
47                 public XmlDataDocument(DataSet dataset) {
48                         this.dataSet = dataset;
49                 }
50
51                 #endregion // Constructors
52
53                 #region Public Properties
54
55                 public override string BaseURI {
56                         [MonoTODO]
57                         get {
58                                 // TODO: why are we overriding?
59                                 return base.BaseURI;
60                         }
61                 }
62
63                 public DataSet DataSet {
64                         [MonoTODO]
65                         get {
66                                 return dataSet;
67                         }
68                 }
69
70                 // override inheritted method from XmlDocument
71                 public override string InnerXml {
72                         [MonoTODO]
73                         get {
74                                 throw new NotImplementedException();
75                         }
76                                  
77                         [MonoTODO]
78                         set {
79                                 throw new NotImplementedException();
80                         }
81                 }
82
83                 public override bool IsReadOnly {
84                         [MonoTODO]
85                         get {
86                                 return isReadOnly;
87                         }
88
89                 }
90
91                 // Item indexer
92                 public override XmlElement this[string name] {
93                         [MonoTODO]
94                         get {
95                                 throw new NotImplementedException();
96                         }
97                 }
98
99                 // Item indexer
100                 public override XmlElement this[string localname, string ns] {
101                         [MonoTODO]
102                         get {
103                                 throw new NotImplementedException();
104                         }
105                 }
106
107                 public override string LocalName {
108                         [MonoTODO]
109                         get {
110                                 throw new NotImplementedException();
111                         }
112                 }
113
114                 public override string Name {
115                         [MonoTODO]
116                         get {
117                                 throw new NotImplementedException();
118                         }
119                 }
120
121                 public override XmlDocument OwnerDocument {
122                         [MonoTODO]
123                         get {
124                                 return null;
125                         }
126                 }
127
128                 #endregion // Public Properties
129
130                 #region Public Methods
131
132                 [MonoTODO]
133                 public override XmlNode CloneNode(bool deep) 
134                 {
135                         throw new NotImplementedException();
136                 }
137
138                 #region overloaded CreateElement methods
139
140                 [MonoTODO]
141                 public new XmlElement CreateElement(string prefix,
142                                 string localName, string namespaceURI) 
143                 {
144                         throw new NotImplementedException();
145                 }
146
147                 [MonoTODO]
148                 public new XmlElement CreateElement(string qualifiedName,
149                                 string namespaceURI) 
150                 {
151                         throw new NotImplementedException();
152                 }
153
154                 [MonoTODO]
155                 public new XmlElement CreateElement(string name) 
156                 {
157                         throw new NotImplementedException();
158                 }
159
160                 #endregion // overloaded CreateElement Methods
161                         
162                 // will not be supported
163                 public override XmlEntityReference CreateEntityReference(string name) 
164                 {
165                         throw new NotSupportedException();
166                 }
167                 
168                 // will not be supported
169                 public override XmlElement GetElementById(string elemId) 
170                 {
171                         throw new NotSupportedException();
172                 }
173
174                 // get the XmlElement associated with the DataRow
175                 public XmlElement GetElementFromRow(DataRow r) 
176                 {
177                         throw new NotImplementedException();
178                 }
179
180                 // get the DataRow associated with the XmlElement
181                 [MonoTODO]
182                 public DataRow GetRowFromElement(XmlElement e)
183                 {
184                         throw new NotImplementedException();
185                 }
186
187                 #region overload Load methods
188
189                 [MonoTODO]
190                 public override void Load(Stream inStream) {
191                         
192                 }
193
194                 [MonoTODO]
195                 public override void Load(string filename) {
196                         throw new NotImplementedException();
197                 }
198
199                 [MonoTODO]
200                 public override void Load(TextReader txtReader) {
201                         throw new NotImplementedException();
202                 }
203
204                 [MonoTODO]
205                 public override void Load(XmlReader reader) {
206                         
207                         DataTable dt = null;
208
209                         // For reading xml to XmlDocument
210                         XmlTextReader textReader = new XmlTextReader (
211                                 reader.BaseURI);
212
213                         if (reader.NodeType != XmlNodeType.Element)
214                                 reader.MoveToContent ();
215
216                         // TODO: Findout which exception should be throwen
217                         if (reader.NodeType != XmlNodeType.Element)
218                                 throw new Exception ();
219
220                         if (dataSet.DataSetName != reader.Name)
221                                 throw new Exception ();
222
223                         // read to next element
224                         while (reader.Read () && reader.NodeType != XmlNodeType.Element);
225
226                         do {
227
228                                 // Find right table from tablecollection
229                                 for (int i = 0; i < DataSet.Tables.Count && dt == null; i++) {
230                                         if (reader.Name == DataSet.Tables [i].TableName) {
231                                                 dt = DataSet.Tables [i];
232                                                 dt.ColumnChanged += new DataColumnChangeEventHandler (OnDataTableColumnChanged);
233
234                                         }
235                                 }
236                                 
237                                 // TODO: Findout what kind of exception 
238                                 if (dt == null) 
239                                         throw new Exception (); // there were no correct table
240                                 
241                                 while ((reader.NodeType != XmlNodeType.EndElement ||
242                                         reader.Name != dt.TableName) && reader.Read()) {
243                                         
244                                         switch (reader.NodeType) {
245                                                 
246                                         case XmlNodeType.Element:
247                                                 dt.Rows.Add (LoadRow (reader, dt.NewRow ()));
248                                                 
249                                                 break;
250                                         default:
251                                                 break;
252                                         }                       
253                                 }               
254                         } while (reader.Read ());
255
256                         base.Load (textReader);
257                 }
258                 
259                 #endregion // overloaded Load methods
260
261                 [MonoTODO]
262                 public override void WriteContentTo(XmlWriter xw) {
263                         base.WriteContentTo (xw);
264                 }
265
266                 [MonoTODO]
267                 public override void WriteTo(XmlWriter w) {
268                         base.WriteTo (w);
269                 }
270
271                 #endregion // Public Methods
272
273                 #region Protected Methods
274
275                 //FIXME: how do you handle this?
276                 //[MonoTODO]
277                 //protected internal override XPathNavigator CreateNavigator(XmlNode node) {
278                 //      throw new NotImplementedException();
279                 //}
280
281                 [MonoTODO]
282                 public new XPathNavigator CreateNavigator() {
283                         throw new NotImplementedException();
284                 }
285
286                 #endregion // Protected Methods
287                 
288                 #region Private Methods
289
290                 [MonoTODO]
291                 private void OnDataTableColumnChanged(object sender, 
292                                                              DataColumnChangeEventArgs eventArgs)
293                 {
294                         // row is not yet in datatable
295                         if (eventArgs.Row.XmlRowID == 0)
296                                 return;
297
298                         // TODO: Here should be some kind of error checking.
299                         GetElementsByTagName (eventArgs.Column.ToString ()) [dataRowIDList.IndexOf (
300                                 eventArgs.Row.XmlRowID)].InnerText = (string)eventArgs.ProposedValue;
301                         
302                         
303                 }
304         
305                 [MonoTODO]
306                 private static void OnDataTableRowDeleted(object sender,
307                                                           DataRowChangeEventArgs eventArgs)
308                 {
309                         throw new NotImplementedException();
310                 }
311                 
312                 [MonoTODO]
313                 private DataRow LoadRow (XmlReader reader, DataRow row)
314                 {
315                         // This method returns DataRow filled by values
316                         // from xmldocument
317                         string rowname = reader.Name;
318                         string column = "";
319                                         
320                         if (reader.NodeType == XmlNodeType.Element)
321                                 column = reader.Name;
322
323                         reader.Read ();
324
325                         if (reader.NodeType == XmlNodeType.Text) {
326
327                                         string val = reader.Value;
328                                         if (row.Table.Columns.Contains (column))
329                                                 row [column] = val;
330                         }
331
332                         // Every row must have unique id.
333                         row.XmlRowID = dataRowID;
334                         dataRowIDList.Add (dataRowID);
335                         dataRowID++;                                    
336
337                         return row;
338                 }
339
340                 #endregion
341         }
342 }