Esempio n. 1
0
        /// <summary>
        /// Get <see cref="ICodeExtension"/> code generator extension
        /// </summary>
        /// <param name="generatorParams">Generator parameters</param>
        /// <returns><see cref="ICodeExtension"/></returns>
        internal static Result <ICodeExtension> GetCodeExtension(GeneratorParams generatorParams)
        {
            var extention = new Result <ICodeExtension>(GeneratorFactory.GetExtention(generatorParams.TargetFramework), true);

            if (extention.Entity != null)
            {
                return(extention);
            }

            var types = Assembly.GetExecutingAssembly().GetTypes()
                        .Select(type => new
            {
                Type = type,
                TargetFrameworkAttributes =
                    type.GetCustomAttributes(typeof(CodeExtensionAttribute), true)
            })

                        .Where(o =>
                               o.TargetFrameworkAttributes.Length > 0 &&
                               o.TargetFrameworkAttributes
                               .Where(attr =>
                                      ((CodeExtensionAttribute)attr).TargetFramework == generatorParams.TargetFramework)
                               .Count() > 0)
                        .ToList();

            if (types.Count == 1)
            {
                try
                {
                    return(new Result <ICodeExtension>(Activator.CreateInstance(types[0].Type) as ICodeExtension, true));
                }
                catch (Exception ex)
                {
                    return(new Result <ICodeExtension>(null, false, ex.Message, MessageType.Error));
                }
            }

            return(new Result <ICodeExtension>(null, false,
                                               string.Format(Resources.UnsupportedTargetFramework,
                                                             generatorParams.TargetFramework),
                                               MessageType.Error));
        }
Esempio n. 2
0
        /// <summary>
        /// Initiate code generation process
        /// </summary>
        /// <param name="generatorParams">Generator parameters</param>
        /// <returns></returns>
        internal static Result <CodeNamespace> Process(GeneratorParams generatorParams)
        {
            var ns = new CodeNamespace();

            XmlReader reader = null;

            try
            {
                #region Set generation context

                GeneratorContext.GeneratorParams = generatorParams;

                #endregion

                #region Get XmlTypeMapping

                XmlSchema xsd;
                var       schemas = new XmlSchemas();

                reader = XmlReader.Create(generatorParams.InputFilePath);
                xsd    = XmlSchema.Read(reader, new ValidationEventHandler(Validate));

                var schemaSet = new XmlSchemaSet();
                schemaSet.Add(xsd);
                schemaSet.Compile();

                foreach (XmlSchema schema in schemaSet.Schemas())
                {
                    schemas.Add(schema);
                }

                const CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateOrder;
                var exporter = new XmlCodeExporter(ns);
                var importer = new XmlSchemaImporter(schemas, generationOptions, new ImportContext(new CodeIdentifiers(), false));

                foreach (XmlSchemaElement element in xsd.Elements.Values)
                {
                    var mapping = importer.ImportTypeMapping(element.QualifiedName);
                    exporter.ExportTypeMapping(mapping);
                }

                //Fixes/handles http://xsd2code.codeplex.com/WorkItem/View.aspx?WorkItemId=6941
                foreach (XmlSchemaComplexType complex in xsd.Items.OfType <XmlSchemaComplexType>())
                {
                    var mapping = importer.ImportSchemaType(complex.QualifiedName);
                    exporter.ExportTypeMapping(mapping);
                }

                #endregion

                #region Execute extensions

                var getExtensionResult = GeneratorFactory.GetCodeExtension(generatorParams);
                if (!getExtensionResult.Success)
                {
                    return(new Result <CodeNamespace>(ns, false, getExtensionResult.Messages));
                }

                var ext = getExtensionResult.Entity;
                ext.Process(ns, xsd);

                #endregion Execute extensions

                return(new Result <CodeNamespace>(ns, true));
            }
            catch (Exception e)
            {
                return(new Result <CodeNamespace>(ns, false, e.Message, MessageType.Error));
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }