Jelajahi Sumber

* XmlReflectionImporter.cs: Fill RelatedMaps property of the generated map.
* XmlSchemas.cs: Find method: make sure the returned object belongs to
the requested type.
* XmlSerializationReader.cs: Removed unneded virtual ReadObject method.
Add null checks for eventSource.
* XmlSerializationReaderInterpreter.cs: ReadObject is not virtual any more.
* XmlSerializationWriter.cs: In Initialize method, initialize the provided
namespece declarations. Virtual method WriteObject not needed any more.
In WriteStartElement, write the provided namespaces.
* XmlSerializationWriterInterpreter.cs: Write object is not virtual any more.
Added GetTypeMap method, that returns the map for a given type. Added some
virtual methods, so writer behavior can be extended at several places by
derived classes.
* XmlSerializer.cs: Changed behavior to match what MS.NET does. The virtual
methods CreateReader and CreateWriter are not called unless no type or
type mapping was provided in the constructor.

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

Lluis Sanchez 22 tahun lalu
induk
melakukan
d19e51679d

+ 19 - 0
mcs/class/System.XML/System.Xml.Serialization/ChangeLog

@@ -1,3 +1,22 @@
+2003-08-28  Lluis Sanchez Gual <[email protected]>
+
+	* XmlReflectionImporter.cs: Fill RelatedMaps property of the generated map.
+	* XmlSchemas.cs: Find method: make sure the returned object belongs to
+	  the requested type.
+	* XmlSerializationReader.cs: Removed unneded virtual ReadObject method.
+	  Add null checks for eventSource.
+	* XmlSerializationReaderInterpreter.cs: ReadObject is not virtual any more.
+	* XmlSerializationWriter.cs: In Initialize method, initialize the provided
+	  namespece declarations. Virtual method WriteObject not needed any more.
+	  In WriteStartElement, write the provided namespaces.
+	* XmlSerializationWriterInterpreter.cs: Write object is not virtual any more.
+	  Added GetTypeMap method, that returns the map for a given type. Added some
+	  virtual methods, so writer behavior can be extended at several places by
+	  derived classes.
+	* XmlSerializer.cs: Changed behavior to match what MS.NET does. The virtual 
+	  methods CreateReader and CreateWriter are not called unless no type or 
+	  type mapping was provided in the constructor.
+
 2003-08-12  Lluis Sanchez Gual <[email protected]>
 
 	* XmlSchemaImporter.cs: ImportTypeMapping doesn't need to check if the

+ 4 - 0
mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs

@@ -21,6 +21,7 @@ namespace System.Xml.Serialization {
 		ArrayList includedTypes;
 		ReflectionHelper helper = new ReflectionHelper();
 		int arrayChoiceCount = 1;
+		ArrayList relatedMaps = new ArrayList ();
 
 		static readonly string errSimple = "Cannot serialize object of type '{0}'. Base " +
 			"type '{1}' has simpleContent and can be only extended by adding XmlAttribute " +
@@ -79,6 +80,7 @@ namespace System.Xml.Serialization {
 				mapping[n] = new XmlMemberMapping (members[n], mapMem);
 			}
 			XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
+			mps.RelatedMaps = relatedMaps;
 			mps.Format = SerializationFormat.Literal;
 			return mps;
 		}
@@ -122,6 +124,7 @@ namespace System.Xml.Serialization {
 				default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
 			}
 
+			map.RelatedMaps = relatedMaps;
 			map.Format = SerializationFormat.Literal;
 			return map;
 		}
@@ -170,6 +173,7 @@ namespace System.Xml.Serialization {
 
 			if (membersNamespace == null) membersNamespace = "";
 			XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType, defaultNamespace);
+			relatedMaps.Add (map);
 			return map;
 		}
 

+ 5 - 3
mcs/class/System.XML/System.Xml.Serialization/XmlSchemas.cs

