2002-12-06 Ville Palo <vi64pa@koti.soon.fi>
authorVille Palo <ville@mono-cvs.ximian.com>
Fri, 6 Dec 2002 11:04:48 +0000 (11:04 -0000)
committerVille Palo <ville@mono-cvs.ximian.com>
Fri, 6 Dec 2002 11:04:48 +0000 (11:04 -0000)
* DataSet.cs: Clean up to reading xmlschema. This looks much better
now (work better too), but it not working correctly yet.

svn path=/trunk/mcs/; revision=9402

mcs/class/System.Data/System.Data/ChangeLog
mcs/class/System.Data/System.Data/DataSet.cs

index 09c00c63faa05954619917aa52fe5f69b83d51dc..4abc64faa5c4648ebf23e1987f4a47d41fb1db99 100644 (file)
@@ -1,3 +1,8 @@
+2002-12-06  Ville Palo <vi64pa@koti.soon.fi>\r
+\r
+       * DataSet.cs: Clean up to reading xmlschema. This looks much better \r
+       now (work better too), but it not working correctly yet.\r
+       \r
 2002-12-04  Ville Palo <vi64pa@koti.soon.fi>\r
 \r
        * DataRow.cs: \r
index 711d488eedcab6beacdb6da2de57894a355b66d7..d3e493f5e99b9df03d30b9a0a7ff150d5a973e3b 100644 (file)
@@ -704,113 +704,120 @@ namespace System.Data {
                private void DoReadXmlSchema(XmlReader reader)
                {
                        DataTable dt = new DataTable ();
-                       XmlSchema schema = XmlSchema.Read (reader, new ValidationEventHandler (OnXmlSchemaValidation));
-
-                       XmlSchemaSequence s;
-                       XmlSchemaElement e;
-               
-                       XmlSchemaComplexType t;
-                       XmlSchemaChoice c;
-
-                       string oldSchemaName = "";
-                       string schemaTypeName = "";
-                       string tableTypeName = "";
-        
+                       XmlSchema schema = XmlSchema.Read (reader, null);
+                       XmlSchemaObjectCollection SimpleTypes = new XmlSchemaObjectCollection ();
+                       XmlSchemaObjectCollection ComplexTypes = new XmlSchemaObjectCollection ();
+                       
+                       // Get types
                        for (int i = 0; i < schema.Items.Count; i++) {
 
-                               // TODO: other types!
-                               if ((e = schema.Items [i] as XmlSchemaElement) != null) {
+                               XmlSchemaSimpleType simpleType = null;
+                               XmlSchemaComplexType complexType = null;
+                               if ((simpleType = schema.Items [i] as XmlSchemaSimpleType) != null) {
+                                       SimpleTypes.Add (simpleType);
+                               } 
+                               else if ((complexType = schema.Items [i] as XmlSchemaComplexType) != null) {
+                                       ComplexTypes.Add (complexType);
+                               }
+                       }
+                       
+                       // Get elements
+                       XmlSchemaObjectCollection Elements = new XmlSchemaObjectCollection ();
+                       for (int i = 0; i < schema.Items.Count; i++) {
 
-                                       dataSetName = e.Name;
-                                       
-                                       if (dataSetName != oldSchemaName) {
-                                               tableCollection.Add (dt);
-                                               oldSchemaName = e.Name;
-                                       }
+                               XmlSchemaElement element = null;
+                               XmlSchemaComplexType complexType = null;
+                               if ((element = schema.Items [i] as XmlSchemaElement) != null) {
 
-                                       if ((t = e.SchemaType as XmlSchemaComplexType) != null) {
+                                       // does element have type defined
+                                       if (!element.SchemaTypeName.IsEmpty)
+                                               ReadTypeElement (element, ComplexTypes, ref dt);
+                                       else if ((complexType = element.SchemaType as XmlSchemaComplexType) != null)
+                                               ReadColumnsFromSchema (complexType, ComplexTypes, ref dt);
 
-                                               if ((c = t.Particle as XmlSchemaChoice) != null) {
-                                                       
-                                                       for (int j = 0; j < c.Items.Count; j++) {
-                                                               if ((e = c.Items [j] as XmlSchemaElement) != null)
-                                                                       dt.TableName = e.Name;
-                                                               
-                                                               // TODO: other types
-                                                               if ((t = e.SchemaType as XmlSchemaComplexType) != null) {
-                                                                       
-                                                                       // TODO: other types
-                                                                       if ((s = t.Particle as XmlSchemaSequence) != null)  {
-                                                                               
-                                                                               // add columns to datatable
-                                                                               foreach (XmlSchemaElement el in s.Items)
-                                                                                       dt.Columns.Add (el.Name);
-                                                                       }
-                                                                       
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       else {
-                                               // If not known type then it's declared after this
-                                               schemaTypeName = e.SchemaTypeName.ToString ();
+                                       DataSet.Tables.Add (dt);
+                               }
+                       }               
+               }
+
+               [MonoTODO]
+               private bool ReadTypeElement (XmlSchemaElement el, XmlSchemaObjectCollection ComplexTypes, 
+                                             ref DataTable datatable) 
+               {
+                       /*
+                        * reads element's type
+                        */
+
+                       bool found = false;
+                       
+                       if (ComplexTypes.Count <= 0)
+                               return found;
+                       
+                       if (ComplexTypes[0] is XmlSchemaComplexType) {
+
+                               foreach (XmlSchemaComplexType c in ComplexTypes) {
+                                       
+                                       if (el.SchemaTypeName.ToString () == c.Name.ToString ()) {
                                                
-                                               // FIXME: when xmlschema works correcty this is not needed anymore
-                                               if (schemaTypeName.StartsWith (":"))
-                                                       schemaTypeName = schemaTypeName.Substring (1);
-                                       }
-                               } // TODO: SimpleType
-                               else if ((t = schema.Items [i] as XmlSchemaComplexType) != null) {
-                                                                               
-                                       if (t.Name == schemaTypeName) {
+                                               found = true;
+                                               ReadColumnsFromSchema (c, ComplexTypes, ref datatable);
+                                       }                                                               
+                               }
+                       } 
 
-                                               if ((s = t.Particle as XmlSchemaSequence) != null) {
-                                                       
-                                                       for (int j  = 0; j < s.Items.Count; j++) {
-                                                               
-                                                               if ((e = s.Items [j] as XmlSchemaElement) != null)
-                                                                       dt.TableName = e.Name;
-                                                               
-                                                               // TODO: other types
-                                                               if ((t = e.SchemaType as XmlSchemaComplexType) != null) {
-                                                                       
-                                                                       // TODO: other types
-                                                                       if ((s = t.Particle as XmlSchemaSequence) != null)  {
-                                                                               
-                                                                               // Add columns to datatable
-                                                                               foreach (XmlSchemaElement el in s.Items)
-                                                                                       dt.Columns.Add (el.Name);
-                                                                       }
-                                                                       
-                                                               }
-                                                               else {
-                                                                       // If table type is not known it's declared after this
-                                                                       tableTypeName = e.SchemaTypeName.ToString ();
-
-                                                                       // FIXME: when XmlSchema works correctly this is not 
-                                                                       // needed anymore
-                                                                       if (tableTypeName.StartsWith (":"))
-                                                                               tableTypeName = tableTypeName.Substring (1);
-                                                               }
-                                                       }                                                                                                                               
-                                               }
+                       return found;
+               }
+
+               [MonoTODO]
+               private void ReadColumnsFromSchema (XmlSchemaComplexType c, XmlSchemaObjectCollection ComplexTypes, 
+                                                   ref DataTable datatable)
+               {
+                       // FIXME: There is so much work to do. And i dont event know is this right way but here it is
+                       XmlSchemaSequence seq = null;
+                       XmlSchemaChoice choice = null;
+                       ArrayList names = new ArrayList ();
+
+                       if ((seq = c.Particle as XmlSchemaSequence) != null) {
+
+                               for (int i = 0; i < seq.Items.Count; i++) {
+
+                                       XmlSchemaElement element = null;
+                                       if ((element = seq.Items [i] as XmlSchemaElement) != null) {
+
+                                               // first is table name and after that comes column names
+                                               if (datatable.TableName == string.Empty)
+                                                       datatable.TableName = element.Name;                                             
+                                               else if (i + 1 > datatable.Columns.Count)
+                                                       datatable.Columns.Add (element.Name);
+
+                                               ReadTypeElement (element, ComplexTypes, ref datatable);
                                        }
-                                       
-                                       else if (t.Name == tableTypeName) { // table type declaration
+                               }
+                       }
+                       else if ((choice = c.Particle as XmlSchemaChoice) != null) {
+
+                               foreach (XmlSchemaObject obj in choice.Items) {
+                                       XmlSchemaElement e = null;
+
+                                       if ((e = obj as XmlSchemaElement) != null) {
 
-                                               if ((s = t.Particle as XmlSchemaSequence) != null) {
+                                               
+                                               // if element has type we got to find out what is it
+                                               if (e.SchemaType != null) {
+                                                       datatable.TableName = e.Name;
+                                                       ReadColumnsFromSchema (e.SchemaType as XmlSchemaComplexType, ComplexTypes, 
+                                                                      ref datatable);
+                                               }
+                                               else if (!e.RefName.IsEmpty) {
                                                        
-                                                       for (int j  = 0; j < s.Items.Count; j++) {
-                                                               
-                                                               if ((e = s.Items [j] as XmlSchemaElement) != null)
-                                                                       dt.Columns.Add (e.Name);
-                                                       }                                                                                                                                       
+                                                       // FIXME: this is wrong way to do. We should find element which is
+                                                       // referenced by e. I do that later
+                                                       dataSetName = e.Name;
+                                                       datatable.TableName = e.RefName.ToString ();
                                                }
-                                               
                                        }
                                }
                        }
-
                }
 
                #endregion
@@ -818,7 +825,6 @@ namespace System.Data {
                [MonoTODO]
                private void OnXmlSchemaValidation (object sender, ValidationEventArgs args)
                {
-                       ;
                }
        
                ///<summary>