Exemplo n.º 1
0
        /// <summary>
        /// Builds the schema. Main list of activities to perform.
        /// </summary>
        private void BuildSchemaImpl(IEnumerable <Entity> entities, IEnumerable <SchemaFile> schemas)
        {
            _namespaceToSchema = schemas.ToDictionary(schema => schema.Namespace);
            _entities          = entities;
            _aliasResolver     = new EntityStreamAliasResolver(entities);
            _schemaManager     = new SchemaResolver(entities, _aliasResolver);
            _schemaManager.Initialize();
            _namesUsed     = new HashSet <XmlQualifiedName>();
            _namesDeclared = new HashSet <XmlQualifiedName>();

            AddRootElement();
            AddRootChildren();

            FindFieldTypes();
            AddFields();
            AddTypes();
            AddRelationships();

            var errors = _namesUsed.Except(_namesDeclared).ToList( );

            if (errors.Count > 0)
            {
                throw new Exception("Names were used but not declared: " + string.Join(", ", errors.Select(qn => string.Format("{0}:{1}", qn.Namespace, qn.Name))));
            }
        }
Exemplo n.º 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);
        }