@@ -78,7 +78,7 @@ namespace System.Xml.Serialization {
 			if (!schema.IsCompiled) {
 				try {
 					schema.Compile (null);
-				} catch {
+				} catch (Exception ex) {
 					throw new InvalidOperationException ("Error compiling XmlSchema " + 
 									     name.Namespace);
 				}
@@ -99,7 +99,9 @@ namespace System.Xml.Serialization {
 			else if (type == typeof (XmlSchemaNotation))
 				tbl = schema.Notations;
 
-			return (tbl != null) ? tbl [name] : null;
+			object res = (tbl != null) ? tbl [name] : null;
+			if (res != null && res.GetType () != type) return null;
+			else return res;
 		}
 
 		public int IndexOf (XmlSchema schema)
@@ -149,4 +151,4 @@ namespace System.Xml.Serialization {
 
 		#endregion // Methods
 	}
-}
+}

+ 12 - 13
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReader.cs

@@ -72,11 +72,6 @@ namespace System.Xml.Serialization {
 			InitIDs ();
 		}
 			
-		internal virtual object ReadObject ()
-		{
-			throw new NotImplementedException ();
-		}
-
 		private ArrayList EnsureArrayList (ArrayList list)
 		{
 			if (list == null)
@@ -752,8 +747,9 @@ namespace System.Xml.Serialization {
 				line_number = 0;
 				line_position = 0;
 			}
-			
-			eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
+
+			if (eventSource != null)
+				eventSource.OnUnknownAttribute (new XmlAttributeEventArgs (attr, line_number, line_position, o));
 		}
 
 		protected void UnknownElement (object o, XmlElement elem)
@@ -767,8 +763,9 @@ namespace System.Xml.Serialization {
 				line_number = 0;
 				line_position = 0;
 			}
-			
-			eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
+	
+			if (eventSource != null)
+				eventSource.OnUnknownElement (new XmlElementEventArgs (elem, line_number, line_position,o));
 		}
 
 		protected void UnknownNode (object o)
@@ -782,8 +779,10 @@ namespace System.Xml.Serialization {
 				line_number = 0;
 				line_position = 0;
 			}
