2006-02-17 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data / XmlDiffLoader.cs
index 1a68d4cd09f2e2577cb3e8c6d99dd9a4b6662297..13ef898fdaaac0b737cedbb358b12eb31ddfa83e 100644 (file)
 //
 // (c)copyright 2003 Ville Palo
 //
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;
 using System.Data;
 using System.Xml;
+using System.Xml.Schema;
 using System.Xml.XPath;
 using System.Collections;
 using System.Globalization;
@@ -93,8 +117,10 @@ namespace System.Data {
 
                private void LoadCurrent (XmlReader reader) 
                {
-                       if (reader.IsEmptyElement) return;
-                       
+                       if (reader.IsEmptyElement) {
+                               reader.Skip();
+                               return;
+                       }
                        reader.ReadStartElement ();             // Dataset root
                        reader.MoveToContent ();
 
@@ -122,8 +148,10 @@ namespace System.Data {
 
                private void LoadBefore (XmlReader reader) 
                {
-                       if (reader.IsEmptyElement) return;
-                       
+                       if (reader.IsEmptyElement) {
+                               reader.Skip ();
+                               return;
+                       }
                        reader.ReadStartElement ();
                        reader.MoveToContent ();
 
@@ -147,8 +175,10 @@ namespace System.Data {
                                           
                private void LoadErrors (XmlReader reader) 
                {
-                       if (reader.IsEmptyElement) return;
-                       
+                       if (reader.IsEmptyElement) {
+                               reader.Skip ();
+                               return;
+                       }
                        reader.ReadStartElement ();
                        reader.MoveToContent ();
 
@@ -182,10 +212,52 @@ namespace System.Data {
                        reader.ReadEndElement ();
                }
 
-               private void LoadColumns (DataTable Table, DataRow Row, XmlReader reader, DataRowVersion loadType) 
+               private void LoadColumns (DataTable Table, DataRow Row, 
+                       XmlReader reader, DataRowVersion loadType)
                {
-                       if (reader.IsEmptyElement) return;
-                       
+                       // attributes
+                       LoadColumnAttributes (Table, Row, reader, loadType);
+                       LoadColumnChildren (Table, Row, reader, loadType);
+               }
+
+               private void LoadColumnAttributes (DataTable Table, DataRow Row,
+                       XmlReader reader, DataRowVersion loadType)
+               {
+                       if (!reader.HasAttributes // this check will be faster
+                               || !reader.MoveToFirstAttribute ())
+                               return;
+                       do {
+                               switch (reader.NamespaceURI) {
+                               case XmlConstants.XmlnsNS:
+                               case XmlConstants.DiffgrNamespace:
+                               case XmlConstants.MsdataNamespace:
+                               case XmlConstants.MspropNamespace:
+                               case XmlSchema.Namespace:
+                                       continue;
+                               }
+                               DataColumn c = Table.Columns [reader.LocalName];
+                               if (c == null ||
+                                       c.ColumnMapping != MappingType.Attribute)                                       continue;
+                               if (c.Namespace == null && reader.NamespaceURI == String.Empty ||
+                                       c.Namespace == reader.NamespaceURI) {
+                                       object data = XmlDataLoader.StringToObject (c.DataType, reader.Value);
+                                       if (loadType == DataRowVersion.Current)
+                                               Row [c] = data;
+                                       else
+                                               Row.SetOriginalValue (c.ColumnName, data);
+                               } // otherwise just ignore as well as unknown elements.
+                       } while (reader.MoveToNextAttribute ());
+                       reader.MoveToElement ();
+               }
+
+               private void LoadColumnChildren (DataTable Table, DataRow Row,
+                       XmlReader reader, DataRowVersion loadType) 
+               {
+                       // children
+                       if (reader.IsEmptyElement) {
+                               reader.Skip ();
+                               return;
+                       }
                        reader.ReadStartElement ();
                        reader.MoveToContent ();