using PixiEditor.Parser; using System.Collections.Generic; using System.IO; using System.Windows.Media.Imaging; namespace PixiEditor.SDK.FileParsers { internal class FileParserList { public List SupportedImageExtensions { get; set; } = new(); public List SupportedDocumentExtensions { get; set; } = new(); public List SupportedExtensions { get; set; } = new(); public ListDictionary ImageParsers { get; set; } = new(); public ListDictionary DocumentParsers { get; set; } = new(); public void AddDocumentParser(DocumentParserInfo info) { foreach (string ext in info.SupportedFileExtensions) { DocumentParsers.Add(ext, info); if (!SupportedDocumentExtensions.Contains(ext)) { SupportedDocumentExtensions.Add(ext); } if (!SupportedExtensions.Contains(ext)) { SupportedExtensions.Add(ext); } } } public void AddImageParser(ImageParserInfo info) { foreach (string ext in info.SupportedFileExtensions) { ImageParsers.Add(ext, info); if (!SupportedImageExtensions.Contains(ext)) { SupportedImageExtensions.Add(ext); } if (!SupportedExtensions.Contains(ext)) { SupportedExtensions.Add(ext); } } } public ImageParser CreateImageParser(string extensions, Stream stream) => Create(ImageParsers, extensions, stream); public DocumentParser CreateDocumentParser(string extension, Stream stream) => Create(DocumentParsers, extension, stream); private static TParser Create( ListDictionary dict, string extension, Stream stream) where TParserInfo : FileParserInfo where TParser : FileParser { if (!dict.ContainsKey(extension)) { return null; } var parserInfos = dict[extension]; foreach (var fileParserInfo in parserInfos) { if (fileParserInfo.Enabled) { return fileParserInfo.Create(stream); } } return null; } } }