Ejemplo n.º 1
0
        private ICodeGenerator buildCodeGenerator(Options.CodeGenOptions options)
        {
            ICodeGenerator codeGenerator = null;

            if (options.GenerateDataContractsOnly) // Generate from data contract files
            {
                // Generate data contract code
                codeGenerator = new DataContractGenerator();
            }
            else // Generate from wsdl file
            {
                codeGenerator = new InterfaceContractGenerator();
            }

            return(codeGenerator);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the <see cref="CodeCompileUnit"/> based on the provide context.
        /// </summary>
        /// <param name="codeGeneratorContext">The code generator context.</param>
        public CodeCompileUnit GenerateDataContractCode(ICodeGeneratorContext codeGeneratorContext)
        {
            CodeCompileUnit codeCompileUnit = CodeExtension.GenerateDataContractCode(codeGeneratorContext.XmlSchemas);

            CodeExtension.RemoveDefaultTypes(codeCompileUnit);

            codeCompileUnit.ImplementsBaijiSerialization(codeGeneratorContext);

            if (codeGeneratorContext.CodeGenOptions.AddCustomRequestInterface)
            {
                DataContractGenerator.BuildElementName2TypeNameMapping(codeGeneratorContext);
                DataContractGenerator.ImplementCustomInterface(codeGeneratorContext, codeCompileUnit);
            }

            if (codeGeneratorContext.CodeGenOptions.ForceElementNamespace)
            {
                DataContractGenerator.BuildElementName2TargetNamespaceMapping(codeGeneratorContext);
            }

            return(codeCompileUnit);
        }
        /// <summary>
        /// Generates the <see cref="CodeNamespace"/> based on the provide context.
        /// </summary>
        /// <param name="codeGeneratorContext">The code generator context.</param>
        public CodeNamespace GenerateCode(ICodeGeneratorContext codeGeneratorContext)
        {
            CodeGenOptions codeGenOptions = codeGeneratorContext.CodeGenOptions;
            XmlSchemas     xmlSchemas     = codeGeneratorContext.XmlSchemas;

            CodeNamespace codeNamespace = new CodeNamespace();

            // Generate DataContracts
            const CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateProperties;
            var exporter = new XmlCodeExporter(codeNamespace);
            var importer = new XmlSchemaImporter(xmlSchemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

            // TypeName to XmlSchemaType mapping
            IDictionary <string, XmlSchemaType>    typeName2schemaTypeMapping         = codeGeneratorContext.TypeName2schemaTypeMapping;
            IDictionary <XmlQualifiedName, string> elementName2TypeNameMapping        = codeGeneratorContext.ElementName2TypeNameMapping;
            IDictionary <string, string>           elementName2TargetNamespaceMapping = codeGeneratorContext.ElementName2TargetNamespaceMapping;

            foreach (XmlSchema schema in xmlSchemas)
            {
                foreach (XmlSchemaElement element in schema.Elements.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportTypeMapping(element.QualifiedName);
                    if (element.IsAbstract)
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    if (typeMapping.XsdTypeName == "anyType")
                    {
                        continue;                                       // ignore no type element
                    }
                    if (string.IsNullOrWhiteSpace(typeMapping.XsdTypeName))
                    {
                        throw new Exception("Cannot use anonymous type for Request/Response element: " + element.Name + ".");
                    }
                    typeName2schemaTypeMapping[typeMapping.XsdTypeName] = element.ElementSchemaType;
                    elementName2TypeNameMapping[element.QualifiedName]  = typeMapping.XsdTypeName;

                    elementName2TargetNamespaceMapping[element.QualifiedName.Name] = schema.TargetNamespace;
                }

                foreach (XmlSchemaType complexType in schema.SchemaTypes.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportSchemaType(complexType.QualifiedName);
                    if (DataContractGenerator.CouldBeAnArray(complexType))
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = complexType;
                }
            }

            if (codeNamespace.Types.Count == 0)
            {
                throw new Exception("No types were generated.");
            }

            // Build type name to code type declaration mapping
            codeGeneratorContext.CodeTypeMap = DataContractGenerator.BuildCodeTypeMap(codeNamespace);

            // Decorate data contracts code
            ICodeExtension codeExtension = new CodeExtension();

            codeExtension.Process(codeNamespace, codeGeneratorContext);

            codeNamespace.ImplementsBaijiSerialization(codeGeneratorContext);

            // Generate interface code
            string                        wsdlFile          = codeGeneratorContext.CodeGenOptions.MetadataLocation;
            InterfaceContract             interfaceContract = ServiceDescriptionEngine.GetInterfaceContract(wsdlFile);
            CodeTypeDeclaration           codeType;
            CodeNamespaceImportCollection imports;

            this.buildInterfaceCode(codeGeneratorContext, interfaceContract, out codeType, out imports);
            codeNamespace.Types.Add(codeType);
            foreach (CodeNamespaceImport @import in imports)
            {
                codeNamespace.Imports.Add(@import);
            }

            // Import SOA common type namespace before removing code types
            codeNamespace.Imports.Add(new CodeNamespaceImport(Constants.C_SERVICE_STACK_COMMON_TYPES_NAMESPACE));
            // Remove SOA common types since they have already been included in CSerivceStack DLL
            CodeExtension.RemoveSOACommonTypes(codeNamespace);

            CodeExtension.RemoveDefaultTypes(codeNamespace);

            return(codeNamespace);
        }
        /// <summary>
        /// Generates the <see cref="CodeNamespace"/> based on the provide context.
        /// </summary>
        /// <param name="codeGeneratorContext">The code generator context.</param>
        public CodeNamespace[] GenerateCodes(ICodeGeneratorContext codeGeneratorContext)
        {
            CodeGenOptions codeGenOptions = codeGeneratorContext.CodeGenOptions;
            XmlSchemas     xmlSchemas     = codeGeneratorContext.XmlSchemas;

            List <CodeNamespace> codeNamespaces = new List <CodeNamespace>();

            // Generate DataContracts
            const CodeGenerationOptions            generationOptions                  = CodeGenerationOptions.GenerateProperties;
            IDictionary <string, XmlSchemaType>    typeName2schemaTypeMapping         = codeGeneratorContext.TypeName2schemaTypeMapping;
            IDictionary <XmlQualifiedName, string> elementName2TypeNameMapping        = codeGeneratorContext.ElementName2TypeNameMapping;
            IDictionary <string, string>           elementName2TargetNamespaceMapping = codeGeneratorContext.ElementName2TargetNamespaceMapping;

            foreach (XmlSchema schema in xmlSchemas)
            {
                CodeNamespace codeNamespace = new CodeNamespace();
                codeNamespace.UserData.Add(Constants.SCHEMA, schema);

                // TypeName to XmlSchemaType mapping
                XmlCodeExporter   exporter = new XmlCodeExporter(codeNamespace);
                XmlSchemaImporter importer = new XmlSchemaImporter(xmlSchemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

                foreach (XmlSchemaElement element in schema.Elements.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportTypeMapping(element.QualifiedName);
                    if (element.IsAbstract)
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    if (typeMapping.XsdTypeName == "anyType")
                    {
                        continue;                                       // ignore no type element
                    }
                    if (string.IsNullOrWhiteSpace(typeMapping.XsdTypeName))
                    {
                        throw new Exception("Cannot use anonymous type for Request/Response element: " + element.Name + ".");
                    }
                    typeName2schemaTypeMapping[typeMapping.XsdTypeName]            = element.ElementSchemaType;
                    elementName2TypeNameMapping[element.QualifiedName]             = typeMapping.XsdTypeName;
                    elementName2TargetNamespaceMapping[element.QualifiedName.Name] = schema.TargetNamespace;
                }

                foreach (XmlSchemaType schemaType in schema.SchemaTypes.Values)
                {
                    if (String.IsNullOrWhiteSpace(schemaType.SourceUri))
                    {
                        schemaType.SourceUri = schema.SourceUri;
                    }
                    XmlTypeMapping typeMapping = importer.ImportSchemaType(schemaType.QualifiedName);
                    if (DataContractGenerator.CouldBeAnArray(schemaType))
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = schemaType;
                }

                if (codeNamespace.Types.Count > 0)
                {
                    codeNamespaces.Add(codeNamespace);
                }
            }

            if (codeNamespaces.Count == 0)
            {
                throw new Exception("No types were generated.");
            }

            // Build type name to code type declaration mapping
            codeGeneratorContext.CodeTypeMap = DataContractGenerator.BuildCodeTypeMap(codeNamespaces.ToArray());

            for (int i = 0; i < codeNamespaces.Count; i++)
            {
                CodeNamespace codeNamespace = codeNamespaces[i];
                // Decorate data contracts code
                ICodeExtension codeExtension = new CodeExtension();
                codeExtension.Process(codeNamespace, codeGeneratorContext);

                // Import SOA common type namespace before removing code types
                codeNamespace.Imports.Add(new CodeNamespaceImport(Constants.C_SERVICE_STACK_COMMON_TYPES_NAMESPACE));
                // Remove SOA common types since they have already been included in CSerivceStack DLL
                CodeExtension.RemoveSOACommonTypes(codeNamespace);

                CodeExtension.RemoveDefaultTypes(codeNamespace);

                XmlSchema schema = codeNamespace.UserData[Constants.SCHEMA] as XmlSchema;
                List <CodeTypeDeclaration> types = new List <CodeTypeDeclaration>();
                foreach (XmlSchemaType schemaType in schema.SchemaTypes.Values)
                {
                    foreach (CodeTypeDeclaration codeType in codeNamespace.Types)
                    {
                        if (codeType.Name == schemaType.Name &&
                            schema.SourceUri == schemaType.SourceUri)
                        {
                            types.Add(codeType);
                        }
                    }
                }
                codeNamespace.Types.Clear();
                codeNamespace.Types.AddRange(types.ToArray());
                if (codeNamespace.Types.Count == 0)
                {
                    codeNamespaces.RemoveAt(i--);
                }
            }

            codeNamespaces.ImplementsBaijiSerialization(codeGeneratorContext);

            //Add Interface CodeNamespace
            CodeNamespace interfaceNamespace = new CodeNamespace(codeGenOptions.ClrNamespace);
            // Generate interface code
            string                        wsdlFile          = codeGeneratorContext.CodeGenOptions.MetadataLocation;
            InterfaceContract             interfaceContract = ServiceDescriptionEngine.GetInterfaceContract(wsdlFile);
            CodeTypeDeclaration           interfaceType;
            CodeNamespaceImportCollection imports;

            this.buildInterfaceCode(codeGeneratorContext, interfaceContract, out interfaceType, out imports);
            interfaceNamespace.Types.Add(interfaceType);
            foreach (CodeNamespaceImport @import in imports)
            {
                interfaceNamespace.Imports.Add(@import);
            }

            // Import SOA common type namespace before removing code types
            interfaceNamespace.Imports.Add(new CodeNamespaceImport(Constants.C_SERVICE_STACK_COMMON_TYPES_NAMESPACE));
            // Remove SOA common types since they have already been included in CSerivceStack DLL
            CodeExtension.RemoveSOACommonTypes(interfaceNamespace);
            CodeExtension.RemoveDefaultTypes(interfaceNamespace);

            string fileName = null;

            if (codeGeneratorContext.CodeGenOptions.CodeGeneratorMode == CodeGeneratorMode.Service)
            {
                fileName = "I" + interfaceContract.ServiceName.Replace("Interface", string.Empty);
            }
            else
            {
                fileName = interfaceContract.ServiceName.Replace("Interface", string.Empty) + "Client";
            }
            interfaceNamespace.UserData.Add(Constants.FILE_NAME, fileName);

            codeNamespaces.Add(interfaceNamespace);

            return(codeNamespaces.ToArray());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Generates the <see cref="CodeNamespace"/> based on the provide context.
        /// </summary>
        /// <param name="codeGeneratorContext">The code generator context.</param>
        public CodeNamespace[] GenerateCodes(ICodeGeneratorContext codeGeneratorContext)
        {
            CodeGenOptions codeGenOptions = codeGeneratorContext.CodeGenOptions;
            XmlSchemas     xmlSchemas     = codeGeneratorContext.XmlSchemas;

            // Generate DataContracts
            const CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateProperties;

            List <CodeNamespace> codeNamespaces = new List <CodeNamespace>();

            // TypeName to XmlSchemaType mapping
            IDictionary <string, XmlSchemaType> typeName2schemaTypeMapping = codeGeneratorContext.TypeName2schemaTypeMapping;

            foreach (XmlSchema schema in xmlSchemas)
            {
                CodeNamespace codeNamespace = new CodeNamespace();
                codeNamespace.UserData.Add(Constants.SCHEMA, schema);

                XmlCodeExporter   exporter = new XmlCodeExporter(codeNamespace);
                XmlSchemaImporter importer = new XmlSchemaImporter(xmlSchemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

                foreach (XmlSchemaElement element in schema.Elements.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportTypeMapping(element.QualifiedName);
                    if (element.IsAbstract)
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = element.ElementSchemaType;
                }

                foreach (XmlSchemaType schemaType in schema.SchemaTypes.Values)
                {
                    if (String.IsNullOrWhiteSpace(schemaType.SourceUri))
                    {
                        schemaType.SourceUri = schema.SourceUri;
                    }
                    XmlTypeMapping typeMapping = importer.ImportSchemaType(schemaType.QualifiedName);
                    if (CouldBeAnArray(schemaType))
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = schemaType;
                }

                if (codeNamespace.Types.Count > 0)
                {
                    codeNamespaces.Add(codeNamespace);
                }
            }

            if (codeNamespaces.Count == 0)
            {
                throw new Exception("No types were generated.");
            }
            // Build type name to code type declaration mapping
            codeGeneratorContext.CodeTypeMap = DataContractGenerator.BuildCodeTypeMap(codeNamespaces.ToArray());
            if (codeGenOptions.ForceElementName)
            {
                DataContractGenerator.BuildElementName2TypeNameMapping(codeGeneratorContext);
            }
            if (codeGenOptions.ForceElementNamespace)
            {
                DataContractGenerator.BuildElementName2TargetNamespaceMapping(codeGeneratorContext);
            }

            for (int i = 0; i < codeNamespaces.Count; i++)
            {
                CodeNamespace codeNamespace = codeNamespaces[i];
                // Decorate data contracts code
                ICodeExtension codeExtension = new CodeExtension();
                codeExtension.Process(codeNamespace, codeGeneratorContext);

                CodeExtension.RemoveDefaultTypes(codeNamespace);

                List <CodeTypeDeclaration> types = new List <CodeTypeDeclaration>();
                XmlSchema schema = codeNamespace.UserData[Constants.SCHEMA] as XmlSchema;

                foreach (XmlSchemaType schemaType in schema.SchemaTypes.Values)
                {
                    foreach (CodeTypeDeclaration codeType in codeNamespace.Types)
                    {
                        if (codeType.Name == schemaType.Name &&
                            schema.SourceUri == schemaType.SourceUri)
                        {
                            types.Add(codeType);
                        }
                    }
                }
                codeNamespace.Types.Clear();
                codeNamespace.Types.AddRange(types.ToArray());

                if (codeNamespace.Types.Count == 0)
                {
                    codeNamespaces.RemoveAt(i--);
                }
            }

            return(codeNamespaces.ToArray());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates the <see cref="CodeNamespace"/> based on the provide context.
        /// </summary>
        /// <param name="codeGeneratorContext">The code generator context.</param>
        public CodeNamespace GenerateCode(ICodeGeneratorContext codeGeneratorContext)
        {
            CodeGenOptions codeGenOptions = codeGeneratorContext.CodeGenOptions;
            XmlSchemas     xmlSchemas     = codeGeneratorContext.XmlSchemas;

            CodeNamespace codeNamespace = new CodeNamespace();

            // Generate DataContracts
            const CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateProperties;
            var exporter = new XmlCodeExporter(codeNamespace);
            var importer = new XmlSchemaImporter(xmlSchemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

            // TypeName to XmlSchemaType mapping
            IDictionary <string, XmlSchemaType> typeName2schemaTypeMapping = codeGeneratorContext.TypeName2schemaTypeMapping;

            foreach (XmlSchema schema in xmlSchemas)
            {
                foreach (XmlSchemaElement element in schema.Elements.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportTypeMapping(element.QualifiedName);
                    if (element.IsAbstract)
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = element.ElementSchemaType;
                }

                foreach (XmlSchemaType complexType in schema.SchemaTypes.Values)
                {
                    XmlTypeMapping typeMapping = importer.ImportSchemaType(complexType.QualifiedName);
                    if (CouldBeAnArray(complexType))
                    {
                        continue;
                    }

                    exporter.ExportTypeMapping(typeMapping);
                    typeName2schemaTypeMapping[typeMapping.TypeName] = complexType;
                }
            }

            if (codeNamespace.Types.Count == 0)
            {
                throw new Exception("No types were generated.");
            }

            // Build type name to code type declaration mapping
            codeGeneratorContext.CodeTypeMap = BuildCodeTypeMap(codeNamespace);
            if (codeGenOptions.ForceElementName)
            {
                DataContractGenerator.BuildElementName2TypeNameMapping(codeGeneratorContext);
            }
            if (codeGenOptions.ForceElementNamespace)
            {
                DataContractGenerator.BuildElementName2TargetNamespaceMapping(codeGeneratorContext);
            }

            // Decorate data contracts code
            ICodeExtension codeExtension = new CodeExtension();

            codeExtension.Process(codeNamespace, codeGeneratorContext);

            CodeExtension.RemoveDefaultTypes(codeNamespace);

            return(codeNamespace);
        }