2004-03-29 Umadevi S (sumadevi@novell.com)
[mono.git] / mcs / class / System.Data / System.Data / XmlDataLoader.cs
1 //
2 // mcs/class/System.Data/System.Data/XmlDataLoader.cs
3 //
4 // Purpose: Loads XmlDocument to DataSet 
5 //
6 // class: XmlDataLoader
7 // assembly: System.Data.dll
8 // namespace: System.Data
9 //
10 // Author:
11 //     Ville Palo <vi64pa@koti.soon.fi>
12 //
13 // (c)copyright 2002 Ville Palo
14 //
15 // XmlDataLoader is included within the Mono Class Library.
16 //
17
18 using System;
19 using System.Data;
20 using System.Xml;
21 using System.Xml.XPath;
22 using System.Collections;
23 using System.Globalization;
24
25 namespace System.Data 
26 {
27
28         internal class XmlDataLoader
29         {
30         
31                 private DataSet DSet;
32                 Hashtable DiffGrRows = new Hashtable ();
33
34                 public XmlDataLoader (DataSet set) 
35                 {
36                         DSet = set;
37                 }
38
39                 public XmlReadMode LoadData (XmlReader reader, XmlReadMode mode)
40                 {
41                         XmlReadMode Result = XmlReadMode.Auto;
42
43                         switch (mode) {
44
45                                 case XmlReadMode.Fragment:
46                                         break;
47                                 case XmlReadMode.ReadSchema:
48                                 case XmlReadMode.IgnoreSchema:
49                                 case XmlReadMode.InferSchema:
50                                         Result = mode;
51                                         ReadModeSchema (reader, mode);
52                                         break;
53                                 default:
54                                         break;
55                         }
56
57                         return Result;
58                 }
59
60                 #region reading
61
62                 // Read information from the reader.
63                 private void ReadModeSchema (XmlReader reader, XmlReadMode mode)
64                 {
65                         bool inferSchema = mode == XmlReadMode.InferSchema ? true : false;
66                         //check if the current element is schema.
67                         if (String.Compare (reader.LocalName, "schema", true) == 0) {
68                                 
69                                 if (mode == XmlReadMode.InferSchema || mode == XmlReadMode.IgnoreSchema)
70                                         reader.Skip(); // skip the schema node.
71                                 else
72                                         DSet.ReadXmlSchema(reader);
73                                 
74                                 reader.MoveToContent();
75                         }
76                         // load an XmlDocument from the reader.
77                         XmlDocument doc = BuildXmlDocument(reader);
78
79                         // treatment for .net compliancy :
80                         // if xml representing dataset has exactly depth of 2 elements,
81                         // than the root element actually represents datatable and not dataset
82                         // so we add new root element to doc 
83                         // in order to create an element representing dataset.
84                         int rootNodeDepth = XmlNodeElementsDepth(doc.DocumentElement);
85                         if (rootNodeDepth == 2) {
86                                 // new dataset name
87                                 String newDataSetName = "NewDataSet";
88                                 // create new document
89                                 XmlDocument newDoc = new XmlDocument();
90                                 // create element for dataset
91                                 XmlElement datasetElement = newDoc.CreateElement(newDataSetName);
92                                 // make the new created element to be the new doc root
93                                 newDoc.AppendChild(datasetElement);
94                                 // import all the elements from doc and insert them into new doc
95                                 XmlNode root = newDoc.ImportNode(doc.DocumentElement,true);
96                                 datasetElement.AppendChild(root);
97                                 doc = newDoc;
98                                 // update dataset name
99                                 DSet.DataSetName = newDataSetName;                      
100                         }
101
102                         // set EnforceConstraint to false - we do not want any validation during 
103                         // load time.
104                         bool origEnforceConstraint = DSet.EnforceConstraints;
105                         DSet.EnforceConstraints = false;
106
107                         // The childs are tables.
108                         XmlNodeList nList = doc.DocumentElement.ChildNodes;
109
110                         for (int i = 0; i < nList.Count; i++) {
111                                 XmlNode node = nList[i];
112                                 // node represents a table onky if it is of type XmlNodeType.Element
113                                 if (node.NodeType == XmlNodeType.Element) {
114                                         AddRowToTable(node, null, inferSchema);
115                                 }
116                         }
117
118                         // set the EnforceConstraints to original value;
119                         DSet.EnforceConstraints = origEnforceConstraint;
120                 }
121
122                 #endregion // reading
123
124                 #region Private helper methods
125                 
126                 private void ReadColumns (XmlReader reader, DataRow row, DataTable table, string TableName)
127                 {
128                         do {
129                                 if (reader.NodeType == XmlNodeType.Element) {
130                                         DataColumn col = table.Columns [reader.LocalName];
131                                         if (col != null) {
132                                                 row [col] = StringToObject (col.DataType, reader.Value);
133                                         }
134                                         reader.Read ();
135                                 }
136                                 else {
137                                         reader.Read ();
138                                 }
139                                 
140                         } while (table.TableName != reader.LocalName 
141                                 || reader.NodeType != XmlNodeType.EndElement);
142                 }
143
144                 internal static object StringToObject (Type type, string value)
145                 {
146                         if (type == null) return value;
147
148                         switch (Type.GetTypeCode (type)) {
149                                 case TypeCode.Boolean: return XmlConvert.ToBoolean (value);
150                                 case TypeCode.Byte: return XmlConvert.ToByte (value);
151                                 case TypeCode.Char: return (char)XmlConvert.ToInt32 (value);
152                                 case TypeCode.DateTime: return XmlConvert.ToDateTime (value);
153                                 case TypeCode.Decimal: return XmlConvert.ToDecimal (value);
154                                 case TypeCode.Double: return XmlConvert.ToDouble (value);
155                                 case TypeCode.Int16: return XmlConvert.ToInt16 (value);
156                                 case TypeCode.Int32: return XmlConvert.ToInt32 (value);
157                                 case TypeCode.Int64: return XmlConvert.ToInt64 (value);
158                                 case TypeCode.SByte: return XmlConvert.ToSByte (value);
159                                 case TypeCode.Single: return XmlConvert.ToSingle (value);
160                                 case TypeCode.UInt16: return XmlConvert.ToUInt16 (value);
161                                 case TypeCode.UInt32: return XmlConvert.ToUInt32 (value);
162                                 case TypeCode.UInt64: return XmlConvert.ToUInt64 (value);
163                         }
164
165                         if (type == typeof (TimeSpan)) return XmlConvert.ToTimeSpan (value);
166                         if (type == typeof (byte[])) return Convert.FromBase64String (value);
167
168                         return Convert.ChangeType (value, type);
169                 }
170
171                 private void AddRowToTable(XmlNode tableNode, DataColumn relationColumn, bool inferSchema)
172                 {
173                         Hashtable rowValue = new Hashtable();
174                         DataTable table;
175                         
176                         // Check if the table exists in the DataSet. If not create one.
177                         if (DSet.Tables.Contains(tableNode.LocalName))
178                                 table = DSet.Tables[tableNode.LocalName];
179                         else if (inferSchema) {
180                                 table = new DataTable(tableNode.LocalName);
181                                 DSet.Tables.Add(table);
182                         }
183                         else
184                                 return;
185
186                         // For elements that are inferred as tables and that contain text 
187                         // but have no child elements, a new column named "TableName_Text" 
188                         // is created for the text of each of the elements. 
189                         // If an element is inferred as a table and has text, but also has child elements,
190                         // the text is ignored.
191                         // Note : if an element is inferred as a table and has text 
192                         // and has no child elements, 
193                         // but the repeated ements of this table have child elements, 
194                         // then the text is ignored.
195                         if(!HaveChildElements(tableNode) && HaveText(tableNode) &&
196                                 !IsRepeatedHaveChildNodes(tableNode)) {
197                                 string columnName = tableNode.Name + "_Text";
198                                 if (!table.Columns.Contains(columnName)) {
199                                         table.Columns.Add(columnName);
200                                 }
201                                 rowValue.Add(columnName, tableNode.InnerText);
202                         }
203                         
204                         // Get the child nodes of the table. Any child can be one of the following tow:
205                         // 1. DataTable - if there was a relation with another table..
206                         // 2. DataColumn - column of the current table.
207                         XmlNodeList childList = tableNode.ChildNodes;
208                         for (int i = 0; i < childList.Count; i++) {
209                                 XmlNode childNode = childList[i];
210
211                                 // we are looping through elements only
212                                 // Note : if an element is inferred as a table and has text, but also has child elements,
213                                 // the text is ignored.
214                                 if (childNode.NodeType != XmlNodeType.Element)
215                                         continue;
216                                 
217                                 // Elements that have attributes are inferred as tables. 
218                                 // Elements that have child elements are inferred as tables. 
219                                 // Elements that repeat are inferred as a single table. 
220                                 if (IsInferedAsTable(childNode)) {
221                                         // child node infered as table
222                                         if (inferSchema) {
223                                                 // We need to create new column for the relation between the current
224                                                 // table and the new table we found (the child table).
225                                                 string newRelationColumnName = table.TableName + "_Id";
226                                                 if (!table.Columns.Contains(newRelationColumnName)) {
227                                                         DataColumn newRelationColumn = new DataColumn(newRelationColumnName, typeof(int));
228                                                         newRelationColumn.AutoIncrement = true;
229                                                         // we do not want to serialize this column so MappingType is Hidden.
230                                                         newRelationColumn.ColumnMapping = MappingType.Hidden;
231                                                         table.Columns.Add(newRelationColumn);
232                                                 }
233                                                 // Add a row to the new table we found.
234                                                 AddRowToTable(childNode, table.Columns[newRelationColumnName], inferSchema);
235                                         }
236                                         else
237                                                 AddRowToTable(childNode, null, inferSchema);
238                                         
239                                 }
240                                 else {
241                                         // Elements that have no attributes or child elements, and do not repeat, 
242                                         // are inferred as columns.
243                                         object val = null;
244                                         if (childNode.FirstChild != null)
245                                                 val = childNode.FirstChild.Value;
246                                         else
247                                                 val = "";
248                                         if (table.Columns.Contains(childNode.LocalName))
249                                                 rowValue.Add(childNode.LocalName, val);
250                                         else if (inferSchema) {
251                                                 table.Columns.Add(childNode.LocalName);
252                                                 rowValue.Add(childNode.LocalName, val);
253                                         }
254                                 }
255                                                 
256                         }
257
258                         // Column can be attribute of the table element.
259                         XmlAttributeCollection aCollection = tableNode.Attributes;
260                         for (int i = 0; i < aCollection.Count; i++) {
261                                 XmlAttribute attr = aCollection[i];
262                                 //the atrribute can be the namespace.
263                                 if (attr.Prefix.Equals("xmlns"))
264                                         table.Namespace = attr.Value;
265                                 else { // the attribute is a column.
266                                         if (!table.Columns.Contains(attr.LocalName))
267                                                 table.Columns.Add(attr.LocalName);
268                                         table.Columns[attr.LocalName].Namespace = table.Namespace;
269
270                                         rowValue.Add(attr.LocalName, attr.Value);
271                                 }
272                         }
273
274                         // If the current table is a child table we need to add a new column for the relation
275                         // and add a new relation to the DataSet.
276                         if (relationColumn != null) {
277                                 if (!table.Columns.Contains(relationColumn.ColumnName)) {
278                                         DataColumn dc = new DataColumn(relationColumn.ColumnName, typeof(int));
279                                         // we do not want to serialize this column so MappingType is Hidden.
280                                         dc.ColumnMapping = MappingType.Hidden;
281                                         table.Columns.Add(dc);
282                                         // Convention of relation name is: ParentTableName_ChildTableName
283                                         DataRelation dr = new DataRelation(relationColumn.Table.TableName + "_" + dc.Table.TableName, relationColumn, dc);
284                                         dr.Nested = true;
285                                         DSet.Relations.Add(dr);
286                                 }
287                                 rowValue.Add (relationColumn.ColumnName, relationColumn.GetAutoIncrementValue());
288                         }
289
290                         // Create new row and add all values to the row.
291                         // then add it to the table.
292                         DataRow row = table.NewRow ();
293                                         
294                         IDictionaryEnumerator enumerator = rowValue.GetEnumerator ();
295                         while (enumerator.MoveNext ()) {
296                                 row [enumerator.Key.ToString ()] = StringToObject (table.Columns[enumerator.Key.ToString ()].DataType, enumerator.Value.ToString ());
297                         }
298
299                         table.Rows.Add (row);
300                         
301                 }
302                 
303                 // bulid the document from the reader.
304                 private XmlDocument BuildXmlDocument(XmlReader reader)
305                 {
306                         XmlDocument doc = new XmlDocument();
307                         // Create the root element. This is the DataSet element.
308                         XmlElement dataSetElement = doc.CreateElement(DSet.DataSetName);
309                         
310                         do {
311                                 XmlNode n = doc.ReadNode (reader);
312                                 if(n == null) break;
313                                 // Add the table nodes to the DataSet node.
314                                 dataSetElement.AppendChild (n);
315                         } while (reader.IsStartElement());
316                         
317                         // Add the DataSet element to the document.
318                         doc.AppendChild(dataSetElement);
319                         return doc;
320                 }
321
322                 // this method calculates the depth of child nodes tree
323                 // and it counts nodes of type XmlNodeType.Element only
324                 private static int XmlNodeElementsDepth(XmlNode node)
325                 {
326                         int maxDepth = -1;
327             if ((node != null)) {
328                                 if  ((node.HasChildNodes) && (node.FirstChild.NodeType == XmlNodeType.Element)) {
329                                         for (int i=0; i<node.ChildNodes.Count; i++) {
330                                                 if (node.ChildNodes[i].NodeType == XmlNodeType.Element) {
331                                                         int childDepth = XmlNodeElementsDepth(node.ChildNodes[i]);
332                                                         maxDepth = (maxDepth < childDepth) ? childDepth : maxDepth;
333                                                 }
334                                         }
335                                 }
336                                 else {
337                                         return 1;
338                                 }
339                         }
340                         else {
341                                 return -1;
342                         }
343
344                         return (maxDepth + 1);
345                 }
346
347                 private bool HaveChildElements(XmlNode node)
348                 {
349                         bool haveChildElements = true;
350                         if(node.ChildNodes.Count > 0) {
351                                 foreach(XmlNode childNode in node.ChildNodes) {
352                                         if (childNode.NodeType != XmlNodeType.Element) {
353                                                 haveChildElements = false;
354                                                 break;
355                                         }
356                                 }
357                         }
358                         else {
359                                 haveChildElements = false;
360                         }
361                         return haveChildElements;
362                 }
363
364                 private bool HaveText(XmlNode node)
365                 {
366                         bool haveText = true;
367                         if(node.ChildNodes.Count > 0) {
368                                 foreach(XmlNode childNode in node.ChildNodes) {
369                                         if (childNode.NodeType != XmlNodeType.Text) {
370                                                 haveText = false;
371                                                 break;
372                                         }
373                                 }
374                         }
375                         else {
376                                 haveText = false;
377                         }
378                         return haveText;
379                 }
380
381                 private bool IsRepeat(XmlNode node)
382                 {
383                         bool isRepeat = false;
384                         if(node.ParentNode != null) {
385                                 foreach(XmlNode childNode in node.ParentNode.ChildNodes) {
386                                         if(childNode != node && childNode.Name == node.Name) {
387                                                 isRepeat = true;
388                                                 break;
389                                         }
390                                 }
391                         }
392                         return isRepeat;
393                 }
394
395                 private bool HaveAttributes(XmlNode node)
396                 {
397                         return (node.Attributes != null && node.Attributes.Count > 0);
398                 }
399
400                 private bool IsInferedAsTable(XmlNode node)
401                 {
402                         // Elements that have attributes are inferred as tables. 
403                         // Elements that have child elements are inferred as tables. 
404                         // Elements that repeat are inferred as a single table. 
405                         return (HaveChildElements(node) || HaveAttributes(node) ||
406                                         IsRepeat(node));
407                 }
408
409                 /// <summary>
410                 /// Returns true is any node that is repeated node for the node supplied
411                 /// (i.e. is child node of node's parent, have the same name and is not the node itself)
412                 /// have child elements
413                 /// </summary>
414                 private bool IsRepeatedHaveChildNodes(XmlNode node)
415                 {
416                         bool isRepeatedHaveChildElements = false;
417                         if(node.ParentNode != null) {
418                                 foreach(XmlNode childNode in node.ParentNode.ChildNodes) {
419                                         if(childNode != node && childNode.Name == node.Name) {
420                                                 if (HaveChildElements(childNode)) {
421                                                         isRepeatedHaveChildElements = true;
422                                                         break;
423                                                 }
424                                         }
425                                 }
426                         }
427                         return isRepeatedHaveChildElements;
428                 }
429
430                 #endregion // Private helper methods
431
432                 
433         }
434
435 }