Exemplo n.º 1
0
        /// <summary>
        ///     Generates the template instances.
        /// </summary>
        public static int GenerateTemplateInstances(List <string> xmlFiles, List <string> templateFiles, string outputPath, string namespaceName)
        {
            IEnumerable <Entity> entities = XmlParser.ReadEntities(xmlFiles);

            /////
            // Construct the alias and schema resolver instances.
            /////
            IList <Entity> entityList = entities as IList <Entity> ?? entities.ToList( );

            Decorator.DecorateEntities(entityList);

            IAliasResolver aliasResolver  = new EntityStreamAliasResolver(entityList);
            var            schemaResolver = new SchemaResolver(entityList, aliasResolver);

            schemaResolver.Initialize( );

            /////
            // Set the required values into the static model class.
            /////
            SetTemplateParameters(namespaceName, schemaResolver, aliasResolver);

            /////
            // Create the transform engine.
            /////
            var engine = new Engine( );

            var host = new TransformHost( );

            /////
            // Method return code.
            /////
            int returnCode = 0;

            /////
            // Cycle through the template files.
            /////
            foreach (string templateFile in templateFiles)
            {
                try
                {
                    var fi = new FileInfo(templateFile);

                    host.TemplateFileValue = fi.Name;

                    if (templateFile.Contains("Combined"))
                    {
                        AcceptAnyTypePrefix();

                        GenerateResultFile(outputPath, engine, host, templateFile, "");
                    }
                    else
                    {
                        ResetTypePrefix();

                        while (MoveToNextTypePrefix())
                        {
                            string outputSuffix = "." + CurrentTypePrefix().ToString().ToUpperInvariant();
                            GenerateResultFile(outputPath, engine, host, templateFile, outputSuffix);
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine("Failed to generate instance for template '{0}'. Exception: {1}", templateFile, exc.Message);

                    returnCode = 1;
                }
            }

            return(returnCode);
        }