Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlSerializer.cs
index 380774f4be215023671db1580ab55d17e3c0ffbe..b0009f5390243fd36b6f2a03464a8e75bd467ac4 100644 (file)
@@ -37,7 +37,7 @@ using System.Reflection;
 using System.Xml;
 using System.Xml.Schema;
 using System.Text;
-#if !TARGET_JVM && !MONOTOUCH
+#if !NET_2_1
 using System.CodeDom;
 using System.CodeDom.Compiler;
 using Microsoft.CSharp;
@@ -68,6 +68,7 @@ namespace System.Xml.Serialization
                internal class SerializerData
                {
                        public int UsageCount;
+                       public bool Generated;
                        public Type ReaderType;
                        public MethodInfo ReaderMethod;
                        public Type WriterType;
@@ -120,7 +121,7 @@ namespace System.Xml.Serialization
                        //       debugging pourposes by adding the "nofallback" option.
                        //       For example: MONO_XMLSERIALIZER_THS=0,nofallback
                        
-#if TARGET_JVM || MONOTOUCH
+#if NET_2_1
                        string db = null;
                        string th = null;
                        generationThreshold = -1;
@@ -145,12 +146,11 @@ namespace System.Xml.Serialization
                                else {
                                        generationThreshold = int.Parse (th, CultureInfo.InvariantCulture);
                                        backgroundGeneration = (generationThreshold != 0);
-                                       if (generationThreshold < 1) generationThreshold = 1;
                                }
                        }
 #endif
                        deleteTempFiles = (db == null || db == "no");
-#if !MONOTOUCH                 
+#if !NET_2_1
                        IDictionary table = (IDictionary) ConfigurationSettings.GetConfig("system.diagnostics");
                        if (table != null) 
                        {
@@ -232,7 +232,6 @@ namespace System.Xml.Serialization
                        get { return typeMapping; }
                }
 
-#if NET_2_0
 
                [MonoTODO]
                public XmlSerializer (Type type,
@@ -244,16 +243,14 @@ namespace System.Xml.Serialization
                        Evidence evidence)
                {
                }
-#endif
 
 #endregion // Constructors
 
 #region Events
-
+               private UnreferencedObjectEventHandler onUnreferencedObject;
                private XmlAttributeEventHandler onUnknownAttribute;
                private XmlElementEventHandler onUnknownElement;
                private XmlNodeEventHandler onUnknownNode;
-               private UnreferencedObjectEventHandler onUnreferencedObject;
 
                public event XmlAttributeEventHandler UnknownAttribute 
                {
@@ -270,11 +267,6 @@ namespace System.Xml.Serialization
                        add { onUnknownNode += value; } remove { onUnknownNode -= value; }
                }
 
-               public event UnreferencedObjectEventHandler UnreferencedObject 
-               {
-                       add { onUnreferencedObject += value; } remove { onUnreferencedObject -= value; }
-               }
-
 
                internal virtual void OnUnknownAttribute (XmlAttributeEventArgs e) 
                {
@@ -296,6 +288,10 @@ namespace System.Xml.Serialization
                        if (onUnreferencedObject != null) onUnreferencedObject(this, e);
                }
 
+               public event UnreferencedObjectEventHandler UnreferencedObject 
+               {
+                       add { onUnreferencedObject += value; } remove { onUnreferencedObject -= value; }
+               }
 
 #endregion // Events
 
@@ -359,8 +355,13 @@ namespace System.Xml.Serialization
                        try {
                                if (reader is XmlSerializationReaderInterpreter)
                                        return ((XmlSerializationReaderInterpreter) reader).ReadRoot ();
-                               else
-                                       return serializerData.ReaderMethod.Invoke (reader, null);
+                               else {
+                                       try {
+                                               return serializerData.ReaderMethod.Invoke (reader, null);
+                                       } catch (TargetInvocationException ex) {
+                                               throw ex.InnerException;
+                                       }
+                               }
                        } catch (Exception ex) {
                                if (ex is InvalidOperationException || ex is InvalidCastException)
                                        throw new InvalidOperationException ("There is an error in"
@@ -391,11 +392,11 @@ namespace System.Xml.Serialization
                        return sers;
                }
 
-               public static XmlSerializer [] FromTypes (Type [] mappings)
+               public static XmlSerializer [] FromTypes (Type [] types)
                {
-                       XmlSerializer [] sers = new XmlSerializer [mappings.Length];
-                       for (int n=0; n<mappings.Length; n++)
-                               sers[n] = new XmlSerializer (mappings[n]);
+                       XmlSerializer [] sers = new XmlSerializer [types.Length];
+                       for (int n=0; n<types.Length; n++)
+                               sers[n] = new XmlSerializer (types[n]);
                        return sers;
                }
 
@@ -407,13 +408,20 @@ namespace System.Xml.Serialization
                                
                        if (writer is XmlSerializationWriterInterpreter)
                                ((XmlSerializationWriterInterpreter)writer).WriteRoot (o);
-                       else
-                               serializerData.WriterMethod.Invoke (writer, new object[] {o});
+                       else {
+                               try {
+                                       serializerData.WriterMethod.Invoke (writer, new object[] {o});
+                               } catch (TargetInvocationException ex) {
+                                       throw ex.InnerException;
+                               }
+                       }
                }
 
