public void Constructor_WhenArgumentsValid_SetsProperties()
        {
            var traits = new FileTypeRecognizerTraits("id", "description");

            Assert.AreEqual("id", traits.Id);
            Assert.AreEqual("description", traits.Description);
        }
Пример #2
0
        private void Initialize(IEnumerable <ComponentHandle <IFileTypeRecognizer, FileTypeRecognizerTraits> > fileTypeRecognizerHandles)
        {
            foreach (var handle in fileTypeRecognizerHandles)
            {
                string id = handle.GetTraits().Id;
                if (fileTypeInfos.ContainsKey(id))
                {
                    throw new InvalidOperationException(string.Format("There appear to be multiple file types registered with id '{0}'.", id));
                }

                fileTypeInfos.Add(id, new FileTypeInfo()
                {
                    RecognizerHandle = handle
                });
            }

            foreach (var fileTypeInfo in fileTypeInfos.Values)
            {
                FileTypeRecognizerTraits recognizerTraits = fileTypeInfo.RecognizerHandle.GetTraits();

                if (recognizerTraits.FileNameRegex != null)
                {
                    fileTypeInfo.FileNameRegex = new Regex(recognizerTraits.FileNameRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                }

                if (recognizerTraits.ContentsRegex != null)
                {
                    fileTypeInfo.ContentsRegex = new Regex(recognizerTraits.ContentsRegex, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                }

                string superTypeId = recognizerTraits.SuperTypeId;
                if (superTypeId != null)
                {
                    FileTypeInfo superTypeInfo;
                    if (!fileTypeInfos.TryGetValue(superTypeId, out superTypeInfo))
                    {
                        throw new InvalidOperationException(string.Format("File type '{0}' refers to super type '{1}' but the super type is not registered.", recognizerTraits.Id, superTypeId));
                    }

                    if (fileTypeInfo.IsSameOrSubtype(superTypeInfo))
                    {
                        throw new InvalidOperationException(string.Format("File type '{0}' contains a circular reference to super type '{1}'.", recognizerTraits.Id, superTypeId));
                    }

                    superTypeInfo.AddSubtype(fileTypeInfo);
                }
                else
                {
                    rootFileTypeInfos.Add(fileTypeInfo);
                }
            }

            GenerateFileTypes(rootFileTypeInfos, null);
        }
Пример #3
0
        private void GenerateFileTypes(IEnumerable <FileTypeInfo> fileTypeInfos, FileType superType)
        {
            foreach (var fileTypeInfo in fileTypeInfos)
            {
                FileTypeRecognizerTraits recognizerTraits = fileTypeInfo.RecognizerHandle.GetTraits();
                FileType fileType = new FileType(recognizerTraits.Id, recognizerTraits.Description, superType);
                fileTypes.Add(fileType);

                fileTypeInfo.FileType = fileType;

                GenerateFileTypes(fileTypeInfo.Subtypes, fileType);
            }
        }