Пример #1
0
        /// <summary>
        /// Produces an individual output file.
        /// </summary>
        /// <param name="outputPath">The output path.</param>
        /// <param name="engine">The engine.</param>
        /// <param name="host">The host.</param>
        /// <param name="templateFile">The template file.</param>
        /// <param name="outputSuffix">The output suffix.</param>
        private static void GenerateResultFile(string outputPath, Engine engine, TransformHost host, string templateFile, string outputSuffix)
        {
            /////
            // Process the template.
            /////
            string templateText = engine.ProcessTemplate(File.ReadAllText(templateFile), host);

            if (host.Errors != null && host.Errors.Count > 0)
            {
                host.LogErrors(host.Errors);
            }

            if (!string.IsNullOrEmpty(templateText))
            {
                using (var writer = new StreamWriter(Path.Combine(outputPath, Path.GetFileNameWithoutExtension(templateFile) + outputSuffix + host.FileExtension)))
                {
                    /////
                    // Write the template out the file system.
                    /////
                    writer.Write(templateText);
                }
            }
        }
Пример #2
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);
        }