+               static Encoding DefaultEncoding = Encoding.Default;
+
                public void Serialize (Stream stream, object o)
                {
-                       XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
+                       XmlTextWriter xmlWriter = new XmlTextWriter (stream, DefaultEncoding);
                        xmlWriter.Formatting = Formatting.Indented;
                        Serialize (xmlWriter, o, null);
                }
@@ -432,7 +440,7 @@ namespace System.Xml.Serialization
 
                public void Serialize (Stream stream, object o, XmlSerializerNamespaces namespaces)
                {
-                       XmlTextWriter xmlWriter = new XmlTextWriter (stream, System.Text.Encoding.Default);
+                       XmlTextWriter xmlWriter = new XmlTextWriter (stream, DefaultEncoding);
                        xmlWriter.Formatting = Formatting.Indented;
                        Serialize (xmlWriter, o, namespaces);
                }
@@ -445,7 +453,7 @@ namespace System.Xml.Serialization
                        xmlWriter.Flush();
                }
 
-               public void Serialize (XmlWriter writer, object o, XmlSerializerNamespaces namespaces)
+               public void Serialize (XmlWriter xmlWriter, object o, XmlSerializerNamespaces namespaces)
                {
                        XmlSerializationWriter xsWriter;
 
@@ -457,18 +465,13 @@ namespace System.Xml.Serialization
 
                                if (namespaces == null || namespaces.Count == 0) {
                                        namespaces = new XmlSerializerNamespaces ();
-#if NET_2_0
                                        namespaces.Add ("xsi", XmlSchema.InstanceNamespace);
                                        namespaces.Add ("xsd", XmlSchema.Namespace);
-#else
-                                       namespaces.Add ("xsd", XmlSchema.Namespace);
-                                       namespaces.Add ("xsi", XmlSchema.InstanceNamespace);
-#endif
                                }
 
-                               xsWriter.Initialize (writer, namespaces);
+                               xsWriter.Initialize (xmlWriter, namespaces);
                                Serialize (o, xsWriter);
-                               writer.Flush ();
+                               xmlWriter.Flush ();
                        } catch (Exception ex) {
                                if (ex is TargetInvocationException)
                                        ex = ex.InnerException;
@@ -481,8 +484,6 @@ namespace System.Xml.Serialization
                        }
                }
                
-#if NET_2_0
-               
                [MonoTODO]
                public object Deserialize (XmlReader xmlReader, string encodingStyle, XmlDeserializationEvents events)
                {
@@ -513,7 +514,7 @@ namespace System.Xml.Serialization
                        throw new NotImplementedException ();
                }
 
-#if !TARGET_JVM && !MONOTOUCH
+#if !MOBILE
                public static Assembly GenerateSerializer (Type[] types, XmlMapping[] mappings)
                {
                        return GenerateSerializer (types, mappings, null);
@@ -557,7 +558,6 @@ namespace System.Xml.Serialization
                {
                        throw new NotImplementedException ();
                }
-#endif
                
                XmlSerializationWriter CreateWriter (XmlMapping typeMapping)
                {
@@ -572,6 +572,7 @@ namespace System.Xml.Serialization
                                }
                        }
                        
+#if !NET_2_1
                        if (!typeMapping.Source.CanBeGenerated || generationThreshold == -1)
                                return new XmlSerializationWriterInterpreter (typeMapping);
 
@@ -586,11 +587,13 @@ namespace System.Xml.Serialization
                                        throw new InvalidOperationException ("Error while generating serializer");
                        }
                        
+#endif
                        return new XmlSerializationWriterInterpreter (typeMapping);
                }
                
                XmlSerializationReader CreateReader (XmlMapping typeMapping)
                {
+#if !NET_2_1
                        XmlSerializationReader reader;
                        
                        lock (this) {
@@ -615,11 +618,12 @@ namespace System.Xml.Serialization
                                if (!generatorFallback)
                                        throw new InvalidOperationException ("Error while generating serializer");
                        }
-                       
+
+#endif
                        return new XmlSerializationReaderInterpreter (typeMapping);
                }
                
-#if TARGET_JVM || MONOTOUCH
+#if NET_2_1
                void CheckGeneratedTypes (XmlMapping typeMapping)
                {
                        throw new NotImplementedException();
@@ -653,7 +657,10 @@ namespace System.Xml.Serialization
                        bool generate = false;
                        lock (serializerData)
                        {
-                               generate = (++serializerData.UsageCount == generationThreshold);
+                               if (serializerData.UsageCount >= generationThreshold && !serializerData.Generated)
+                                       serializerData.Generated = generate = true;
+
+                               serializerData.UsageCount++;
                        }
                        
                        if (generate)
@@ -790,17 +797,10 @@ namespace System.Xml.Serialization
                }
 #endif
                
-#if NET_2_0
                GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
                {
                        return batch;
                }
-#else
-               GenerationBatch LoadFromSatelliteAssembly (GenerationBatch batch)
-               {
-                       return batch;
-               }
-#endif
                
 #endregion // Methods
        }