ParseInstance.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:ParseInstance
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/13 15:04:06
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using RevitExport.Export;
  10. namespace RevitExport.Parse
  11. {
  12. /*
  13. * 最好由导出类控制,导出的开始和结束。这样有一个好处是可以挂接多个解析类。
  14. */
  15. /// <summary>
  16. /// 解析实例
  17. /// </summary>
  18. public class ParseInstance<C, Result> where C:ParseContext
  19. {
  20. public ParseInstance(C context,List<IParseElement<C, Result>> parsers)
  21. {
  22. Context = context;
  23. Parsers = parsers;
  24. InitFastFilter();
  25. }
  26. protected Dictionary<string, IParseElement<C, Result>> FastFilter { get; set; }
  27. protected List<IParseElement<C, Result>> Parsers { get; set; }
  28. protected void InitFastFilter()
  29. {
  30. FastFilter = new Dictionary<string, IParseElement<C, Result>>();
  31. foreach (var parseElement in Parsers)
  32. {
  33. var keys = parseElement.FastIndex();
  34. foreach (var key in keys)
  35. {
  36. if (!string.IsNullOrWhiteSpace(key))
  37. {
  38. FastFilter[key] = parseElement;
  39. }
  40. }
  41. }
  42. }
  43. protected virtual IParseElement<C, Result> GetMatchParser(ElementWrapper wrapper)
  44. {
  45. if (FastFilter.TryGetValue(wrapper.Category, out IParseElement<C, Result> parser))
  46. {
  47. return parser;
  48. }
  49. var currentParser = Parsers.FirstOrDefault(p => p.Match(wrapper));
  50. return currentParser;
  51. }
  52. public virtual List<Result> ParseElement(ElementWrapper elementWrapper)
  53. {
  54. var currentParser = GetMatchParser(elementWrapper);
  55. if (currentParser != null)
  56. {
  57. return currentParser.Parse(elementWrapper, Context);
  58. }
  59. return null;
  60. }
  61. #region 解析元素相关
  62. public virtual void ParseElement(object sender, ExportArgs e)
  63. {
  64. if (!Parsers.Any())
  65. return;
  66. ElementWrapper element = e.ElementWrapper;
  67. ParseElement(element);
  68. }
  69. /// <summary>
  70. /// 当前解析上下文
  71. /// </summary>
  72. public C Context { get;protected set; }
  73. /// <summary>
  74. /// 解析数据
  75. /// </summary>
  76. /// <param name="export"></param>
  77. /// <returns></returns>
  78. public virtual bool Parse(ExportInstance export)
  79. {
  80. try
  81. {
  82. BindingExport(this, export);
  83. Context.Export = export;
  84. return export.Export();
  85. }
  86. finally
  87. {
  88. export.ParseElement -= ParseElement;
  89. }
  90. }
  91. public static void BindingExport(ParseInstance<C, Result> instance, ExportInstance export)
  92. {
  93. export.ParseElement -= instance.ParseElement;
  94. export.ParseElement += instance.ParseElement;
  95. }
  96. #endregion
  97. }
  98. }