2009-10-29 Veerapuram Varadhan <vvaradhan@novell.com>
[mono.git] / mcs / class / System.Data / System.Data / XmlSchemaDataImporter.cs
1 //
2 // XmlSchemaDataImporter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C)2004 Novell Inc.
8 //
9 //
10 // ***** The design note became somewhat obsolete. Should be rewritten. *****
11 //
12 // * Design Notes
13 //
14 // ** Abstract
15 //
16 //      This class is used to import an XML Schema into a DataSet schema.
17 //
18 //      Only XmlReader is acceptable as the input to the class.
19 //      This class is not expected to read XML Schema multi time.
20 //
21 // ** Targetable Schema Components
22 //
23 //      Only global global elements that hold complex type are converted 
24 //      into a table. 
25 //      <del>
26 //      The components of the type of the element are subsequently converted
27 //      into a table, BUT there is an exception. As for "DataSet elements",
28 //      the type is just ignored (see "DataSet Element definition" below).
29 //      </del><ins>
30 //      The components of the type of the element are subsequently converted
31 //      into a table. As for "DataSet elements", its complex type is also 
32 //      handled.
33 //      </ins>
34 //
35 //      Unused complex types are never be converted.
36 //
37 //      Global simple types and global attributes are never converted.
38 //      They cannot be a table.
39 //      Local complex types are also converted into a table.
40 //
41 //      Local elements are converted into either a table or a column in
42 //      the "context DataTable". Simple-typed element is not always converted
43 //      into a DataColumn; if maxOccurs > 1, it will be converted as a table.
44 //
45 // ** Name Convention
46 //
47 //      Ignore this section. Microsoft.NET was buggy enough to confuse
48 //      against these name conflicts.
49 //
50 //      Since local complex types are anonymous, we have to name for each
51 //      component. Thus, and since complex types and elements can have the 
52 //      same name each other, we have to manage a table for mappings from 
53 //      a name to a component. The names must be also used in DataRelation
54 //      definitions correctly.
55 //
56 // ** DataSet element definition
57 //
58 //      "DataSet element" is 1) such element that has an attribute 
59 //      msdata:IsDataSet (where prefix "msdata" is bound to 
60 //      urn:schemas-microsoft-com:xml-msdata), or 2) the only one
61 //      element definition in the schema.
62 //
63 //      There is another complicated rule. 1) If there is only one element EL 
64 //      in the schema, and 2) if the type of EL is complex named CT, and 3)
65 //      the content of the CT is a group base, and 4) the group base contains 
66 //      an element EL2, and finally 5) if EL2 is complex, THEN the element is
67 //      the DataSet element.
68 //
69 //      Only the first global element that matches the condition above is
70 //      regarded as DataSet element (by necessary design or just a bug?) 
71 //      instead of handling as an error.
72 //
73 //      All global elements are considered as an alternative in the dataset
74 //      element.
75 //
76 //      For local elements, msdata:IsDataSet are just ignored.
77 //
78 // ** Importing Complex Types as Columns
79 //
80 //      When an xs:element is going to be mapped, its complex type (remember
81 //      that only complex-typed elements are targettable) are expanded to
82 //      DataColumn.
83 //
84 //      DataColumn has a property MappingType that shows whether this column
85 //       came from attribute or element.
86 //
87 //      [Question: How about MappingType.Simple? How is it used?]
88 //
89 //      Additionally, for particle elements, it might also create another
90 //      DataTable (but for the particle elements in context DataTable, it
91 //      will create an index to the new table).
92 //
93 //      For group base particles (XmlSchemaGroupBase; sequence, choice, all)
94 //      each component in those groups are mapped to a column. Even if you
95 //      import "choice" or "all" components, DataSet.WriteXmlSchema() will
96 //      output them just as a "sequence".
97 //
98 //      Columns cannot be added directly to current context DataTable; they
99 //      need to be added after processing all the columns, because they may
100 //      have msdata:Ordinal attribute that specifies the order of the columns
101 //      in the DataTable.
102 //
103 //      "Nested elements" are not allowed. (Clarification required?)
104 //
105 // ** Identity Constraints and DataRelations
106 //
107 // *** DataRelations from element identity constraints
108 //
109 //      Only constraints on "DataSet element" is considered. All other
110 //      constraint definitions are ignored. Note that it is DataSet that has
111 //      the property Relations (of type DataRelationCollection).
112 //
113 //      xs:key and xs:unique are handled as the same (then both will be
114 //      serialized as xs:unique).
115 //
116 //      The XPath expressions in the constraints are strictly limited; they
117 //      are expected to be expandable enough to be mappable for each
118 //
119 //              * selector to "any_valid_XPath/is/OK/blah" 
120 //                where "blah" is one of the DataTable name. It looks that
121 //                only the last QName section is significant and any heading
122 //                XPath step is OK (even if the mapped node does not exist).
123 //              * field to QName that is mapped to DataColumn in the DataTable
124 //                (even ./QName is not allowed)
125 //
126 // *** DataRelations from annotations
127 //
128 //      See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_mapping_relationship_specified_for_nested_elements.asp and http://msdn.microsoft.com/library/en-us/cpguide/html/_specifying_relationship_between_elements_with_no_nesting.asp
129 //
130 // ** Informative References
131 //
132 // Generating DataSet Relational Structure from XML Schema (XSD)
133 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/_generating_dataset_relational_structure_from_xsd.asp
134 //
135
136 //
137 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
138 //
139 // Permission is hereby granted, free of charge, to any person obtaining
140 // a copy of this software and associated documentation files (the
141 // "Software"), to deal in the Software without restriction, including
142 // without limitation the rights to use, copy, modify, merge, publish,
143 // distribute, sublicense, and/or sell copies of the Software, and to
144 // permit persons to whom the Software is furnished to do so, subject to
145 // the following conditions:
146 // 
147 // The above copyright notice and this permission notice shall be
148 // included in all copies or substantial portions of the Software.
149 // 
150 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
151 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
152 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
153 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
154 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
155 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
156 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
157 //
158
159 using System;
160 using System.Collections;
161 using System.Data;
162 using System.Data.Common;
163 using System.Globalization;
164 using System.Xml;
165 using System.Xml.Schema;
166
167
168 namespace System.Data
169 {
170         internal class TableStructureCollection : CollectionBase
171         {
172                 public void Add (TableStructure table)
173                 {
174                         List.Add (table);
175                 }
176
177                 public TableStructure this [int i] {
178                         get { return List [i] as TableStructure; }
179                 }
180
181                 public TableStructure this [string name] {
182                         get {
183                                 foreach (TableStructure ts in List)
184                                         if (ts.Table.TableName == name)
185                                                 return ts;
186                                 return null;
187                         }
188                 }
189         }
190
191         internal class RelationStructureCollection : CollectionBase
192         {
193                 public void Add (RelationStructure rel)
194                 {
195                         List.Add (rel);
196                 }
197
198                 public RelationStructure this [int i] {
199                         get { return List [i] as RelationStructure; }
200                 }
201
202                 public RelationStructure this [string parent, string child] {
203                         get {
204                                 foreach (RelationStructure rel in List)
205                                         if (rel.ParentTableName == parent && rel.ChildTableName == child)
206                                                 return rel;
207                                 return null;
208                         }
209                 }
210         }
211         
212         internal class TableStructure
213         {
214                 public TableStructure (DataTable table)
215                 {
216                         this.Table = table;
217                 }
218
219                 // The columns and orders which will be added to the context
220                 // table (See design notes; Because of the ordinal problem)
221                 public DataTable Table;
222                 public Hashtable OrdinalColumns = new Hashtable ();
223                 public ArrayList NonOrdinalColumns = new ArrayList ();
224                 public DataColumn PrimaryKey;
225
226                 public bool ContainsColumn (string name)
227                 {
228                         foreach (DataColumn col in NonOrdinalColumns)
229                                 if (col.ColumnName == name)
230                                         return true;
231                         foreach (DataColumn col in OrdinalColumns.Keys)
232                                 if (col.ColumnName == name)
233                                         return true;
234                         return false;
235                 }
236         }
237
238         internal class RelationStructure
239         {
240                 public string ExplicitName;
241                 public string ParentTableName;
242                 public string ChildTableName;
243                 public string ParentColumnName;
244                 public string ChildColumnName;
245                 public bool IsNested;
246                 public bool CreateConstraint;
247         }
248
249         internal class ConstraintStructure
250         {
251                 public readonly string TableName;
252                 public readonly string [] Columns;
253                 public readonly bool [] IsAttribute;
254                 public readonly string ConstraintName;
255                 public readonly bool IsPrimaryKey;
256                 public readonly string ReferName;
257                 public readonly bool IsNested;
258                 public readonly bool IsConstraintOnly;
259
260                 public ConstraintStructure (string tname, string [] cols, bool [] isAttr, string cname, bool isPK, string refName, bool isNested, bool isConstraintOnly)
261                 {
262                         TableName = tname;
263                         Columns = cols;
264                         IsAttribute = isAttr;
265                         ConstraintName = XmlHelper.Decode (cname);
266                         IsPrimaryKey = isPK;
267                         ReferName = refName;
268                         IsNested = isNested;
269                         IsConstraintOnly = isConstraintOnly;
270                 }
271         }
272
273         internal class XmlSchemaDataImporter
274         {
275                 static readonly XmlSchemaDatatype schemaIntegerType;
276                 static readonly XmlSchemaDatatype schemaDecimalType;
277                 static readonly XmlSchemaComplexType schemaAnyType;
278
279                 static XmlSchemaDataImporter ()
280                 {
281                         XmlSchema s = new XmlSchema ();
282                         XmlSchemaAttribute a = new XmlSchemaAttribute ();
283                         a.Name = "foo";
284                         a.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
285                         s.Items.Add (a);
286                         XmlSchemaAttribute b = new XmlSchemaAttribute ();
287                         b.Name = "bar";
288                         b.SchemaTypeName = new XmlQualifiedName ("decimal", XmlSchema.Namespace);
289                         s.Items.Add (b);
290                         XmlSchemaElement e = new XmlSchemaElement ();
291                         e.Name = "bar";
292                         s.Items.Add (e);
293                         s.Compile (null);
294 #if NET_2_0
295                         schemaIntegerType = ((XmlSchemaSimpleType) a.AttributeSchemaType).Datatype;
296                         schemaDecimalType = ((XmlSchemaSimpleType) b.AttributeSchemaType).Datatype;
297                         schemaAnyType = e.ElementSchemaType as XmlSchemaComplexType;
298 #else
299                         schemaIntegerType = a.AttributeType as XmlSchemaDatatype;
300                         schemaDecimalType = b.AttributeType as XmlSchemaDatatype;
301                         schemaAnyType = e.ElementType as XmlSchemaComplexType;
302 #endif
303                 }
304
305                 #region Fields
306
307                 DataSet dataset;
308                 bool forDataSet;
309                 XmlSchema schema;
310
311                 ArrayList relations = new ArrayList ();
312                 Hashtable reservedConstraints = new Hashtable ();
313
314                 // such element that has an attribute msdata:IsDataSet="true"
315                 XmlSchemaElement datasetElement;
316
317                 // choice alternatives in the "dataset element"
318                 ArrayList topLevelElements = new ArrayList ();
319
320                 // import target elements
321                 ArrayList targetElements = new ArrayList ();
322
323                 TableStructure currentTable;
324         
325 #if NET_2_0
326                 // TODO: Do we need a collection here?
327                 TableAdapterSchemaInfo currentAdapter;
328 #endif
329                 #endregion
330
331                 // .ctor()
332
333                 public XmlSchemaDataImporter (DataSet dataset, XmlReader reader, bool forDataSet)
334                 {
335                         this.dataset = dataset;
336                         this.forDataSet = forDataSet;
337                         dataset.DataSetName = "NewDataSet"; // Initialize always
338                         schema = XmlSchema.Read (reader, null);
339                         if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == "schema" && reader.NamespaceURI == XmlSchema.Namespace)
340                                 reader.ReadEndElement ();
341                         schema.Compile (null);
342                 }
343
344 #if NET_2_0
345                 // properties
346                 internal TableAdapterSchemaInfo CurrentAdapter {
347                         get { return currentAdapter; }
348                 }
349 #endif
350                 
351                 // methods
352
353                 public void Process ()
354                 {
355                         if (schema.Id != null)
356                                 dataset.DataSetName = schema.Id; // default. Overridable by "DataSet element"
357                         dataset.Namespace = schema.TargetNamespace;
358
359                         // Find dataset element
360                         foreach (XmlSchemaObject obj in schema.Items) {
361                                 XmlSchemaElement el = obj as XmlSchemaElement;
362                                 if (el != null) {
363                                         if (datasetElement == null &&
364                                                 IsDataSetElement (el))
365                                                 datasetElement = el;
366 #if NET_2_0
367                                         if (el.ElementSchemaType is XmlSchemaComplexType &&
368                                             el.ElementSchemaType != schemaAnyType)
369 #else
370                                         if (el.ElementType is XmlSchemaComplexType &&
371                                             el.ElementType != schemaAnyType)
372 #endif
373                                                 targetElements.Add (obj);
374                                 }
375                         }
376
377                         // make reservation of identity constraints
378                         if (datasetElement != null) {
379                                 // keys/uniques.
380                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
381                                         if (! (obj is XmlSchemaKeyref))
382                                                 ReserveSelfIdentity ((XmlSchemaIdentityConstraint) obj);
383                                 // keyrefs.
384                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
385                                         if (obj is XmlSchemaKeyref)
386                                                 ReserveRelationIdentity (datasetElement, (XmlSchemaKeyref) obj);
387                         }
388
389                         foreach (XmlSchemaObject obj in schema.Items) {
390                                 if (obj is XmlSchemaElement) {
391                                         XmlSchemaElement el = obj as XmlSchemaElement;
392 #if NET_2_0
393                                         if (el.ElementSchemaType is XmlSchemaComplexType &&
394                                             el.ElementSchemaType != schemaAnyType)
395 #else
396                                         if (el.ElementType is XmlSchemaComplexType &&
397                                             el.ElementType != schemaAnyType)
398 #endif
399                                                 targetElements.Add (obj);
400                                 }
401                         }
402
403                         // This collection will grow up while processing elements.
404                         int globalElementCount = targetElements.Count;
405
406                         for (int i = 0; i < globalElementCount; i++)
407                                 ProcessGlobalElement ((XmlSchemaElement) targetElements [i]);
408
409                         // Rest are local elements.
410                         for (int i = globalElementCount; i < targetElements.Count; i++)
411                                 ProcessDataTableElement ((XmlSchemaElement) targetElements [i]);
412
413                         // Handle relation definitions written as xs:annotation.
414                         // See detail: http://msdn.microsoft.com/library/shared/happyUrl/fnf_msdn.asp?Redirect=%22http://msdn.microsoft.com/404/default.asp%22
415                         foreach (XmlSchemaObject obj in schema.Items)
416                                 if (obj is XmlSchemaAnnotation)
417                                         HandleAnnotations ((XmlSchemaAnnotation) obj, false);
418
419                         if (datasetElement != null) {
420                                 // Handle constraints in the DataSet element. First keys.
421                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
422                                         if (! (obj is XmlSchemaKeyref))
423                                                 ProcessSelfIdentity (reservedConstraints [obj] as ConstraintStructure);
424                                 // Then keyrefs.
425                                 foreach (XmlSchemaObject obj in datasetElement.Constraints)
426                                         if (obj is XmlSchemaKeyref)
427                                                 ProcessRelationIdentity (datasetElement, reservedConstraints [obj] as ConstraintStructure);
428                         }
429
430                         foreach (RelationStructure rs in this.relations)
431                                 dataset.Relations.Add (GenerateRelationship (rs));
432                 }
433
434                 private bool IsDataSetElement (XmlSchemaElement el)
435                 {
436                         if (el.UnhandledAttributes != null) {
437                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
438                                         if (attr.LocalName == "IsDataSet" &&
439                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
440                                                 switch (attr.Value) {
441                                                 case "true": // case sensitive
442                                                         return true;
443                                                 case "false":
444                                                         break;
445                                                 default:
446                                                         throw new DataException (String.Format ("Value {0} is invalid for attribute 'IsDataSet'.", attr.Value));
447                                                 }
448                                         }
449                                 }
450                         }
451
452                         if (schema.Elements.Count != 1)
453                                 return false;
454                         if (!(el.SchemaType is XmlSchemaComplexType))
455                                 return false;
456                         XmlSchemaComplexType ct = (XmlSchemaComplexType) el.SchemaType;
457                         if (ct.AttributeUses.Count > 0)
458                                 return false;
459                         XmlSchemaGroupBase gb = ct.ContentTypeParticle as XmlSchemaGroupBase;
460                         if (gb == null || gb.Items.Count == 0)
461                                 return false;
462                         foreach (XmlSchemaParticle p in gb.Items) {
463                                 if (ContainsColumn (p))
464                                         return false;
465                         }
466                         return true;
467                 }
468
469                 private bool ContainsColumn (XmlSchemaParticle p)
470                 {
471                         XmlSchemaElement el = p as XmlSchemaElement;
472                         if (el != null) {
473                                 XmlSchemaComplexType ct = null;
474 #if NET_2_0
475                                 ct = el.ElementSchemaType as XmlSchemaComplexType;
476 #else
477                                 ct = el.ElementType as XmlSchemaComplexType;
478 #endif
479                                 if (ct == null || ct == schemaAnyType)
480                                         return true; // column element
481                                 if (ct.AttributeUses.Count > 0)
482                                         return false; // table element
483                                 if (ct.ContentType == XmlSchemaContentType.TextOnly)
484                                         return true; // column element
485                                 else
486                                         return false; // table element
487                         }
488                         XmlSchemaGroupBase gb = p as XmlSchemaGroupBase;
489                         for (int i = 0; i < gb.Items.Count; i++) {
490                                 if (ContainsColumn ((XmlSchemaParticle) gb.Items [i]))
491                                         return true;
492                         }
493                         return false;
494                 }
495
496                 private void ProcessGlobalElement (XmlSchemaElement el)
497                 {
498                         // If it is already registered (by resolving reference
499                         // in previously-imported elements), just ignore.
500                         if (dataset.Tables.Contains (el.QualifiedName.Name))
501                                 return;
502
503                         // If type is not complex, just skip this element
504 #if NET_2_0
505                         if (! (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType))
506 #else
507                         if (! (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType))
508 #endif
509                                 return;
510
511                         if (IsDataSetElement (el)) {
512                                 ProcessDataSetElement (el);
513                                 return;
514                         }
515                         else
516                                 dataset.Locale = CultureInfo.CurrentCulture;
517
518                         // Register as a top-level element
519                         topLevelElements.Add (el);
520                         // Create DataTable for this element
521                         ProcessDataTableElement (el);
522                 }
523
524                 private void ProcessDataSetElement (XmlSchemaElement el)
525                 {
526                         dataset.DataSetName = el.Name;
527                         this.datasetElement = el;
528
529                         // Search for locale attributes
530                         bool useCurrent = false;
531                         if (el.UnhandledAttributes != null) {
532                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
533 #if NET_2_0
534                                         if (attr.LocalName == "UseCurrentLocale" &&
535                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace)
536                                                 useCurrent = true;
537 #endif
538                                         if (attr.LocalName == "Locale" &&
539                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace) {
540                                                 CultureInfo ci = new CultureInfo (attr.Value);
541                                                 dataset.Locale = ci;
542                                         }
543                                 }
544                         }
545 #if NET_2_0
546                         if (!useCurrent && !dataset.LocaleSpecified) // then set current culture instance _explicitly_
547                                 dataset.Locale = CultureInfo.CurrentCulture;
548 #endif
549
550                         // Process content type particle (and create DataTable)
551                         XmlSchemaComplexType ct = null;
552 #if NET_2_0
553                         ct = el.ElementSchemaType as XmlSchemaComplexType;
554 #else
555                         ct = el.ElementType as XmlSchemaComplexType;
556 #endif
557                         XmlSchemaParticle p = ct != null ? ct.ContentTypeParticle : null;
558                         if (p != null)
559                                 HandleDataSetContentTypeParticle (p);
560                 }
561
562                 private void HandleDataSetContentTypeParticle (XmlSchemaParticle p)
563                 {
564                         XmlSchemaElement el = p as XmlSchemaElement;
565                         if (el != null) {
566 #if NET_2_0
567                                 if (el.ElementSchemaType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
568 #else
569                                 if (el.ElementType is XmlSchemaComplexType && el.RefName != el.QualifiedName)
570 #endif
571                                         ProcessDataTableElement (el);
572                         }
573                         else if (p is XmlSchemaGroupBase) {
574                                 foreach (XmlSchemaParticle pc in ((XmlSchemaGroupBase) p).Items)
575                                         HandleDataSetContentTypeParticle (pc);
576                         }
577                 }
578
579                 private void ProcessDataTableElement (XmlSchemaElement el)
580                 {
581                         string tableName = XmlHelper.Decode (el.QualifiedName.Name);
582                         // If it is already registered, just ignore.
583                         if (dataset.Tables.Contains (tableName))
584                                 return;
585
586                         DataTable table = new DataTable (tableName);
587                         table.Namespace = el.QualifiedName.Namespace;
588                         TableStructure oldTable = currentTable;
589                         currentTable = new TableStructure (table);
590
591                         dataset.Tables.Add (table);
592
593                         // Find Locale
594                         if (el.UnhandledAttributes != null) {
595                                 foreach (XmlAttribute attr in el.UnhandledAttributes) {
596                                         if (attr.LocalName == "Locale" &&
597                                                 attr.NamespaceURI == XmlConstants.MsdataNamespace)
598                                                 table.Locale = new CultureInfo (attr.Value);
599                                 }
600                         }
601
602                         // Handle complex type (NOTE: It is (or should be)
603                         // impossible the type is other than complex type).
604                         XmlSchemaComplexType ct = null;
605 #if NET_2_0
606                         ct = (XmlSchemaComplexType) el.ElementSchemaType;
607 #else
608                         ct = (XmlSchemaComplexType) el.ElementType;
609 #endif
610
611                         // Handle attributes
612                         foreach (DictionaryEntry de in ct.AttributeUses)
613                                 ImportColumnAttribute ((XmlSchemaAttribute) de.Value);
614
615                         // Handle content type particle
616                         if (ct.ContentTypeParticle is XmlSchemaElement)
617                                 ImportColumnElement (el, (XmlSchemaElement) ct.ContentTypeParticle);
618                         else if (ct.ContentTypeParticle is XmlSchemaGroupBase)
619                                 ImportColumnGroupBase (el, (XmlSchemaGroupBase) ct.ContentTypeParticle);
620                         // else if null then do nothing.
621
622                         // Handle simple content
623                         switch (ct.ContentType) {
624                         case XmlSchemaContentType.TextOnly:
625 //                      case XmlSchemaContentType.Mixed:
626                                 // LAMESPEC: When reading from XML Schema, it maps to "_text", while on the data inference, it is mapped to "_Text" (case ignorant).
627                                 string simpleName = el.QualifiedName.Name + "_text";
628                                 DataColumn simple = new DataColumn (simpleName);
629                                 simple.Namespace = el.QualifiedName.Namespace;
630                                 simple.AllowDBNull = (el.MinOccurs == 0);
631                                 simple.ColumnMapping = MappingType.SimpleContent;
632                                 simple.DataType = ConvertDatatype (ct.Datatype);
633                                 currentTable.NonOrdinalColumns.Add (simple);
634                                 break;
635                         }
636
637                         // add columns to the table in specified order 
638                         // (by msdata:Ordinal attributes)
639                         SortedList sd = new SortedList ();
640                         foreach (DictionaryEntry de in currentTable.OrdinalColumns)
641                                 sd.Add (de.Value, de.Key);
642                         foreach (DictionaryEntry de in sd)
643                                 table.Columns.Add ((DataColumn) de.Value);
644                         foreach (DataColumn dc in currentTable.NonOrdinalColumns)
645                                 table.Columns.Add (dc);
646
647                         currentTable = oldTable;
648                 }
649
650                 private DataRelation GenerateRelationship (RelationStructure rs)
651                 {
652                         DataTable ptab = dataset.Tables [rs.ParentTableName];
653                         DataTable ctab = dataset.Tables [rs.ChildTableName];
654
655                         DataRelation rel ;
656                         string name = rs.ExplicitName != null ? rs.ExplicitName : XmlHelper.Decode (ptab.TableName) + '_' + XmlHelper.Decode (ctab.TableName);
657
658                         // Annotation Relations belonging to a DataSet can contain multiple colnames
659                         // in parentkey and childkey.
660                         if (datasetElement != null) {
661                                 String[] pcolnames = rs.ParentColumnName.Split (null);
662                                 String[] ccolnames = rs.ChildColumnName.Split (null);
663
664                                 DataColumn[] pcol = new DataColumn [pcolnames.Length];
665                                 for (int i=0; i<pcol.Length; ++i)
666                                         pcol [i] = ptab.Columns [XmlHelper.Decode (pcolnames [i])];
667
668                                 DataColumn[] ccol = new DataColumn [ccolnames.Length];
669                                 for (int i=0; i < ccol.Length; ++i) {
670                                         ccol [i] = ctab.Columns [XmlHelper.Decode (ccolnames [i])];
671                                         if (ccol [i] == null)
672                                                 ccol [i] = CreateChildColumn (pcol [i], ctab);
673                                 }
674                                 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
675                         } else {
676                                 DataColumn pcol = ptab.Columns [XmlHelper.Decode (rs.ParentColumnName)];
677                                 DataColumn ccol = ctab.Columns [XmlHelper.Decode (rs.ChildColumnName)];
678                                 if (ccol == null) 
679                                         ccol = CreateChildColumn (pcol, ctab);
680                                 rel = new DataRelation (name, pcol, ccol, rs.CreateConstraint);
681                         }
682                         rel.Nested = rs.IsNested;
683                         if (rs.CreateConstraint)
684                                 rel.ParentTable.PrimaryKey = rel.ParentColumns;
685                         return rel;
686                 }
687
688                 private DataColumn CreateChildColumn (DataColumn parentColumn, DataTable childTable)
689                 {
690                         DataColumn col = childTable.Columns.Add (parentColumn.ColumnName, 
691                                                                 parentColumn.DataType);
692                         col.Namespace = String.Empty;
693                         col.ColumnMapping = MappingType.Hidden;
694                         return col;
695                 }
696
697                 private void ImportColumnGroupBase (XmlSchemaElement parent, XmlSchemaGroupBase gb)
698                 {
699                         foreach (XmlSchemaParticle p in gb.Items) {
700                                 XmlSchemaElement el = p as XmlSchemaElement;
701                                 if (el != null)
702                                         ImportColumnElement (parent, el);
703                                 else if (p is XmlSchemaGroupBase)
704                                         ImportColumnGroupBase (parent, (XmlSchemaGroupBase) p);
705                                 // otherwise p is xs:any
706                         }
707                 }
708
709                 private XmlSchemaDatatype GetSchemaPrimitiveType (object type)
710                 {
711                         if (type is XmlSchemaComplexType)
712                                 return null; // It came here, so that maybe it is xs:anyType
713                         XmlSchemaDatatype dt = type as XmlSchemaDatatype;
714                         if (dt == null && type != null)
715                                 dt = ((XmlSchemaSimpleType) type).Datatype;
716                         return dt;
717                 }
718
719                 // Note that this column might be Hidden
720                 private void ImportColumnAttribute (XmlSchemaAttribute attr)
721                 {
722                         DataColumn col = new DataColumn ();
723                         col.ColumnName = attr.QualifiedName.Name;
724                         col.Namespace = attr.QualifiedName.Namespace;
725                         XmlSchemaDatatype dt = null;
726 #if NET_2_0
727                         dt = GetSchemaPrimitiveType (((XmlSchemaSimpleType) attr.AttributeSchemaType).Datatype);
728 #else
729                         dt = GetSchemaPrimitiveType (attr.AttributeType);
730 #endif
731                         // This complicated check comes from the fact that
732                         // MS.NET fails to map System.Object to anyType (that
733                         // will cause ReadTypedObject() fail on XmlValidatingReader).
734                         // ONLY In DataSet context, we set System.String for
735                         // simple ur-type.
736                         col.DataType = ConvertDatatype (dt);
737                         if (col.DataType == typeof (object))
738                                 col.DataType = typeof (string);
739                         // When attribute use="prohibited", then it is regarded as 
740                         // Hidden column.
741                         if (attr.Use == XmlSchemaUse.Prohibited)
742                                 col.ColumnMapping = MappingType.Hidden;
743                         else {
744                                 col.ColumnMapping = MappingType.Attribute;
745                                 col.DefaultValue = GetAttributeDefaultValue (attr);
746                         }
747                         if (attr.Use == XmlSchemaUse.Required)
748                                 col.AllowDBNull = false;
749
750 #if NET_2_0
751                         FillFacet (col, attr.AttributeSchemaType as XmlSchemaSimpleType);
752 #else
753                         FillFacet (col, attr.AttributeType as XmlSchemaSimpleType);
754 #endif
755
756                         // Call this method after filling the name
757                         ImportColumnMetaInfo (attr, attr.QualifiedName, col);
758                         AddColumn (col);
759                 }
760
761                 private void ImportColumnElement (XmlSchemaElement parent, XmlSchemaElement el)
762                 {
763                         // FIXME: element nest check
764
765                         DataColumn col = new DataColumn ();
766                         col.DefaultValue = GetElementDefaultValue (el);
767                         col.AllowDBNull = (el.MinOccurs == 0);
768
769 #if NET_2_0
770                         if (el.ElementSchemaType is XmlSchemaComplexType && el.ElementSchemaType != schemaAnyType)
771 #else
772                         if (el.ElementType is XmlSchemaComplexType && el.ElementType != schemaAnyType)
773 #endif
774                                 FillDataColumnComplexElement (parent, el, col);
775                         else if (el.MaxOccurs != 1)
776                                 FillDataColumnRepeatedSimpleElement (parent, el, col);
777                         else
778                                 FillDataColumnSimpleElement (el, col);
779                 }
780
781                 // common process for element and attribute
782                 private void ImportColumnMetaInfo (XmlSchemaAnnotated obj, XmlQualifiedName name, DataColumn col)
783                 {
784                         if (obj.UnhandledAttributes != null) {
785                                 foreach (XmlAttribute attr in obj.UnhandledAttributes) {
786                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
787                                                 continue;
788                                         switch (attr.LocalName) {
789                                         case XmlConstants.Caption:
790                                                 col.Caption = attr.Value;
791                                                 break;
792                                         case XmlConstants.DataType:
793                                                 col.DataType = Type.GetType (attr.Value);
794                                                 break;
795                                         case XmlConstants.AutoIncrement:
796                                                 col.AutoIncrement = bool.Parse (attr.Value);
797                                                 break;
798                                         case XmlConstants.AutoIncrementSeed:
799                                                 col.AutoIncrementSeed = int.Parse (attr.Value);
800                                                 break;
801                                         case XmlConstants.AutoIncrementStep:
802                                                 col.AutoIncrementStep = int.Parse (attr.Value);
803                                                 break;
804                                         case XmlConstants.ReadOnly:
805                                                 col.ReadOnly = XmlConvert.ToBoolean (attr.Value);
806                                                 break;
807                                         case XmlConstants.Ordinal:
808                                                 int ordinal = int.Parse (attr.Value);
809                                                 break;
810                                         }
811                                 }
812                         }
813                 }
814
815                 private void FillDataColumnComplexElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
816                 {
817                         if (targetElements.Contains (el))
818                                 return; // do nothing
819
820                         string elName = XmlHelper.Decode (el.QualifiedName.Name);
821                         if (elName == dataset.DataSetName)
822                                 // Well, why it is ArgumentException :-?
823                                 throw new ArgumentException ("Nested element must not have the same name as DataSet's name.");
824
825                         if (el.Annotation != null)
826                                 HandleAnnotations (el.Annotation, true);
827                         // If xsd:keyref xsd:key for this table exists, then don't add
828                         // relation here manually.
829                         else if (!DataSetDefinesKey (elName)) {
830                                 AddParentKeyColumn (parent, el, col);
831
832                                 RelationStructure rel = new RelationStructure ();
833                                 rel.ParentTableName = XmlHelper.Decode (parent.QualifiedName.Name);
834                                 rel.ChildTableName = elName;
835                                 rel.ParentColumnName = col.ColumnName;
836                                 rel.ChildColumnName = col.ColumnName;
837                                 rel.CreateConstraint = true;
838                                 rel.IsNested = true;
839                                 relations.Add (rel);
840                         }
841
842                         // If the element is not referenced one, the element will be handled later.
843                         if (el.RefName == XmlQualifiedName.Empty)
844                                 ProcessDataTableElement (el);
845
846                 }
847
848                 private bool DataSetDefinesKey (string name)
849                 {
850                         foreach (ConstraintStructure c in reservedConstraints.Values)
851                                 if (c.TableName == name && (c.IsPrimaryKey || c.IsNested))
852                                         return true;
853                         return false;
854                 }
855
856                 private void AddParentKeyColumn (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
857                 {
858                         if (currentTable.PrimaryKey != null)
859                                 return;
860
861                         // check name identity
862                         string name = XmlHelper.Decode (parent.QualifiedName.Name) + "_Id";
863                         int count = 0;
864                         while (currentTable.ContainsColumn (name))
865                                 name = String.Format ("{0}_{1}", name, count++);
866                         // check existing primary key
867                         if (currentTable.Table.PrimaryKey.Length > 0)
868                                 throw new DataException (String.Format ("There is already primary key columns in the table \"{0}\".", currentTable.Table.TableName));
869
870                         col.ColumnName = name;
871                         col.ColumnMapping = MappingType.Hidden;
872                         col.Namespace = parent.QualifiedName.Namespace;
873                         col.DataType = typeof (int);
874                         col.AutoIncrement = true;
875                         col.AllowDBNull = false;
876
877                         ImportColumnMetaInfo (el, el.QualifiedName, col);
878                         AddColumn (col);
879                         currentTable.PrimaryKey = col;
880                 }
881
882                 private void FillDataColumnRepeatedSimpleElement (XmlSchemaElement parent, XmlSchemaElement el, DataColumn col)
883                 {
884                         if (targetElements.Contains (el))
885                                 return; // do nothing
886
887                         AddParentKeyColumn (parent, el, col);
888                         DataColumn pkey = currentTable.PrimaryKey;
889
890                         string elName = XmlHelper.Decode (el.QualifiedName.Name);
891                         string parentName = XmlHelper.Decode (parent.QualifiedName.Name);
892
893                         DataTable dt = new DataTable ();
894                         dt.TableName = elName;
895                         dt.Namespace = el.QualifiedName.Namespace;
896                         // reference key column to parent
897                         DataColumn cc = new DataColumn ();
898                         cc.ColumnName = parentName + "_Id";
899                         cc.Namespace = parent.QualifiedName.Namespace;
900                         cc.ColumnMapping = MappingType.Hidden;
901                         cc.DataType = typeof (int);
902
903                         // repeatable content simple element
904                         DataColumn cc2 = new DataColumn ();
905                         cc2.ColumnName = elName + "_Column";
906                         cc2.Namespace = el.QualifiedName.Namespace;
907                         cc2.ColumnMapping = MappingType.SimpleContent;
908                         cc2.AllowDBNull = false;
909 #if NET_2_0
910                         cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
911 #else
912                         cc2.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
913 #endif
914
915                         dt.Columns.Add (cc2);
916                         dt.Columns.Add (cc);
917                         dataset.Tables.Add (dt);
918
919                         RelationStructure rel = new RelationStructure ();
920                         rel.ParentTableName = parentName;
921                         rel.ChildTableName = dt.TableName;
922                         rel.ParentColumnName = pkey.ColumnName;
923                         rel.ChildColumnName = cc.ColumnName;
924                         rel.IsNested = true;
925                         rel.CreateConstraint = true;
926                         relations.Add (rel);
927                 }
928
929                 private void FillDataColumnSimpleElement (XmlSchemaElement el, DataColumn col)
930                 {
931                         col.ColumnName = XmlHelper.Decode (el.QualifiedName.Name);
932                         col.Namespace = el.QualifiedName.Namespace;
933                         col.ColumnMapping = MappingType.Element;
934 #if NET_2_0
935                         col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementSchemaType));
936                         FillFacet (col, el.ElementSchemaType as XmlSchemaSimpleType);
937 #else
938                         col.DataType = ConvertDatatype (GetSchemaPrimitiveType (el.ElementType));
939                         FillFacet (col, el.ElementType as XmlSchemaSimpleType);
940 #endif
941
942                         ImportColumnMetaInfo (el, el.QualifiedName, col);
943
944                         AddColumn (col);
945                 }
946
947                 private void AddColumn (DataColumn col)
948                 {
949                         if (col.Ordinal < 0)
950                                 currentTable.NonOrdinalColumns.Add (col);
951                         else
952                                 currentTable.OrdinalColumns.Add (col, col.Ordinal);
953                 }
954
955                 private void FillFacet (DataColumn col, XmlSchemaSimpleType st)
956                 {
957                         if (st == null || st.Content == null)
958                                 return;
959
960                         // Handle restriction facets
961
962                         XmlSchemaSimpleTypeRestriction restriction = st == null ? null : st.Content as XmlSchemaSimpleTypeRestriction;
963                         if (restriction == null)
964                                 throw new DataException ("DataSet does not suport 'list' nor 'union' simple type.");
965
966                         foreach (XmlSchemaFacet f in restriction.Facets) {
967                                 if (f is XmlSchemaMaxLengthFacet)
968                                         // There is no reason why MaxLength is limited to int, except for the fact that DataColumn.MaxLength property is int.
969                                         col.MaxLength = int.Parse (f.Value);
970                         }
971                 }
972
973                 private Type ConvertDatatype (XmlSchemaDatatype dt)
974                 {
975                         if (dt == null)
976                                 return typeof (string);
977                         else if (dt.ValueType == typeof (decimal)) {
978                                 // LAMESPEC: MSDN documentation says it is based 
979                                 // on ValueType. However, in the System.Xml.Schema
980                                 // context, xs:integer is mapped to Decimal, while
981                                 // in DataSet context it is mapped to Int64.
982                                 if (dt == schemaDecimalType)
983                                         return typeof (decimal);
984                                 else if (dt == schemaIntegerType)
985                                         return typeof (long);
986                                 else
987                                         return typeof (ulong);
988                         }
989                         else
990                                 return dt.ValueType;
991                 }
992
993                 // This method cuts out the local name of the last step from XPath.
994                 // It is nothing more than hack. However, MS looks to do similar.
995                 private string GetSelectorTarget (string xpath)
996                 {
997                         string tableName = xpath;
998                         int index = tableName.LastIndexOf ('/');
999                         // '>' is enough. If XPath [0] = '/', it is invalid. 
1000                         // Selector can specify only element axes.
1001                         if (index > 0)
1002                                 tableName = tableName.Substring (index + 1);
1003
1004                         // Round QName to NSName
1005                         index = tableName.LastIndexOf (':');
1006                         if (index > 0)
1007                                 tableName = tableName.Substring (index + 1);
1008
1009                         return XmlHelper.Decode (tableName);
1010                 }
1011
1012                 private void ReserveSelfIdentity (XmlSchemaIdentityConstraint ic)
1013                 {
1014                         string tableName = GetSelectorTarget (ic.Selector.XPath);
1015
1016                         string [] cols = new string [ic.Fields.Count];
1017                         bool [] isAttrSpec = new bool [cols.Length];
1018
1019                         int i = 0;
1020                         foreach (XmlSchemaXPath Field in ic.Fields) {
1021                                 string colName = Field.XPath;
1022                                 bool isAttr = colName.Length > 0 && colName [0] == '@';
1023                                 int index = colName.LastIndexOf (':');
1024                                 if (index > 0)
1025                                         colName = colName.Substring (index + 1);
1026                                 else if (isAttr)
1027                                         colName = colName.Substring (1);
1028
1029                                 colName = XmlHelper.Decode (colName);
1030                                 cols [i] = colName;
1031                                 isAttrSpec [i] = isAttr;
1032                                 i++;
1033                         }
1034                         
1035                         bool isPK = false;
1036                         // find if there is an attribute with the constraint name
1037                         // if not use the XmlSchemaConstraint's name.
1038                         string constraintName = ic.Name;
1039                         if (ic.UnhandledAttributes != null) {
1040                                 foreach (XmlAttribute attr in ic.UnhandledAttributes) {
1041                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1042                                                 continue;
1043                                         switch (attr.LocalName) {
1044                                         case XmlConstants.ConstraintName:
1045                                                 constraintName = attr.Value;
1046                                                 break;
1047                                         case XmlConstants.PrimaryKey:
1048                                                 isPK = bool.Parse(attr.Value);
1049                                                 break;
1050                                         }
1051                                 }
1052                         }
1053                         reservedConstraints.Add (ic,
1054                                 new ConstraintStructure (tableName, cols,
1055                                         isAttrSpec, constraintName, isPK, null, false, false));
1056                 }
1057
1058                 private void ProcessSelfIdentity (ConstraintStructure c)
1059                 {
1060                         // Basic concept came from XmlSchemaMapper.cs
1061
1062                         string tableName = c.TableName;
1063                         
1064                         DataTable dt = dataset.Tables [tableName];
1065                         if (dt == null) {
1066                                 if (forDataSet)
1067                                         throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1068                                 else
1069                                         // nonexistent table name. .NET ignores it for DataTable.ReadXmlSchema().
1070                                         return;
1071                         }
1072
1073                         DataColumn [] cols = new DataColumn [c.Columns.Length];
1074                         for (int i = 0; i < cols.Length; i++) {
1075                                 string colName = c.Columns [i];
1076                                 bool isAttr = c.IsAttribute [i];
1077                                 DataColumn col = dt.Columns [colName];
1078                                 if (col == null)
1079                                         throw new DataException (String.Format ("Invalid XPath selection inside field. Cannot find: {0}", tableName));
1080                                 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1081                                         throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1082                                 if (!isAttr && col.ColumnMapping != MappingType.Element)
1083                                         throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1084
1085                                 cols [i] = dt.Columns [colName];
1086                         }
1087                         
1088                         bool isPK = c.IsPrimaryKey;
1089                         string constraintName = c.ConstraintName;
1090                         dt.Constraints.Add (new UniqueConstraint (
1091                                 constraintName, cols, isPK));
1092                 }
1093
1094                 private void ReserveRelationIdentity (XmlSchemaElement element, XmlSchemaKeyref keyref)
1095                 {
1096                         // Basic concept came from XmlSchemaMapper.cs
1097
1098                         string tableName = GetSelectorTarget (keyref.Selector.XPath);
1099
1100                         string [] cols = new string [keyref.Fields.Count];
1101                         bool [] isAttrSpec = new bool [cols.Length];
1102                         int i = 0;
1103                         foreach (XmlSchemaXPath Field in keyref.Fields) {
1104                                 string colName = Field.XPath;
1105                                 bool isAttr = colName.Length > 0 && colName [0] == '@';
1106                                 int index = colName.LastIndexOf (':');
1107                                 if (index > 0)
1108                                         colName = colName.Substring (index + 1);
1109                                 else if (isAttr)
1110                                         colName = colName.Substring (1);
1111
1112                                 colName = XmlHelper.Decode (colName);
1113                                 cols [i] = colName;
1114                                 isAttrSpec [i] = isAttr;
1115                                 i++;
1116                         }
1117                         string constraintName = keyref.Name;
1118                         bool isNested = false;
1119                         bool isConstraintOnly = false;
1120                         if (keyref.UnhandledAttributes != null) {
1121                                 foreach (XmlAttribute attr in keyref.UnhandledAttributes) {
1122                                         if (attr.NamespaceURI != XmlConstants.MsdataNamespace)
1123                                                 continue;
1124                                         switch (attr.LocalName) {
1125                                         case XmlConstants.ConstraintName:
1126                                                 constraintName = attr.Value;
1127                                                 break;
1128                                         case XmlConstants.IsNested:
1129                                                 if (attr.Value == "true")
1130                                                         isNested = true;
1131                                                 break;
1132                                         case XmlConstants.ConstraintOnly:
1133                                                 if (attr.Value == "true")
1134                                                         isConstraintOnly = true;
1135                                                 break;
1136                                         }
1137                                 }
1138                         }
1139
1140                         reservedConstraints.Add (keyref, new ConstraintStructure (
1141                                 tableName, cols, isAttrSpec, constraintName,
1142                                 false, keyref.Refer.Name, isNested, isConstraintOnly));
1143                 }
1144
1145                 private void ProcessRelationIdentity (XmlSchemaElement element, ConstraintStructure c)
1146                 {
1147                         // Basic concept came from XmlSchemaMapper.cs
1148
1149                         string tableName = c.TableName;
1150
1151                         DataColumn [] cols;
1152                         DataTable dt = dataset.Tables [tableName];
1153                         if (dt == null)
1154                                 throw new DataException (String.Format ("Invalid XPath selection inside selector. Cannot find: {0}", tableName));
1155
1156                         cols = new DataColumn [c.Columns.Length];
1157                         for (int i = 0; i < cols.Length; i++) {
1158                                 string colName = c.Columns [i];
1159                                 bool isAttr = c.IsAttribute [i];
1160                                 DataColumn col = dt.Columns [colName];
1161                                 if (isAttr && col.ColumnMapping != MappingType.Attribute)
1162                                         throw new DataException ("The XPath specified attribute field, but mapping type is not attribute.");
1163                                 if (!isAttr && col.ColumnMapping != MappingType.Element)
1164                                         throw new DataException ("The XPath specified simple element field, but mapping type is not simple element.");
1165                                 cols [i] = col;
1166                         }
1167                         string name = c.ReferName;
1168                         // get the unique constraint for the releation
1169                         UniqueConstraint uniq = FindConstraint (name, element);
1170                         // generate the FK.
1171                         ForeignKeyConstraint fkc = new ForeignKeyConstraint(c.ConstraintName, uniq.Columns, cols);
1172                         dt.Constraints.Add (fkc);
1173
1174                         if (!c.IsConstraintOnly) {
1175                                 // generate the relation.
1176                                 DataRelation rel = new DataRelation (c.ConstraintName, uniq.Columns, cols, true);
1177                                 rel.Nested = c.IsNested;
1178                                 rel.SetParentKeyConstraint (uniq);
1179                                 rel.SetChildKeyConstraint (fkc);
1180
1181                                 dataset.Relations.Add (rel);
1182                         }
1183                 }
1184
1185                 // get the unique constraint for the relation.
1186                 // name - the name of the XmlSchemaUnique element
1187                 private UniqueConstraint FindConstraint (string name, XmlSchemaElement element)
1188                 {
1189                         // Copied from XmlSchemaMapper.cs
1190
1191                         // find the element in the constraint collection.
1192                         foreach (XmlSchemaIdentityConstraint c in element.Constraints) {
1193                                 if (c is XmlSchemaKeyref)
1194                                         continue;
1195
1196                                 if (c.Name == name) {
1197                                         string tableName = GetSelectorTarget (c.Selector.XPath);
1198
1199                                         // find the table in the dataset.
1200                                         DataTable dt = dataset.Tables [tableName];
1201
1202                                         string constraintName = c.Name;
1203                                         // find if there is an attribute with the constraint name
1204                                         // if not use the XmlSchemaUnique name.
1205                                         if (c.UnhandledAttributes != null)
1206                                                 foreach (XmlAttribute attr in c.UnhandledAttributes)
1207                                                         if (attr.LocalName == "ConstraintName" && attr.NamespaceURI == XmlConstants.MsdataNamespace)
1208                                                                 constraintName = attr.Value;
1209                                         return (UniqueConstraint) dt.Constraints [constraintName];
1210                                 }
1211                         }
1212                         throw new DataException ("Target identity constraint was not found: " + name);
1213                 }
1214
1215                 private void HandleAnnotations (XmlSchemaAnnotation an, bool nested)
1216                 {
1217                         foreach (XmlSchemaObject content in an.Items) {
1218                                 XmlSchemaAppInfo ai = content as XmlSchemaAppInfo;
1219                                 if (ai != null) {
1220                                         foreach (XmlNode n in ai.Markup) {
1221                                                 XmlElement el = n as XmlElement;
1222                                                 
1223                                                 // #325464 debugging
1224                                                 //Console.WriteLine ("Name: " + el.LocalName + " NS: " + el.NamespaceURI + " Const: " + XmlConstants.MsdataNamespace);
1225                                                 if (el != null && el.LocalName == "Relationship" && el.NamespaceURI == XmlConstants.MsdataNamespace)
1226                                                         HandleRelationshipAnnotation (el, nested);
1227 #if NET_2_0
1228                                                 if (el != null && el.LocalName == "DataSource" && el.NamespaceURI == XmlConstants.MsdatasourceNamespace)
1229                                                         HandleDataSourceAnnotation (el, nested);
1230 #endif
1231                                         }
1232                                 }
1233                         }
1234                 }
1235
1236 #if NET_2_0
1237                 private void HandleDataSourceAnnotation (XmlElement el, bool nested)
1238                 {
1239                         // Handle: Connections and Tables
1240                         // For Tables: extract the provider information from connection and use
1241                         // the corresponding providerfactory to create the adapter and et al objects 
1242                         // and populate them
1243                         
1244                         // #325464 debugging
1245                         //Console.WriteLine ("In HandleDataSourceAnnotation... ");
1246                         string providerName = null;
1247                         string connString = null;
1248                         DbProviderFactory provider = null;
1249                         XmlElement e, tablesElement = null;
1250                         
1251                         foreach (XmlNode n in el.ChildNodes) {
1252                                 e = n as XmlElement;
1253                                 
1254                                 if (e == null)
1255                                         continue;
1256                                 
1257 #if !MONOTOUCH
1258                                 if (e.LocalName == "Connections") {
1259                                         providerName = ((XmlElement)e.FirstChild).GetAttribute ("Provider");
1260                                         connString = ((XmlElement)e.FirstChild).GetAttribute ("AppSettingsPropertyName");
1261                                         provider = DbProviderFactories.GetFactory (providerName);
1262                                         continue;
1263                                 }
1264 #endif // !MONOTOUCH
1265                                 // #325464 debugging
1266                                 //Console.WriteLine ("ProviderName: " + providerName + "Connstr: " + connString);
1267                                 
1268                                 if (e.LocalName == "Tables")
1269                                         tablesElement = e;
1270                         }
1271                                 
1272                         if (tablesElement != null && provider != null) {
1273                                 foreach (XmlNode node in tablesElement.ChildNodes) {
1274                                         ProcessTableAdapter (node as XmlElement, provider, connString);
1275                                 }
1276                         }
1277                 }
1278                 
1279                 private void ProcessTableAdapter (XmlElement el, DbProviderFactory provider, string connStr)
1280                 {
1281                         XmlElement e;
1282                         string datasetTableName = null;
1283                         
1284                         if (el == null)
1285                                 return;
1286                         
1287                         // #325464 debugging
1288                         //Console.WriteLine ("in ProcessTableAdapters...");
1289                         currentAdapter = new TableAdapterSchemaInfo (provider); 
1290                         currentAdapter.ConnectionString = connStr;
1291                         
1292                         //Console.WriteLine ("Provider: {0}, connection: {1}, adapter: {2}", 
1293                         //                   provider, currentAdapter.Connection, currentAdapter.Adapter);
1294                         currentAdapter.BaseClass = el.GetAttribute ("BaseClass");
1295                         datasetTableName = el.GetAttribute ("Name");
1296                         currentAdapter.Name = el.GetAttribute ("GeneratorDataComponentClassName");
1297                         
1298                         if (String.IsNullOrEmpty (currentAdapter.Name))
1299                                 currentAdapter.Name = el.GetAttribute ("DataAccessorName");
1300
1301                         //Console.WriteLine ("Name: "+currentAdapter.Name);
1302                         foreach (XmlNode n in el.ChildNodes) {
1303                                 e = n as XmlElement;
1304                                 
1305                                 //Console.WriteLine ("Children of Tables: "+e.LocalName);
1306                                 if (e == null)
1307                                         continue;
1308                                 
1309                                 switch (e.LocalName) {
1310                                         case "MainSource": 
1311                                         case "Sources": 
1312                                                 foreach (XmlNode msn in e.ChildNodes)
1313                                                         ProcessDbSource (msn as XmlElement);
1314                                                 break;
1315                                         
1316                                         case "Mappings":
1317                                                 DataTableMapping tableMapping = new DataTableMapping ();
1318                                                 tableMapping.SourceTable = "Table";
1319                                                 tableMapping.DataSetTable = datasetTableName;
1320                                                 
1321                                                 foreach (XmlNode mps in e.ChildNodes)
1322                                                         ProcessColumnMapping (mps as XmlElement, tableMapping);
1323                                                 
1324                                                 currentAdapter.Adapter.TableMappings.Add (tableMapping);
1325                                                 break;                                          
1326                                 }
1327                         }
1328                 }
1329                 
1330                 private void ProcessDbSource (XmlElement el)
1331                 {
1332                         
1333                         string cmdType;
1334                         string tmp = null;
1335                         XmlElement e;
1336                         
1337                         if (el == null)
1338                                 return;
1339                         
1340                         //Console.WriteLine ("ProcessDbSources: "+el.LocalName);
1341
1342                         tmp = el.GetAttribute ("GenerateShortCommands");
1343                         //Console.WriteLine ("GenerateShortCommands: {0}", tmp);
1344                         if (!String.IsNullOrEmpty (tmp))
1345                                 currentAdapter.ShortCommands = Convert.ToBoolean (tmp);
1346                 
1347                         DbCommandInfo cmdInfo = new DbCommandInfo ();
1348                         tmp = el.GetAttribute ("GenerateMethods");
1349                         if (!String.IsNullOrEmpty (tmp)) {
1350                                 DbSourceMethodInfo mthdInfo = null;
1351                                 
1352                                 switch ((GenerateMethodsType) Enum.Parse (typeof (GenerateMethodsType), tmp)) {
1353                                 case GenerateMethodsType.Get:
1354                                         mthdInfo = new DbSourceMethodInfo ();
1355                                         mthdInfo.Name = el.GetAttribute ("GetMethodName");
1356                                         mthdInfo.Modifier = el.GetAttribute ("GetMethodModifier");
1357                                         if (String.IsNullOrEmpty (mthdInfo.Modifier))
1358                                                 mthdInfo.Modifier = "Public";
1359                                         mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1360                                         mthdInfo.QueryType = el.GetAttribute ("QueryType");
1361                                         mthdInfo.MethodType = GenerateMethodsType.Get;
1362                                         cmdInfo.Methods = new DbSourceMethodInfo [1];
1363                                         cmdInfo.Methods[0] = mthdInfo;
1364                                         break;
1365                                         
1366                                 case GenerateMethodsType.Fill:
1367                                         mthdInfo = new DbSourceMethodInfo ();
1368                                         mthdInfo.Name = el.GetAttribute ("FillMethodName");
1369                                         mthdInfo.Modifier = el.GetAttribute ("FillMethodModifier");
1370                                         if (String.IsNullOrEmpty (mthdInfo.Modifier))
1371                                                 mthdInfo.Modifier = "Public";
1372                                         mthdInfo.ScalarCallRetval = null;
1373                                         mthdInfo.QueryType = null;
1374                                         mthdInfo.MethodType = GenerateMethodsType.Fill;
1375                                         cmdInfo.Methods = new DbSourceMethodInfo [1];
1376                                         cmdInfo.Methods[0] = mthdInfo;
1377                                         break;
1378                                         
1379                                 case GenerateMethodsType.Both:
1380                                         mthdInfo = new DbSourceMethodInfo ();
1381                                         // Get
1382                                         mthdInfo.Name = el.GetAttribute ("GetMethodName");
1383                                         mthdInfo.Modifier = el.GetAttribute ("GetMethodModifier");
1384                                         if (String.IsNullOrEmpty (mthdInfo.Modifier))
1385                                                 mthdInfo.Modifier = "Public";
1386                                         mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1387                                         mthdInfo.QueryType = el.GetAttribute ("QueryType");
1388                                         mthdInfo.MethodType = GenerateMethodsType.Get;
1389                                         cmdInfo.Methods = new DbSourceMethodInfo [2];
1390                                         cmdInfo.Methods[0] = mthdInfo;
1391                                         
1392                                         // Fill
1393                                         mthdInfo = new DbSourceMethodInfo ();
1394                                         mthdInfo.Name = el.GetAttribute ("FillMethodName");
1395                                         mthdInfo.Modifier = el.GetAttribute ("FillMethodModifier");
1396                                         if (String.IsNullOrEmpty (mthdInfo.Modifier))
1397                                                 mthdInfo.Modifier = "Public";
1398                                         mthdInfo.ScalarCallRetval = null;
1399                                         mthdInfo.QueryType = null;
1400                                         mthdInfo.MethodType = GenerateMethodsType.Fill;
1401                                         cmdInfo.Methods[1] = mthdInfo;
1402                                         break;
1403                                 }
1404                         } else {
1405                                 // no Get or Fill methods - non <MainSource> sources
1406                                 DbSourceMethodInfo mthdInfo = new DbSourceMethodInfo ();
1407                                 mthdInfo.Name = el.GetAttribute ("Name");
1408                                 mthdInfo.Modifier = el.GetAttribute ("Modifier");
1409                                 if (String.IsNullOrEmpty (mthdInfo.Modifier))
1410                                         mthdInfo.Modifier = "Public";
1411                                 mthdInfo.ScalarCallRetval = el.GetAttribute ("ScalarCallRetval");
1412                                 mthdInfo.QueryType = el.GetAttribute ("QueryType");
1413                                 mthdInfo.MethodType = GenerateMethodsType.None;
1414                                 // Add MethodInfo to DbCommandInfo
1415                                 cmdInfo.Methods = new DbSourceMethodInfo [1];
1416                                 cmdInfo.Methods[0] = mthdInfo;
1417                         }
1418                         
1419                         foreach (XmlNode n in el.ChildNodes) {
1420                                 e = n as XmlElement;
1421                                 
1422                                 if (e == null) 
1423                                         continue;
1424                                 
1425                                 switch (e.LocalName) {
1426                                         case "SelectCommand": 
1427                                                 cmdInfo.Command = ProcessDbCommand (e.FirstChild as XmlElement);
1428                                                 currentAdapter.Commands.Add (cmdInfo);
1429                                                 break;
1430                                         case "InsertCommand": 
1431                                                 currentAdapter.Adapter.InsertCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1432                                                 break;
1433                                         case "UpdateCommand": 
1434                                                 currentAdapter.Adapter.UpdateCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1435                                                 break;
1436                                         case "DeleteCommand": 
1437                                                 currentAdapter.Adapter.DeleteCommand = ProcessDbCommand (e.FirstChild as XmlElement);
1438                                                 break;
1439                                 }
1440                         }
1441                 }
1442                 
1443                 private DbCommand ProcessDbCommand (XmlElement el)
1444                 {
1445                         XmlElement e;
1446                         //Console.WriteLine (el.LocalName);
1447                         string cmdText = null;
1448                         string cmdType = null;
1449                         ArrayList parameters = null;
1450                         
1451                         if (el == null)
1452                                 return null;
1453                         
1454                         cmdType = el.GetAttribute ("CommandType");
1455                         foreach (XmlNode n in el.ChildNodes) {
1456                                 e = n as XmlElement;
1457                                 if (e != null && e.LocalName == "CommandText")
1458                                         cmdText = e.InnerText;
1459                                 else if (e != null && e.LocalName == "Parameters" && !e.IsEmpty)
1460                                         parameters = ProcessDbParameters (e);
1461                         }
1462                         
1463                         DbCommand cmd = currentAdapter.Provider.CreateCommand ();
1464                         cmd.CommandText = cmdText;
1465                         if (cmdType == "StoredProcedure")
1466                                 cmd.CommandType = CommandType.StoredProcedure;
1467                         else
1468                                 cmd.CommandType = CommandType.Text;
1469
1470                         if (parameters != null)
1471                                 cmd.Parameters.AddRange (parameters.ToArray ());
1472                         
1473                         //Console.WriteLine ("Parameters count: {0}", cmd.Parameters.Count);
1474                         return cmd;
1475                 }
1476                 
1477                 private ArrayList ProcessDbParameters (XmlElement el)
1478                 {
1479                         //Console.WriteLine ("ProcessDbParameters: "+el.LocalName);
1480                         string tmp = null;
1481                         XmlElement e;
1482                         DbParameter param = null;
1483                         ArrayList parameters = new ArrayList ();
1484                         
1485                         if (el == null)
1486                                 return parameters;
1487                         
1488                         foreach (XmlNode n in el.ChildNodes) {
1489                                 e = n as XmlElement;
1490                                 
1491                                 if (e == null)
1492                                         continue;
1493                                 param = currentAdapter.Provider.CreateParameter ();
1494
1495                                 tmp = e.GetAttribute ("AllowDbNull");
1496                                 if (!String.IsNullOrEmpty (tmp))
1497                                         param.IsNullable = Convert.ToBoolean (tmp);
1498                                 
1499                                 param.ParameterName = e.GetAttribute ("ParameterName");
1500                                 tmp = e.GetAttribute ("ProviderType");
1501                                 if (!String.IsNullOrEmpty (tmp))
1502                                         tmp = e.GetAttribute ("DbType");
1503                                 param.FrameworkDbType = tmp;
1504                                 
1505                                 tmp = e.GetAttribute ("Direction");
1506                                 param.Direction = (ParameterDirection) Enum.Parse (typeof (ParameterDirection), tmp);
1507                                 
1508                                 ((IDbDataParameter)param).Precision = Convert.ToByte (e.GetAttribute ("Precision"));
1509                                 ((IDbDataParameter)param).Scale = Convert.ToByte (e.GetAttribute ("Scale"));
1510                                 param.Size = Convert.ToInt32 (e.GetAttribute ("Size"));
1511                                 param.SourceColumn = e.GetAttribute ("SourceColumn");
1512                                 
1513                                 tmp = e.GetAttribute ("SourceColumnNullMapping");
1514                                 if (!String.IsNullOrEmpty (tmp))
1515                                         param.SourceColumnNullMapping = Convert.ToBoolean (tmp);
1516                                 
1517                                 tmp = e.GetAttribute ("SourceVersion");
1518                                 param.SourceVersion = (DataRowVersion) Enum.Parse (typeof (DataRowVersion), tmp);                               
1519                                 parameters.Add (param);
1520                         }
1521                         
1522                         return parameters;
1523                 }
1524
1525                 private void ProcessColumnMapping (XmlElement el, DataTableMapping tableMapping)
1526                 {
1527                         if (el == null)
1528                                 return;
1529                         
1530                         tableMapping.ColumnMappings.Add (el.GetAttribute ("SourceColumn"), 
1531                                                          el.GetAttribute ("DataSetColumn"));
1532                 }
1533                 
1534 #endif
1535                 
1536                 private void HandleRelationshipAnnotation (XmlElement el, bool nested)
1537                 {
1538                         string name = el.GetAttribute ("name");
1539                         string ptn = el.GetAttribute ("parent", XmlConstants.MsdataNamespace);
1540                         string ctn = el.GetAttribute ("child", XmlConstants.MsdataNamespace);
1541                         string pkn = el.GetAttribute ("parentkey", XmlConstants.MsdataNamespace);
1542                         string fkn = el.GetAttribute ("childkey", XmlConstants.MsdataNamespace);
1543
1544                         RelationStructure rel = new RelationStructure ();
1545                         rel.ExplicitName = XmlHelper.Decode (name);
1546                         rel.ParentTableName = XmlHelper.Decode (ptn);
1547                         rel.ChildTableName = XmlHelper.Decode (ctn);
1548                         // ColumnNames will be decoded wherever they are used as they can
1549                         // contain 'space' separated list of column-names.
1550                         rel.ParentColumnName = pkn;
1551                         rel.ChildColumnName = fkn;
1552                         rel.IsNested = nested;
1553                         rel.CreateConstraint = false; // by default?
1554                         relations.Add (rel);
1555                 }
1556
1557                 private object GetElementDefaultValue (XmlSchemaElement elem)
1558                 {
1559                         // Unlike attribute, element cannot have a default value.
1560                         if (elem.RefName == XmlQualifiedName.Empty)
1561                                 return elem.DefaultValue;
1562                         XmlSchemaElement referenced = schema.Elements [elem.RefName] as XmlSchemaElement;
1563                         if (referenced == null) // considering missing sub components
1564                                 return null;
1565                         return referenced.DefaultValue;
1566                 }
1567
1568                 private object GetAttributeDefaultValue (XmlSchemaAttribute attr)
1569                 {
1570 #if BUGGY_MS_COMPATIBLE
1571                         if (attr == null)
1572                                 return null;
1573                         else if (attr.RefName != XmlQualifiedName.Empty) {
1574                                 XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1575                                 if (referenced != null)
1576                                         return referenced.DefaultValue;
1577                                 else
1578                                         return null;
1579                         }
1580                         if (attr.DefaultValue != null)
1581                                 return attr.DefaultValue;
1582                         return attr.FixedValue;
1583 #else
1584                         if (attr.DefaultValue != null)
1585                                 return attr.DefaultValue;
1586                         else if (attr.FixedValue != null)
1587                                 return attr.FixedValue;
1588                         else if (attr.RefName == XmlQualifiedName.Empty)
1589                                 return null;
1590                         XmlSchemaAttribute referenced = schema.Attributes [attr.RefName] as XmlSchemaAttribute;
1591                         if (referenced == null) // considering missing sub components
1592                                 return null;
1593                         if (referenced.DefaultValue != null)
1594                                 return referenced.DefaultValue;
1595                         return referenced.FixedValue;
1596 #endif
1597                 }
1598         }
1599 }