public void Remove(SchemaImporterExtension extension)
 {
 }
 public int IndexOf(SchemaImporterExtension extension)
 {
 }
 public void Insert(int index, SchemaImporterExtension extension)
 {
 }
 public bool Contains(SchemaImporterExtension extension)
 {
 }
 public void CopyTo(SchemaImporterExtension[] array, int index)
 {
 }
        /// <summary>
        /// Generates the output.
        /// </summary>
        protected override string OnGenerateCode(string inputFileName, string inputFileContent)
        {
            #region load the configuration
            Configuration.Load(base.InputFilePath);
            StringBuilder output = new StringBuilder();
            output.Append(CustomTool.GetToolGeneratedCodeWarning(typeof(XsdCodeGenerator)));
            #endregion

            #region read the schema(s)
            XmlSchema xsd;
            Directory.SetCurrentDirectory(Path.GetDirectoryName(base.InputFilePath));

            using (FileStream fs = File.OpenRead(base.InputFilePath))
            {
                xsd = XmlSchema.Read(fs, null);
            }
            #endregion

            #region create the class generator and set default options
            XsdClassGenerator xsdClassGenerator = new XsdClassGenerator(xsd);

            xsdClassGenerator.CodeGenerationOptions = CodeGenerationOptions.None;

            if (Configuration.Current.EnableDataBinding)
            {
                xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.EnableDataBinding;
            }

            if (Configuration.Current.GenerateOrder)
            {
                xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.GenerateOrder;
            }

            if (Configuration.Current.GenerateProperties)
            {
                xsdClassGenerator.CodeGenerationOptions |= CodeGenerationOptions.GenerateProperties;
            }

            xsdClassGenerator.CodeNamespaceString = base.FileNameSpace;
            xsdClassGenerator.CodeGeneratorOptions.GenerateCodeString = true;
            xsdClassGenerator.CodeGeneratorOptions.GenerateObjects    = false;
            xsdClassGenerator.CodeGeneratorOptions.CompileAssembly    = false;
            #endregion

            #region load the code modifiers
            foreach (AssemblyType assembly in Configuration.Current.CodeModifiers)
            {
                ObjectHandle handle = Activator.CreateInstance(assembly.Assembly, assembly.Type);
                if (handle == null)
                {
                    output.AppendFormat("//\tWarning, could not create CodeModifier type {0} from assembly {0}", assembly.Type, assembly.Assembly);
                    continue;
                }

                ICodeModifier modifier = handle.Unwrap() as ICodeModifier;
                if (modifier == null)
                {
                    output.AppendFormat("//\tWarning CodeModifier {0} from assembly {0} does not derive from ICodeModifier", assembly.Type, assembly.Assembly);
                    continue;
                }

                modifier.XmlOptions = assembly.Any;

                xsdClassGenerator.CodeModifiers.Add(modifier);
            }
            #endregion

            #region load the SchemaImporterExtensions
            foreach (AssemblyType assembly in Configuration.Current.SchemaImporterExtensions)
            {
                ObjectHandle handle = Activator.CreateInstance(assembly.Assembly, assembly.Type);
                if (handle == null)
                {
                    output.AppendFormat("//\tWarning, could not create SchemaImporterExtensions type {0} from assembly {0}", assembly.Type, assembly.Assembly);
                    continue;
                }

                SchemaImporterExtension extension = handle.Unwrap() as SchemaImporterExtension;
                if (extension == null)
                {
                    output.AppendFormat("//\tWarning SchemaImporterExtensions {0} from assembly {0} does not derive from SchemaImporterExtension", assembly.Type, assembly.Assembly);
                    continue;
                }

                xsdClassGenerator.SchemaImporterExtensions.Add(extension);
            }
            #endregion

            // generate code
            xsdClassGenerator.Compile();

            // save config file and make sure it's added to the project
            Configuration.Save(base.InputFilePath);
            AddToProject(Configuration.GetConfigFileName(base.InputFilePath));

            #region Workaround for known bug with fixed attributes:
            output.AppendLine(Resources.Message_1591);
            output.AppendLine(Resources.Pragma_1591_Disable);
            output.Append(xsdClassGenerator.CodeString);
            output.Append(Resources.Pragma_1591_Enable);
            #endregion

            return(output.ToString());
        }
 // Methods
 public int Add(SchemaImporterExtension extension)
 {
 }
