public DeclarationSectionReader(IXmlLoadLogger logger, FeatureSwitcher featureSwitcher, IConditionParser conditionParser)
 {
     this.logger          = logger;
     this.featureSwitcher = featureSwitcher;
     this.conditionParser = conditionParser;
 }
示例#2
0
        public bool Load(Stream stream, IXmlLoadLogger logger, out ComponentDescription description)
        {
            description = new ComponentDescription();

            if (stream is FileStream fs)
            {
                description.Source = new ComponentDescriptionSource(fs.Name);
            }

            var errorCheckingLogger = new ErrorCheckingLogger(logger);
            var sectionRegistry     = new SectionRegistry();

            try
            {
                var root = XElement.Load(stream, LoadOptions.SetLineInfo);
                ReadHeader(root, description);

                var featureSwitcher = new FeatureSwitcher();

                // Read declaration
                var declaration = root.Element(ComponentNamespace + "declaration");
                if (declaration == null)
                {
                    logger.LogError(root, "Missing required element: 'declaration'");
                    return(false);
                }
                var declarationReader = container.Value.ResolveNamed <IXmlSectionReader>(declaration.Name.NamespaceName + ":" + declaration.Name.LocalName,
                                                                                         new TypedParameter(typeof(IXmlLoadLogger), errorCheckingLogger),
                                                                                         new TypedParameter(typeof(FeatureSwitcher), featureSwitcher));
                declarationReader.ReadSection(declaration, description);

                var descriptionInstance = description;
                var scope = container.Value.BeginLifetimeScope(configure =>
                {
                    configure.RegisterInstance(errorCheckingLogger).As <IXmlLoadLogger>();
                    configure.RegisterInstance(sectionRegistry).As <ISectionRegistry>();
                    configure.RegisterInstance(descriptionInstance);
                    configure.RegisterInstance(featureSwitcher).As <IFeatureSwitcher>();

                    foreach (var feature in features)
                    {
                        if (featureSwitcher.IsFeatureEnabled(feature.Key, out var featureSourceElement))
                        {
                            logger.Log(LogLevel.Information, featureSourceElement, $"Feature enabled: {feature.Key}");
                            feature.Value(configure);
                        }
                    }
                });

                try
                {
                    // Read remaining elements
                    foreach (var element in root.Elements().Except(new[] { declaration }))
                    {
                        var sectionReader = scope.ResolveOptionalNamed <IXmlSectionReader>(element.Name.NamespaceName + ":" + element.Name.LocalName);
                        sectionReader?.ReadSection(element, description);
                    }

                    return(!errorCheckingLogger.HasErrors);
                }
                finally
                {
                    scope.Dispose();
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, new FileRange(1, 1, 1, 2), ex.Message, ex);
                return(false);
            }
        }