FileParserList.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using PixiEditor.Parser;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Windows.Media.Imaging;
  5. namespace PixiEditor.SDK.FileParsers
  6. {
  7. internal class FileParserList
  8. {
  9. public List<string> SupportedImageExtensions { get; set; } = new();
  10. public List<string> SupportedDocumentExtensions { get; set; } = new();
  11. public List<string> SupportedExtensions { get; set; } = new();
  12. public ListDictionary<string, ImageParserInfo> ImageParsers { get; set; } = new();
  13. public ListDictionary<string, DocumentParserInfo> DocumentParsers { get; set; } = new();
  14. public void AddDocumentParser(DocumentParserInfo info)
  15. {
  16. foreach (string ext in info.SupportedFileExtensions)
  17. {
  18. DocumentParsers.Add(ext, info);
  19. if (!SupportedDocumentExtensions.Contains(ext))
  20. {
  21. SupportedDocumentExtensions.Add(ext);
  22. }
  23. if (!SupportedExtensions.Contains(ext))
  24. {
  25. SupportedExtensions.Add(ext);
  26. }
  27. }
  28. }
  29. public void AddImageParser(ImageParserInfo info)
  30. {
  31. foreach (string ext in info.SupportedFileExtensions)
  32. {
  33. ImageParsers.Add(ext, info);
  34. if (!SupportedImageExtensions.Contains(ext))
  35. {
  36. SupportedImageExtensions.Add(ext);
  37. }
  38. if (!SupportedExtensions.Contains(ext))
  39. {
  40. SupportedExtensions.Add(ext);
  41. }
  42. }
  43. }
  44. public ImageParser CreateImageParser(string extensions, Stream stream) =>
  45. Create<ImageParserInfo, ImageParser, WriteableBitmap>(ImageParsers, extensions, stream);
  46. public DocumentParser CreateDocumentParser(string extension, Stream stream) =>
  47. Create<DocumentParserInfo, DocumentParser, SerializableDocument>(DocumentParsers, extension, stream);
  48. private static TParser Create<TParserInfo, TParser, T>(
  49. ListDictionary<string, TParserInfo> dict,
  50. string extension,
  51. Stream stream)
  52. where TParserInfo : FileParserInfo<TParser, T>
  53. where TParser : FileParser<T>
  54. {
  55. if (!dict.ContainsKey(extension))
  56. {
  57. return null;
  58. }
  59. var parserInfos = dict[extension];
  60. foreach (var fileParserInfo in parserInfos)
  61. {
  62. if (fileParserInfo.Enabled)
  63. {
  64. return fileParserInfo.Create(stream);
  65. }
  66. }
  67. return null;
  68. }
  69. }
  70. }