示例#1
0
        public Stream ToFormattedTemplate(ProvisioningTemplate template)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            using (var scope = new PnPSerializationScope(typeof(TSchemaTemplate)))
            {
                var result = new TSchemaTemplate();

                // Create the wrapper
                var    wrapperType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Provisioning, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
                Object wrapper     = Activator.CreateInstance(wrapperType);

                // Create the Preferences
                var    preferencesType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Preferences, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
                Object preferences     = Activator.CreateInstance(preferencesType);

                wrapper.GetType().GetProperty("Preferences",
                                              System.Reflection.BindingFlags.Instance |
                                              System.Reflection.BindingFlags.Public |
                                              System.Reflection.BindingFlags.IgnoreCase).SetValue(wrapper, preferences);

                // Handle the Parameters of the schema wrapper, if any
                var tps = new TemplateParametersSerializer();
                tps.Serialize(template, wrapper);

                // Handle the Localizations of the schema wrapper, if any
                var ls = new LocalizationsSerializer();
                ls.Serialize(template, wrapper);

                // Handle the Tenant-wide of the schema wrapper, if any
                var ts = new TenantSerializer();
                ts.Serialize(template, wrapper);

                // Configure the Generator
                preferences.GetType().GetProperty("Generator",
                                                  System.Reflection.BindingFlags.Instance |
                                                  System.Reflection.BindingFlags.Public |
                                                  System.Reflection.BindingFlags.IgnoreCase).SetValue(preferences, this.GetType().Assembly.FullName);

                // Configure the output Template
                var templatesType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Templates, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
                var templates     = Array.CreateInstance(templatesType, 1);
                var templatesItem = Activator.CreateInstance(templatesType);

                templatesItem.GetType().GetProperty("ID",
                                                    System.Reflection.BindingFlags.Instance |
                                                    System.Reflection.BindingFlags.Public |
                                                    System.Reflection.BindingFlags.IgnoreCase).SetValue(templatesItem, $"CONTAINER-{template.Id}");

                var provisioningTemplates = Array.CreateInstance(typeof(TSchemaTemplate), 1);
                provisioningTemplates.SetValue(result, 0);

                templatesItem.GetType().GetProperty("ProvisioningTemplate",
                                                    System.Reflection.BindingFlags.Instance |
                                                    System.Reflection.BindingFlags.Public |
                                                    System.Reflection.BindingFlags.IgnoreCase).SetValue(templatesItem, provisioningTemplates);

                templates.SetValue(templatesItem, 0);

                wrapperType.GetProperty("Templates",
                                        System.Reflection.BindingFlags.Instance |
                                        System.Reflection.BindingFlags.Public |
                                        System.Reflection.BindingFlags.IgnoreCase).SetValue(wrapper, templates);

                SerializeTemplate(template, result);

                XmlSerializerNamespaces ns =
                    new XmlSerializerNamespaces();
                ns.Add(((IXMLSchemaFormatter)this).NamespacePrefix,
                       ((IXMLSchemaFormatter)this).NamespaceUri);

                MemoryStream  output        = new MemoryStream();
                XmlSerializer xmlSerializer = new XmlSerializer(wrapperType);
                if (ns != null)
                {
                    xmlSerializer.Serialize(output, wrapper, ns);
                }
                else
                {
                    xmlSerializer.Serialize(output, wrapper);
                }

                output.Position = 0;
                return(output);
            }
        }
示例#2
0
        private void ProcessOutputHierarchy(ProvisioningTemplate template, out Type wrapperType, out object wrapper, out Array templates, out object templatesItem)
        {
            // Create the wrapper
            wrapperType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Provisioning, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
            wrapper     = Activator.CreateInstance(wrapperType);

            // Create the Preferences
            var    preferencesType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Preferences, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
            Object preferences     = Activator.CreateInstance(preferencesType);

            wrapper.GetType().GetProperty("Preferences",
                                          System.Reflection.BindingFlags.Instance |
                                          System.Reflection.BindingFlags.Public |
                                          System.Reflection.BindingFlags.IgnoreCase).SetValue(wrapper, preferences);

            // Handle the Parameters of the schema wrapper, if any
            var tps = new TemplateParametersSerializer();

            tps.Serialize(template, wrapper);

            // Handle the Localizations of the schema wrapper, if any
            var ls = new LocalizationsSerializer();

            ls.Serialize(template, wrapper);

            // Handle the Tenant-wide of the schema wrapper, if any
            var ts = new TenantSerializer();

            ts.Serialize(template, wrapper);

            // Configure the basic properties of the wrapper
            if (template.ParentHierarchy != null)
            {
                var author = wrapper.GetType().GetProperty("Author",
                                                           System.Reflection.BindingFlags.Instance |
                                                           System.Reflection.BindingFlags.Public |
                                                           System.Reflection.BindingFlags.IgnoreCase);
                if (author != null)
                {
                    author.SetValue(wrapper,
                                    template.ParentHierarchy.Author);
                }
                var displayName = wrapper.GetType().GetProperty("DisplayName",
                                                                System.Reflection.BindingFlags.Instance |
                                                                System.Reflection.BindingFlags.Public |
                                                                System.Reflection.BindingFlags.IgnoreCase);
                if (displayName != null)
                {
                    displayName.SetValue(wrapper,
                                         template.ParentHierarchy.DisplayName);
                }
                var description = wrapper.GetType().GetProperty("Description",
                                                                System.Reflection.BindingFlags.Instance |
                                                                System.Reflection.BindingFlags.Public |
                                                                System.Reflection.BindingFlags.IgnoreCase);
                if (description != null)
                {
                    description.SetValue(wrapper,
                                         template.ParentHierarchy.Description);
                }
                var imagePreviewUrl = wrapper.GetType().GetProperty("ImagePreviewUrl",
                                                                    System.Reflection.BindingFlags.Instance |
                                                                    System.Reflection.BindingFlags.Public |
                                                                    System.Reflection.BindingFlags.IgnoreCase);
                if (imagePreviewUrl != null)
                {
                    imagePreviewUrl.SetValue(wrapper,
                                             template.ParentHierarchy.ImagePreviewUrl);
                }
                var generator = wrapper.GetType().GetProperty("Generator",
                                                              System.Reflection.BindingFlags.Instance |
                                                              System.Reflection.BindingFlags.Public |
                                                              System.Reflection.BindingFlags.IgnoreCase);
                if (generator != null)
                {
                    generator.SetValue(wrapper,
                                       template.ParentHierarchy.Generator);
                }
                var version = wrapper.GetType().GetProperty("Version",
                                                            System.Reflection.BindingFlags.Instance |
                                                            System.Reflection.BindingFlags.Public |
                                                            System.Reflection.BindingFlags.IgnoreCase);
                if (version != null)
                {
                    version.SetValue(wrapper,
                                     (Decimal)template.ParentHierarchy.Version);
                }
            }

            // Configure the Generator
            preferences.GetType().GetProperty("Generator",
                                              System.Reflection.BindingFlags.Instance |
                                              System.Reflection.BindingFlags.Public |
                                              System.Reflection.BindingFlags.IgnoreCase).SetValue(preferences, this.GetType().Assembly.FullName);

            // Configure the output Template
            var templatesType = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Templates, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);

            templates     = Array.CreateInstance(templatesType, 1);
            templatesItem = Activator.CreateInstance(templatesType);
            templatesItem.GetType().GetProperty("ID",
                                                System.Reflection.BindingFlags.Instance |
                                                System.Reflection.BindingFlags.Public |
                                                System.Reflection.BindingFlags.IgnoreCase).SetValue(templatesItem, $"CONTAINER-{template.Id}");
        }