-			
-			eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
+	
+			if (eventSource != null)
+				eventSource.OnUnknownNode (new XmlNodeEventArgs(line_number, line_position, Reader.LocalName, Reader.Name, Reader.NamespaceURI, Reader.NodeType, o, Reader.Value));
+	
 			if (Reader.NodeType == XmlNodeType.Attribute)
 			{
 				XmlAttribute att = (XmlAttribute) ReadXmlNode (false);
@@ -806,7 +805,8 @@ namespace System.Xml.Serialization {
 
 		protected void UnreferencedObject (string id, object o)
 		{
-			eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
+			if (eventSource != null)
+				eventSource.OnUnreferencedObject (new UnreferencedObjectEventArgs (o,id));
 		}
 
 		#endregion // Methods
@@ -917,4 +917,3 @@ namespace System.Xml.Serialization {
 
 	}
 }
-

+ 1 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs

@@ -44,7 +44,7 @@ namespace System.Xml.Serialization
 		{
 		}
 
-		internal override object ReadObject ()
+		public object ReadObject ()
 		{
 			Reader.MoveToContent();
 			if (_typeMap is XmlTypeMapping)

+ 17 - 10
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriter.cs

@@ -39,6 +39,17 @@ namespace System.Xml.Serialization {
 		{
 			qnameCount = 0;
 		}
+		
+		internal void Initialize (XmlWriter writer, XmlSerializerNamespaces nss)
+		{
+			this.writer = writer;
+			if (nss != null)
+			{
+				namespaces = new ArrayList ();
+				foreach (XmlQualifiedName ns in nss.ToArray())
+					namespaces.Add (ns);
+			}	
+		}
 
 		#endregion // Constructors
 
@@ -58,16 +69,6 @@ namespace System.Xml.Serialization {
 
 		#region Methods
 
-		internal void Initialize (XmlWriter writer)
-		{
-			this.writer = writer;
-		}
-
-		internal virtual void WriteObject (object ob)
-		{
-			throw new NotImplementedException ();
-		}
-
 		protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
 		{
 			WriteCallbackInfo info = new WriteCallbackInfo ();
@@ -684,6 +685,12 @@ namespace System.Xml.Serialization {
 					WriteAttribute ("xmlns","xsd",xmlNamespace,XmlSchema.Namespace);
 				if (Writer.LookupPrefix (XmlSchema.InstanceNamespace) == null)
 					WriteAttribute ("xmlns","xsi",xmlNamespace,XmlSchema.InstanceNamespace);
+					
+				if (namespaces != null)
+				{
+					foreach (XmlQualifiedName qn in namespaces)
+						WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
+				}
 			}
 			topLevelElement = false;
 		}

+ 44 - 12
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriterInterpreter.cs

@@ -15,12 +15,12 @@ using System.Xml.Schema;
 
 namespace System.Xml.Serialization
 {
-	internal class XmlSerializationWriterInterpreter: XmlSerializationWriter
+	public class XmlSerializationWriterInterpreter: XmlSerializationWriter
 	{
 		XmlMapping _typeMap;
 		SerializationFormat _format;
 
-		public XmlSerializationWriterInterpreter(XmlMapping typeMap)
+		public XmlSerializationWriterInterpreter (XmlMapping typeMap)
 		{
 			_typeMap = typeMap;
 			_format = typeMap.Format;
@@ -39,7 +39,7 @@ namespace System.Xml.Serialization
 			}
 		}
 
-		internal override void WriteObject (object ob)
+		public void WriteObject (object ob)
 		{
 			WriteStartDocument ();
 
@@ -61,8 +61,19 @@ namespace System.Xml.Serialization
 
 			WriteReferencedElements ();
 		}
+		
+		protected XmlTypeMapping GetTypeMap (Type type)
+		{
+			ArrayList maps = _typeMap.RelatedMaps;
+			if (maps != null)
+			{
+				foreach (XmlTypeMapping map in maps)
+					if (map.TypeData.Type == type) return map;
+			}
+			throw new InvalidOperationException ("Type " + type + " not mapped");
+		}
 
-		internal void WriteObject (XmlTypeMapping typeMap, object ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
+		protected virtual void WriteObject (XmlTypeMapping typeMap, object ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
 		{
 			if (ob == null)
 			{
@@ -116,7 +127,7 @@ namespace System.Xml.Serialization
 				WriteEndElement (ob);
 		}
 
-		void WriteMessage (XmlMembersMapping membersMap, object[] parameters)
+		protected virtual void WriteMessage (XmlMembersMapping membersMap, object[] parameters)
 		{
 			if (membersMap.HasWrapperElement) {
 				TopLevelElement ();
@@ -135,15 +146,35 @@ namespace System.Xml.Serialization
 				WriteEndElement();
 		}
 
-		void WriteObjectElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
+		protected virtual void WriteObjectElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
 		{
 			ClassMap map = (ClassMap)typeMap.ObjectMap;
 			if (map.NamespaceDeclarations != null)
 				WriteNamespaceDeclarations ((XmlSerializerNamespaces) map.NamespaceDeclarations.GetValue (ob));
-			WriteMembers (map, ob, false);
+			
+			WriteObjectElementAttributes (typeMap, ob);
+			WriteObjectElementElements (typeMap, ob);
+		}
+		
+		protected virtual void WriteObjectElementAttributes (XmlTypeMapping typeMap, object ob)
+		{
+			ClassMap map = (ClassMap)typeMap.ObjectMap;
+			WriteAttributeMembers (map, ob, false);
+		}
+
+		protected virtual void WriteObjectElementElements (XmlTypeMapping typeMap, object ob)
+		{
+			ClassMap map = (ClassMap)typeMap.ObjectMap;
+			WriteElementMembers (map, ob, false);
 		}
 
 		void WriteMembers (ClassMap map, object ob, bool isValueList)
+		{
+			WriteAttributeMembers (map, ob, isValueList);
+			WriteElementMembers (map, ob, isValueList);
+		}
+		
+		void WriteAttributeMembers (ClassMap map, object ob, bool isValueList)
 		{
 			// Write attributes
 
@@ -166,9 +197,10 @@ namespace System.Xml.Serialization
 						WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
 				}
 			}
+		}
 
-			// Write elements
-
+		void WriteElementMembers (ClassMap map, object ob, bool isValueList)
+		{
 			ICollection members = map.ElementMembers;
 			if (members != null)
 			{
@@ -318,7 +350,7 @@ namespace System.Xml.Serialization
 			}
 		}
 
-		void WriteListElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
+		protected virtual void WriteListElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
 		{
 			if (_format == SerializationFormat.Encoded)
 			{
@@ -407,12 +439,12 @@ namespace System.Xml.Serialization
 			}
 		}
 
-		void WritePrimitiveElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
+		protected virtual void WritePrimitiveElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
 		{
 			Writer.WriteString (GetStringValue (typeMap, typeMap.TypeData, ob));
 		}
 
-		void WriteEnumElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
+		protected virtual void WriteEnumElement (XmlTypeMapping typeMap, object ob, string element, string namesp)
 		{
 			Writer.WriteString (GetEnumXmlValue (typeMap, ob));
 		}

+ 37 - 8
mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs

@@ -79,7 +79,7 @@ namespace System.Xml.Serialization
 
 			XmlReflectionImporter importer = new XmlReflectionImporter (overrides, defaultNamespace);
 
-			if (extraTypes != null) 
+			if (extraTypes != null) 
 			{
 				foreach (Type intype in extraTypes)
 					importer.IncludeType (intype);
@@ -156,12 +156,14 @@ namespace System.Xml.Serialization
 
 		protected virtual XmlSerializationReader CreateReader ()
 		{
-			return new XmlSerializationReaderInterpreter (typeMapping);
+			// Must be implemented in derived class
+			throw new NotImplementedException ();
 		}
 
 		protected virtual XmlSerializationWriter CreateWriter ()
 		{
-			return new XmlSerializationWriterInterpreter (typeMapping);
+			// Must be implemented in derived class
+			throw new NotImplementedException ();
 		}
 
 		public object Deserialize (Stream stream)
@@ -178,14 +180,27 @@ namespace System.Xml.Serialization
 
 		public object Deserialize (XmlReader xmlReader)
 		{
-			XmlSerializationReader xsReader = CreateReader ();
+			XmlSerializationReader xsReader;
+			if (typeMapping == null)
+				xsReader = CreateReader ();
+			else
+				xsReader = new XmlSerializationReaderInterpreter (typeMapping);
+				
 			xsReader.Initialize (xmlReader, this);
 			return Deserialize (xsReader);
 		}
 
 		protected virtual object Deserialize (XmlSerializationReader reader)
 		{
-			return reader.ReadObject ();
+			if (typeMapping == null)
+			{
+				XmlSerializationReaderInterpreter rd = reader as XmlSerializationReaderInterpreter;
+				if (rd == null) throw new InvalidOperationException ();
+				return rd.ReadObject ();
+			}
+			else
+				// Must be implemented in derived class
+				throw new NotImplementedException ();
 		}
 
 		public static XmlSerializer [] FromMappings (XmlMapping	[] mappings)
@@ -206,7 +221,15 @@ namespace System.Xml.Serialization
 
 		protected virtual void Serialize (object o, XmlSerializationWriter writer)
 		{
-			writer.WriteObject (o);
+			if (typeMapping != null)
+			{
+				XmlSerializationWriterInterpreter wr = writer as XmlSerializationWriterInterpreter;
+				if (wr == null) throw new InvalidOperationException ();
+				wr.WriteObject (o);
+			}
+			else
+				// Must be implemented in derived class
+				throw new NotImplementedException ();
 		}
 
 		public void Serialize (Stream stream, object o)
@@ -245,8 +268,14 @@ namespace System.Xml.Serialization
 
 		public void Serialize (XmlWriter writer, object o, XmlSerializerNamespaces namespaces)
 		{
-			XmlSerializationWriter xsWriter = CreateWriter ();
-			xsWriter.Initialize (writer);
+			XmlSerializationWriter xsWriter;
+			
+			if (typeMapping == null)
+				xsWriter = CreateWriter ();
+			else
+				xsWriter = new XmlSerializationWriterInterpreter (typeMapping);
+				
+			xsWriter.Initialize (writer, namespaces);
 			Serialize (o, xsWriter);
 			writer.Flush ();
 		}