Ejemplo n.º 1
0
        private static IEnumerable <FeatureDescriptor> GetFeaturesForExtension(IDictionary <string, string> manifest, ExtensionDescriptor extensionDescriptor)
        {
            var featureDescriptors = new List <FeatureDescriptor>();

            // Default feature
            FeatureDescriptor defaultFeature = new FeatureDescriptor {
                Id       = extensionDescriptor.Id,
                Name     = GetValue(manifest, FeatureNameSection) ?? extensionDescriptor.Name,
                Priority = GetValue(manifest, PrioritySection) != null?int.Parse(GetValue(manifest, PrioritySection)) : 0,
                               Description  = GetValue(manifest, FeatureDescriptionSection) ?? GetValue(manifest, DescriptionSection) ?? string.Empty,
                               Dependencies = ParseFeatureDependenciesEntry(GetValue(manifest, DependenciesSection)),
                               Extension    = extensionDescriptor,
                               Category     = GetValue(manifest, CategorySection)
            };

            featureDescriptors.Add(defaultFeature);

            // Remaining features
            string featuresText = GetValue(manifest, FeaturesSection);

            if (featuresText != null)
            {
                FeatureDescriptor featureDescriptor = null;
                using (StringReader reader = new StringReader(featuresText)) {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (IsFeatureDeclaration(line))
                        {
                            if (featureDescriptor != null)
                            {
                                if (!featureDescriptor.Equals(defaultFeature))
                                {
                                    featureDescriptors.Add(featureDescriptor);
                                }

                                featureDescriptor = null;
                            }

                            string[] featureDeclaration  = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                            string   featureDescriptorId = featureDeclaration[0].Trim();
                            if (String.Equals(featureDescriptorId, extensionDescriptor.Id, StringComparison.OrdinalIgnoreCase))
                            {
                                featureDescriptor      = defaultFeature;
                                featureDescriptor.Name = extensionDescriptor.Name;
                            }
                            else
                            {
                                featureDescriptor = new FeatureDescriptor {
                                    Id        = featureDescriptorId,
                                    Extension = extensionDescriptor
                                };
                            }
                        }
                        else if (IsFeatureFieldDeclaration(line))
                        {
                            if (featureDescriptor != null)
                            {
                                string[] featureField       = line.Split(new[] { ":" }, 2, StringSplitOptions.None);
                                int      featureFieldLength = featureField.Length;
                                if (featureFieldLength != 2)
                                {
                                    continue;
                                }
                                for (int i = 0; i < featureFieldLength; i++)
                                {
                                    featureField[i] = featureField[i].Trim();
                                }

                                switch (featureField[0].ToLowerInvariant())
                                {
                                case NameSection:
                                    featureDescriptor.Name = featureField[1];
                                    break;

                                case DescriptionSection:
                                    featureDescriptor.Description = featureField[1];
                                    break;

                                case CategorySection:
                                    featureDescriptor.Category = featureField[1];
                                    break;

                                case PrioritySection:
                                    featureDescriptor.Priority = int.Parse(featureField[1]);
                                    break;

                                case DependenciesSection:
                                    featureDescriptor.Dependencies = ParseFeatureDependenciesEntry(featureField[1]);
                                    break;
                                }
                            }
                            else
                            {
                                string message = string.Format("The line {0} in manifest for extension {1} was ignored", line, extensionDescriptor.Id);
                                throw new ArgumentException(message);
                            }
                        }
                        else
                        {
                            string message = string.Format("The line {0} in manifest for extension {1} was ignored", line, extensionDescriptor.Id);
                            throw new ArgumentException(message);
                        }
                    }

                    if (featureDescriptor != null && !featureDescriptor.Equals(defaultFeature))
                    {
                        featureDescriptors.Add(featureDescriptor);
                    }
                }
            }

            return(featureDescriptors);
        }
        private static IEnumerable <FeatureDescriptor> GetFeaturesForExtension(IDictionary <string, string> manifest, ExtensionDescriptorEntry entry)
        {
            var featureDescriptors = new List <FeatureDescriptor>();

            var descriptor = entry.Descriptor;
            //默认特性
            var defaultFeature = new FeatureDescriptor
            {
                Id       = entry.Id,
                Name     = GetValue(manifest, FeatureNameSection) ?? descriptor.Name,
                Priority = GetValue(manifest, PrioritySection) != null?int.Parse(GetValue(manifest, PrioritySection)) : 0,
                               Description  = GetValue(manifest, FeatureDescriptionSection) ?? GetValue(manifest, DescriptionSection) ?? string.Empty,
                               Dependencies = ParseFeatureDependenciesEntry(GetValue(manifest, DependenciesSection)),
                               Extension    = entry,
                               Category     = GetValue(manifest, CategorySection)
            };

            featureDescriptors.Add(defaultFeature);

            //剩余特性
            var featuresText = GetValue(manifest, FeaturesSection);

            if (string.IsNullOrWhiteSpace(featuresText))
            {
                return(featureDescriptors);
            }
            FeatureDescriptor featureDescriptor = null;

            using (var reader = new StringReader(featuresText))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (IsFeatureDeclaration(line))
                    {
                        if (featureDescriptor != null)
                        {
                            if (!featureDescriptor.Equals(defaultFeature))
                            {
                                featureDescriptors.Add(featureDescriptor);
                            }
                        }

                        var featureDeclaration  = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                        var featureDescriptorId = featureDeclaration[0].Trim();
                        if (string.Equals(featureDescriptorId, entry.Id, StringComparison.OrdinalIgnoreCase))
                        {
                            featureDescriptor      = defaultFeature;
                            featureDescriptor.Name = descriptor.Name;
                        }
                        else
                        {
                            featureDescriptor = new FeatureDescriptor
                            {
                                Id        = featureDescriptorId,
                                Extension = entry
                            };
                        }
                    }
                    else if (IsFeatureFieldDeclaration(line))
                    {
                        if (featureDescriptor != null)
                        {
                            var featureField       = line.Split(new[] { ":" }, 2, StringSplitOptions.None);
                            var featureFieldLength = featureField.Length;
                            if (featureFieldLength != 2)
                            {
                                continue;
                            }
                            for (var i = 0; i < featureFieldLength; i++)
                            {
                                featureField[i] = featureField[i].Trim();
                            }

                            switch (featureField[0].ToLowerInvariant())
                            {
                            case NameSection:
                                featureDescriptor.Name = featureField[1];
                                break;

                            case DescriptionSection:
                                featureDescriptor.Description = featureField[1];
                                break;

                            case CategorySection:
                                featureDescriptor.Category = featureField[1];
                                break;

                            case PrioritySection:
                                featureDescriptor.Priority = int.Parse(featureField[1]);
                                break;

                            case DependenciesSection:
                                featureDescriptor.Dependencies = ParseFeatureDependenciesEntry(featureField[1]);
                                break;
                            }
                        }
                        else
                        {
                            var message = string.Format("行 {0} 在清单文件中被忽略,来自扩展 {1}", line, entry.Id);
                            throw new ArgumentException(message);
                        }
                    }
                    else
                    {
                        var message = string.Format("行 {0} 在清单文件中被忽略,来自扩展 {1}", line, entry.Id);
                        throw new ArgumentException(message);
                    }
                }

                if (featureDescriptor != null && !featureDescriptor.Equals(defaultFeature))
                {
                    featureDescriptors.Add(featureDescriptor);
                }
            }

            return(featureDescriptors);
        }