Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / MapCodeGenerator.cs
index 8a50c329372314a38c387566b855c8c8018775a6..1887ab24723f765700b0594cbb1fe3f8c8ad10b6 100644 (file)
 using System.CodeDom;
 using System.CodeDom.Compiler;
 using System.Collections;
-#if NET_2_0
 using System.ComponentModel;
 using System.Diagnostics;
-#endif
 using System.Globalization;
 using System.Xml.Schema;
 
@@ -44,9 +42,9 @@ namespace System.Xml.Serialization {
        internal class MapCodeGenerator {
 
                CodeNamespace codeNamespace;
-               CodeCompileUnit codeCompileUnit;
+//             CodeCompileUnit codeCompileUnit;
                CodeAttributeDeclarationCollection includeMetadata;
-               XmlTypeMapping exportedAnyType = null;
+               XmlTypeMapping exportedAnyType;
                protected bool includeArrayTypes;
                CodeDomProvider codeProvider;
                CodeGenerationOptions options;
@@ -57,7 +55,7 @@ namespace System.Xml.Serialization {
 
                public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
                {
-                       this.codeCompileUnit = codeCompileUnit;
+//                     this.codeCompileUnit = codeCompileUnit;
                        this.codeNamespace = codeNamespace;
                        this.options = options;
                        this.identifiers = new CodeIdentifiers ();
@@ -65,15 +63,11 @@ namespace System.Xml.Serialization {
 
                public MapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
                {
-                       this.codeCompileUnit = codeCompileUnit;
+//                     this.codeCompileUnit = codeCompileUnit;
                        this.codeNamespace = codeNamespace;
-                       this.codeProvider = codeProvider;
                        this.options = options;
-#if NET_2_0
+                       this.codeProvider = codeProvider;
                        this.identifiers = new CodeIdentifiers ((codeProvider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0);
-#else
-                       this.identifiers = new CodeIdentifiers ();
-#endif
 //                     this.mappings = mappings;
                }
 
@@ -137,9 +131,7 @@ namespace System.Xml.Serialization {
                                if (codeClass != null) {
                                        // Regenerate attributes, since things may have changed
                                        codeClass.CustomAttributes.Clear ();
-#if NET_2_0
                                        AddClassAttributes (codeClass);
-#endif
                                        GenerateClass (map, codeClass, isTopLevel);
                                        ExportDerivedTypeAttributes (map, codeClass);
                                }
@@ -164,10 +156,8 @@ namespace System.Xml.Serialization {
                        AddCodeType (codeClass, map.Documentation);
                        codeClass.Attributes = MemberAttributes.Public;
 
-#if NET_2_0
                        codeClass.IsPartial = CodeProvider.Supports(GeneratorSupport.PartialTypes);
                        AddClassAttributes (codeClass);
-#endif
 
                        GenerateClass (map, codeClass, isTopLevel);
                        ExportDerivedTypeAttributes (map, codeClass);
@@ -176,7 +166,7 @@ namespace System.Xml.Serialization {
 
                        if (map.BaseMap != null && map.BaseMap.TypeData.SchemaType != SchemaTypes.XmlNode)
                        {
-                               CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData);
+                               CodeTypeReference ctr = GetDomType (map.BaseMap.TypeData, false);
                                codeClass.BaseTypes.Add (ctr);
                                if (map.BaseMap.IncludeInSchema) {
                                        ExportMapCode (map.BaseMap, false);
@@ -278,12 +268,12 @@ namespace System.Xml.Serialization {
 
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, TypeData type, string name)
                {
-                       return CreateFieldMember (codeClass, GetDomType (type), name, System.DBNull.Value, null, null);
+                       return CreateFieldMember (codeClass, GetDomType (type, false), name, System.DBNull.Value, null, null);
                }
 
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMember member)
                {
-                       return CreateFieldMember (codeClass, GetDomType (member.TypeData), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
+                       return CreateFieldMember (codeClass, GetDomType (member.TypeData, member.RequiresNullable), member.Name, member.DefaultValue, member.TypeData, member.Documentation);
                }
                
                CodeTypeMember CreateFieldMember (CodeTypeDeclaration codeClass, CodeTypeReference type, string name, object defaultValue, TypeData defaultType, string documentation)
@@ -292,7 +282,7 @@ namespace System.Xml.Serialization {
                        CodeTypeMember codeProp = null;
 
                        if ((options & CodeGenerationOptions.GenerateProperties) > 0) {
-                               string field = identifiers.MakeUnique (CodeIdentifier.MakeCamel (name + "Field"));
+                               string field = identifiers.AddUnique (CodeIdentifier.MakeCamel (name + "Field"), name);
                                codeField = new CodeMemberField (type, field);
                                codeField.Attributes = MemberAttributes.Private;
                                codeClass.Members.Add (codeField);
@@ -381,14 +371,15 @@ namespace System.Xml.Serialization {
                        
                        foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
                        {
-                               if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
-                                       continue;
-
-                               GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName);
                                if (einfo.MappedType != null) {
                                        ExportMapCode (einfo.MappedType, false);
                                        RemoveInclude (einfo.MappedType);
                                }
+
+                               if (ExportExtraElementAttributes (attributes, einfo, defaultNamespace, defaultType))
+                                       continue;
+
+                               GenerateElementInfoMember (attributes, member, einfo, defaultType, defaultNamespace, addAlwaysAttr, forceUseMemberName | addAlwaysAttr);
                        }
 
                        GenerateElementMember (attributes, member);
@@ -497,7 +488,6 @@ namespace System.Xml.Serialization {
                        if (emap.IsFlags)
                                codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration ("System.FlagsAttribute"));
 
-#if NET_2_0
                        CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
                                new CodeTypeReference (typeof(GeneratedCodeAttribute)));
                        generatedCodeAttribute.Arguments.Add (new CodeAttributeArgument (
@@ -508,7 +498,6 @@ namespace System.Xml.Serialization {
 
                        codeEnum.CustomAttributes.Add (new CodeAttributeDeclaration (
                                new CodeTypeReference (typeof (SerializableAttribute))));
-#endif
 
                        GenerateEnum (map, codeEnum, isTopLevel);
                        
@@ -605,7 +594,6 @@ namespace System.Xml.Serialization {
                        codeNamespace.Types.Add (type);
                }
 
-#if NET_2_0
                void AddClassAttributes (CodeTypeDeclaration codeClass)
                {
                        CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration (
@@ -627,16 +615,13 @@ namespace System.Xml.Serialization {
                                new CodePrimitiveExpression ("code")));
                        codeClass.CustomAttributes.Add (designerCategoryAttribute);
                }
-#endif
                
-               CodeTypeReference GetDomType (TypeData data)
+               CodeTypeReference GetDomType (TypeData data, bool requiresNullable)
                {
-#if NET_2_0
-                       if (data.IsValueType && data.IsNullable)
+                       if (data.IsValueType && (data.IsNullable || requiresNullable))
                                return new CodeTypeReference ("System.Nullable", new CodeTypeReference (data.FullTypeName));
-#endif
                        if (data.SchemaType == SchemaTypes.Array)
-                               return new CodeTypeReference (GetDomType (data.ListItemTypeData),1);
+                               return new CodeTypeReference (GetDomType (data.ListItemTypeData, false),1);
                        else
                                return new CodeTypeReference (data.FullTypeName);
                }
@@ -645,7 +630,6 @@ namespace System.Xml.Serialization {
 
                #region Private Properties
 
-#if NET_2_0
                private CodeDomProvider CodeProvider {
                        get {
                                if (codeProvider == null) {
@@ -654,7 +638,6 @@ namespace System.Xml.Serialization {
                                return codeProvider;
                        }
                }
-#endif
 
                #endregion
 
@@ -681,15 +664,16 @@ namespace System.Xml.Serialization {
                                        throw new InvalidOperationException ("Type " + typeData.TypeName + " not supported");
 
                                IFormattable defaultValueFormattable = defaultValue as IFormattable;
-                               CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
+                               CodeFieldReferenceExpression fref = new CodeFieldReferenceExpression (new CodeTypeReferenceExpression (GetDomType (typeData, false)), defaultValueFormattable != null ? defaultValueFormattable.ToString(null, CultureInfo.InvariantCulture) : defaultValue.ToString ());
                                CodeAttributeArgument arg = new CodeAttributeArgument (fref);
                                AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", arg);
-                               internalField.InitExpression = fref;
+                               //internalField.InitExpression = fref;
                        }
                        else
                        {
+                               defaultValue = defaultValue is decimal ? (object) ('"' + ((decimal) defaultValue).ToString (CultureInfo.InvariantCulture) + '"') : defaultValue;
                                AddCustomAttribute (externalField, "System.ComponentModel.DefaultValue", GetArg (defaultValue));
-                               internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
+                               //internalField.InitExpression = new CodePrimitiveExpression (defaultValue);
                        }
                }