IwbFunction.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Xml;
  7. using IwbZero.Expr;
  8. using IwbZero.IwbBase;
  9. using IwbZero.ToolCommon.AssemblyHelpers;
  10. using IwbZero.ToolCommon.StringModel;
  11. namespace IwbZero.ExprFunctions
  12. {
  13. public class IwbFunction:IIwbId
  14. {
  15. protected IIwbFunction Function;
  16. public string Id { get; set; }
  17. public string FunctionId { get; set; }
  18. public string FunctionClass{ get; set; }
  19. public string FunctionName{ get; set; }
  20. public string FunctionDesc { get; set; }
  21. public string FunctionParam { get; set; }
  22. /// <summary>
  23. /// 根据ID加载函数
  24. /// </summary>
  25. /// <param name="functionId"></param>
  26. /// <returns></returns>
  27. public static IwbFunction LoadById(string functionId)
  28. {
  29. return QueryFunById(LoadFunXmlNode(), functionId);
  30. }
  31. /// <summary>
  32. /// 查询函数
  33. /// </summary>
  34. /// <param name="xmlNode"></param>
  35. /// <param name="functionId"></param>
  36. /// <returns></returns>
  37. public static IwbFunction QueryFunById(XmlNode xmlNode,string functionId)
  38. {
  39. if (!xmlNode.HasChildNodes)
  40. {
  41. return null;
  42. }
  43. foreach (XmlNode child in xmlNode.ChildNodes)
  44. {
  45. if (child.Attributes?["id"] != null && child.Attributes["id"].Value.UAndT()==functionId)
  46. {
  47. return Xml2Fun(child);
  48. //IwbFunction function = new IwbFunction()
  49. //{
  50. // FunctionId = child.Attributes["id"].Value,
  51. // Id = functionId,
  52. //};
  53. //if (child.HasChildNodes)
  54. //{
  55. // foreach (XmlNode node in child.ChildNodes)
  56. // {
  57. // switch (node.Name.Trim())
  58. // {
  59. // case "Name":
  60. // function.FunctionName = node.FirstChild?.Value;
  61. // break;
  62. // case "ClassName":
  63. // function.FunctionClass = node.FirstChild?.Value;
  64. // break;
  65. // case "Description":
  66. // function.FunctionDesc = node.FirstChild?.Value;
  67. // break;
  68. // case "Param":
  69. // function.FunctionParam = node.FirstChild?.Value;
  70. // break;
  71. // }
  72. // }
  73. //}
  74. }
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// 执行函数
  80. /// </summary>
  81. /// <param name="poRoot"></param>
  82. /// <returns></returns>
  83. public virtual string InvokeFunction(ExprObject poRoot)
  84. {
  85. string result = "";
  86. if (Function == null)
  87. {
  88. LoadFunction();
  89. }
  90. if (Function != null)
  91. {
  92. result = Function.Invoke(poRoot);
  93. }
  94. return result;
  95. }
  96. /// <summary>
  97. /// 加载函数
  98. /// </summary>
  99. protected virtual void LoadFunction()
  100. {
  101. if (FunctionClass != "")
  102. {
  103. object obj = AssemblyHelper.CreateInstance(FunctionClass);
  104. if (obj != null && obj is IIwbFunction function)
  105. {
  106. Function = function;
  107. }
  108. }
  109. if (Function == null)
  110. {
  111. throw new Exception($"Unable to load '{FunctionClass}' for function {FunctionName}.");
  112. }
  113. }
  114. public static List<IwbFunction> FunctionList => QueryAllFuns();
  115. public static List<IwbFunction> QueryAllFuns()
  116. {
  117. var funs = LoadByXml(LoadFunXmlNode());
  118. foreach (var fun in funs)
  119. {
  120. fun.FunctionParam = fun.FunctionParam.IsEmpty()
  121. ? ""
  122. : string.Join(",", fun.FunctionParam.Replace(",",",").Split(',').Select(a => $"'{a}'"));
  123. }
  124. return funs;
  125. }
  126. /// <summary>
  127. /// 加载Fun资源
  128. /// </summary>
  129. /// <returns></returns>
  130. private static XmlNode LoadFunXmlNode()
  131. {
  132. string fileName = "IwbZero.ExprFunctions.Functions.function.xml";
  133. XmlDocument xDoc = new XmlDocument();
  134. Assembly asm = Assembly.GetExecutingAssembly();
  135. //读取嵌入式资源
  136. Stream sm = asm.GetManifestResourceStream(fileName);
  137. if (sm != null)
  138. {
  139. xDoc.Load(sm);
  140. }
  141. if (!xDoc.HasChildNodes)
  142. {
  143. return null;
  144. }
  145. XmlNode xmlNode = xDoc.LastChild;
  146. return xmlNode;
  147. }
  148. /// <summary>
  149. /// 加载函数
  150. /// </summary>
  151. /// <param name="xmlNode"></param>
  152. /// <returns></returns>
  153. public static List<IwbFunction> LoadByXml(XmlNode xmlNode)
  154. {
  155. if (!xmlNode.HasChildNodes)
  156. {
  157. return null;
  158. }
  159. var funList = new List<IwbFunction>();
  160. foreach (XmlNode child in xmlNode.ChildNodes)
  161. {
  162. if (child.Attributes != null)
  163. {
  164. IwbFunction function = Xml2Fun(child);
  165. funList.Add(function);
  166. }
  167. }
  168. return funList;
  169. }
  170. /// <summary>
  171. /// XML 转成 FUN
  172. /// </summary>
  173. /// <param name="xmlNode"></param>
  174. /// <returns></returns>
  175. public static IwbFunction Xml2Fun(XmlNode xmlNode)
  176. {
  177. IwbFunction function = new IwbFunction
  178. {
  179. FunctionId = xmlNode.Attributes != null ? xmlNode.Attributes["id"]?.Value : ""
  180. };
  181. function.Id = function.FunctionId;
  182. if (xmlNode.HasChildNodes)
  183. {
  184. foreach (XmlNode node in xmlNode.ChildNodes)
  185. {
  186. switch (node.Name.Trim())
  187. {
  188. case "Name":
  189. function.FunctionName = node.FirstChild?.Value;
  190. break;
  191. case "ClassName":
  192. function.FunctionClass = node.FirstChild?.Value;
  193. break;
  194. case "Description":
  195. function.FunctionDesc = node.FirstChild?.Value;
  196. break;
  197. case "Param":
  198. function.FunctionParam = node.FirstChild?.Value;
  199. break;
  200. }
  201. }
  202. }
  203. return function;
  204. }
  205. public override string ToString()
  206. {
  207. return FunctionName;
  208. }
  209. }
  210. }