Пример #8
0
        private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor)
        {
            string attributeName = accessor.IsFixed ? "fixed" : "default";

            if (!memberTypeDesc.HasDefaultSupport)
            {
                if (comments != null && defaultValue is string)
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    // do not generate intializers for the user prefered types if they do not have default capability
                    AddWarningComment(comments, string.Format(ResXml.XmlDropAttributeValue, attributeName, mapping.TypeName, defaultValue.ToString()));
                }
                return;
            }
            if (memberTypeDesc.IsArrayLike && accessor is ElementAccessor)
            {
                if (comments != null && defaultValue is string)
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    // do not generate intializers for array-like types
                    AddWarningComment(comments, string.Format(ResXml.XmlDropArrayAttributeValue, attributeName, defaultValue.ToString(), ((ElementAccessor)accessor).Name));
                }
                return;
            }
            if (mapping.TypeDesc.IsMappedType && field != null && defaultValue is string)
            {
                SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension;
                CodeExpression          init      = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);

                if (init != null)
                {
                    if (ctor != null)
                    {
                        AddInitializationStatement(ctor, field, init);
                    }
                    else
                    {
                        field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                    }
                }
                if (comments != null)
                {
                    DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName);
                    if (init == null)
                    {
                        AddWarningComment(comments, string.Format(ResXml.XmlNotKnownDefaultValue, extension.GetType().FullName, attributeName, (string)defaultValue, mapping.TypeName, mapping.Namespace));
                    }
                }
                return;
            }
            object value = null;

            if (defaultValue is string || defaultValue == null)
            {
                value = ImportDefault(mapping, (string)defaultValue);
            }
            if (value == null)
            {
                return;
            }
            if (!(mapping is PrimitiveMapping))
            {
                if (comments != null)
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    AddWarningComment(comments, string.Format(ResXml.XmlDropNonPrimitiveAttributeValue, attributeName, defaultValue.ToString()));
                }
                return;
            }
            PrimitiveMapping pm = (PrimitiveMapping)mapping;

            if (comments != null && !pm.TypeDesc.HasDefaultSupport && pm.TypeDesc.IsMappedType)
            {
                // do not generate intializers for the user prefered types if they do not have default capability
                DropDefaultAttribute(accessor, comments, pm.TypeDesc.FullName);
                return;
            }
            if (value == DBNull.Value)
            {
                if (comments != null)
                {
                    AddWarningComment(comments, string.Format(ResXml.XmlDropAttributeValue, attributeName, pm.TypeName, defaultValue.ToString()));
                }
                return;
            }
            CodeAttributeArgument[] arguments      = null;
            CodeExpression          initExpression = null;

            if (pm.IsList)
            {
#if DEBUG
                // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
                if (value.GetType() != typeof(object[]))
                {
                    throw new InvalidOperationException(string.Format(ResXml.XmlInternalErrorDetails, "Default value for list should be object[], not " + value.GetType().Name));
                }
#endif

                object[]         vals         = (object[])value;
                CodeExpression[] initializers = new CodeExpression[vals.Length];
                for (int i = 0; i < vals.Length; i++)
                {
                    GetDefaultValueArguments(pm, vals[i], out initializers[i]);
                }
                initExpression = new CodeArrayCreateExpression(field.Type, initializers);
            }
            else
            {
                arguments = GetDefaultValueArguments(pm, value, out initExpression);
            }

            if (field != null)
            {
                if (ctor != null)
                {
                    AddInitializationStatement(ctor, field, initExpression);
                }
                else
                {
                    field.InitExpression = initExpression;
                }
            }
            if (arguments != null && pm.TypeDesc.HasDefaultSupport && accessor.IsOptional && !accessor.IsFixed)
            {
                // Add [DefaultValueAttribute]
                CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments);
                metadata.Add(attribute);
            }
            else if (comments != null)
            {
                DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
            }
        }
        private void AddDefaultValueAttribute(CodeMemberField field, CodeAttributeDeclarationCollection metadata, object defaultValue, TypeMapping mapping, CodeCommentStatementCollection comments, TypeDesc memberTypeDesc, Accessor accessor, CodeConstructor ctor)
        {
            string str = accessor.IsFixed ? "fixed" : "default";

            if (!memberTypeDesc.HasDefaultSupport)
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping.TypeName, defaultValue.ToString() }));
                }
            }
            else if (memberTypeDesc.IsArrayLike && (accessor is ElementAccessor))
            {
                if ((comments != null) && (defaultValue is string))
                {
                    DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                    CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropArrayAttributeValue", new object[] { str, defaultValue.ToString(), ((ElementAccessor)accessor).Name }));
                }
            }
            else if ((mapping.TypeDesc.IsMappedType && (field != null)) && (defaultValue is string))
            {
                SchemaImporterExtension extension = mapping.TypeDesc.ExtendedType.Extension;
                CodeExpression          init      = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                if (init != null)
                {
                    if (ctor != null)
                    {
                        AddInitializationStatement(ctor, field, init);
                    }
                    else
                    {
                        field.InitExpression = extension.ImportDefaultValue((string)defaultValue, mapping.TypeDesc.FullName);
                    }
                }
                if (comments != null)
                {
                    DropDefaultAttribute(accessor, comments, mapping.TypeDesc.FullName);
                    if (init == null)
                    {
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlNotKnownDefaultValue", new object[] { extension.GetType().FullName, str, (string)defaultValue, mapping.TypeName, mapping.Namespace }));
                    }
                }
            }
            else
            {
                object obj2 = null;
                if ((defaultValue is string) || (defaultValue == null))
                {
                    obj2 = this.ImportDefault(mapping, (string)defaultValue);
                }
                if (obj2 != null)
                {
                    if (!(mapping is PrimitiveMapping))
                    {
                        DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                        CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropNonPrimitiveAttributeValue", new object[] { str, defaultValue.ToString() }));
                    }
                    else
                    {
                        PrimitiveMapping mapping2 = (PrimitiveMapping)mapping;
                        if (((comments != null) && !mapping2.TypeDesc.HasDefaultSupport) && mapping2.TypeDesc.IsMappedType)
                        {
                            DropDefaultAttribute(accessor, comments, mapping2.TypeDesc.FullName);
                        }
                        else if (obj2 == DBNull.Value)
                        {
                            if (comments != null)
                            {
                                CodeExporter.AddWarningComment(comments, Res.GetString("XmlDropAttributeValue", new object[] { str, mapping2.TypeName, defaultValue.ToString() }));
                            }
                        }
                        else
                        {
                            CodeAttributeArgument[] arguments      = null;
                            CodeExpression          initExpression = null;
                            if (mapping2.IsList)
                            {
                                object[]         objArray     = (object[])obj2;
                                CodeExpression[] initializers = new CodeExpression[objArray.Length];
                                for (int i = 0; i < objArray.Length; i++)
                                {
                                    this.GetDefaultValueArguments(mapping2, objArray[i], out initializers[i]);
                                }
                                initExpression = new CodeArrayCreateExpression(field.Type, initializers);
                            }
                            else
                            {
                                arguments = this.GetDefaultValueArguments(mapping2, obj2, out initExpression);
                            }
                            if (field != null)
                            {
                                if (ctor != null)
                                {
                                    AddInitializationStatement(ctor, field, initExpression);
                                }
                                else
                                {
                                    field.InitExpression = initExpression;
                                }
                            }
                            if (((arguments != null) && mapping2.TypeDesc.HasDefaultSupport) && (accessor.IsOptional && !accessor.IsFixed))
                            {
                                CodeAttributeDeclaration declaration = new CodeAttributeDeclaration(typeof(DefaultValueAttribute).FullName, arguments);
                                metadata.Add(declaration);
                            }
                            else if (comments != null)
                            {
                                DropDefaultAttribute(accessor, comments, memberTypeDesc.FullName);
                            }
                        }
                    }
                }
            }
        }
 // Methods
 public int Add(SchemaImporterExtension extension)
 {
 }
 public void Remove(SchemaImporterExtension extension)
 {
 }
 public bool Contains(SchemaImporterExtension extension)
 {
 }
 public int IndexOf(SchemaImporterExtension extension)
 {
 }
 public void Insert(int index, SchemaImporterExtension extension)
 {
 }