| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Xml;
- using IwbZero.Expr;
- using IwbZero.IwbBase;
- using IwbZero.ToolCommon.AssemblyHelpers;
- using IwbZero.ToolCommon.StringModel;
- namespace IwbZero.ExprFunctions
- {
- public class IwbFunction:IIwbId
- {
- protected IIwbFunction Function;
- public string Id { get; set; }
- public string FunctionId { get; set; }
- public string FunctionClass{ get; set; }
- public string FunctionName{ get; set; }
- public string FunctionDesc { get; set; }
- public string FunctionParam { get; set; }
- /// <summary>
- /// 根据ID加载函数
- /// </summary>
- /// <param name="functionId"></param>
- /// <returns></returns>
- public static IwbFunction LoadById(string functionId)
- {
- return QueryFunById(LoadFunXmlNode(), functionId);
- }
-
- /// <summary>
- /// 查询函数
- /// </summary>
- /// <param name="xmlNode"></param>
- /// <param name="functionId"></param>
- /// <returns></returns>
- public static IwbFunction QueryFunById(XmlNode xmlNode,string functionId)
- {
- if (!xmlNode.HasChildNodes)
- {
- return null;
- }
- foreach (XmlNode child in xmlNode.ChildNodes)
- {
- if (child.Attributes?["id"] != null && child.Attributes["id"].Value.UAndT()==functionId)
- {
- return Xml2Fun(child);
- //IwbFunction function = new IwbFunction()
- //{
- // FunctionId = child.Attributes["id"].Value,
- // Id = functionId,
- //};
- //if (child.HasChildNodes)
- //{
- // foreach (XmlNode node in child.ChildNodes)
- // {
- // switch (node.Name.Trim())
- // {
- // case "Name":
- // function.FunctionName = node.FirstChild?.Value;
- // break;
- // case "ClassName":
- // function.FunctionClass = node.FirstChild?.Value;
- // break;
- // case "Description":
- // function.FunctionDesc = node.FirstChild?.Value;
- // break;
- // case "Param":
- // function.FunctionParam = node.FirstChild?.Value;
- // break;
- // }
- // }
- //}
- }
- }
- return null;
- }
- /// <summary>
- /// 执行函数
- /// </summary>
- /// <param name="poRoot"></param>
- /// <returns></returns>
- public virtual string InvokeFunction(ExprObject poRoot)
- {
- string result = "";
- if (Function == null)
- {
- LoadFunction();
- }
- if (Function != null)
- {
- result = Function.Invoke(poRoot);
- }
- return result;
- }
- /// <summary>
- /// 加载函数
- /// </summary>
- protected virtual void LoadFunction()
- {
- if (FunctionClass != "")
- {
- object obj = AssemblyHelper.CreateInstance(FunctionClass);
- if (obj != null && obj is IIwbFunction function)
- {
- Function = function;
- }
- }
- if (Function == null)
- {
- throw new Exception($"Unable to load '{FunctionClass}' for function {FunctionName}.");
- }
- }
- public static List<IwbFunction> FunctionList => QueryAllFuns();
- public static List<IwbFunction> QueryAllFuns()
- {
- var funs = LoadByXml(LoadFunXmlNode());
- foreach (var fun in funs)
- {
- fun.FunctionParam = fun.FunctionParam.IsEmpty()
- ? ""
- : string.Join(",", fun.FunctionParam.Replace(",",",").Split(',').Select(a => $"'{a}'"));
- }
- return funs;
- }
- /// <summary>
- /// 加载Fun资源
- /// </summary>
- /// <returns></returns>
- private static XmlNode LoadFunXmlNode()
- {
- string fileName = "IwbZero.ExprFunctions.Functions.function.xml";
- XmlDocument xDoc = new XmlDocument();
- Assembly asm = Assembly.GetExecutingAssembly();
- //读取嵌入式资源
- Stream sm = asm.GetManifestResourceStream(fileName);
- if (sm != null)
- {
- xDoc.Load(sm);
- }
- if (!xDoc.HasChildNodes)
- {
- return null;
- }
- XmlNode xmlNode = xDoc.LastChild;
- return xmlNode;
- }
- /// <summary>
- /// 加载函数
- /// </summary>
- /// <param name="xmlNode"></param>
- /// <returns></returns>
- public static List<IwbFunction> LoadByXml(XmlNode xmlNode)
- {
- if (!xmlNode.HasChildNodes)
- {
- return null;
- }
- var funList = new List<IwbFunction>();
- foreach (XmlNode child in xmlNode.ChildNodes)
- {
- if (child.Attributes != null)
- {
- IwbFunction function = Xml2Fun(child);
- funList.Add(function);
- }
- }
- return funList;
- }
- /// <summary>
- /// XML 转成 FUN
- /// </summary>
- /// <param name="xmlNode"></param>
- /// <returns></returns>
- public static IwbFunction Xml2Fun(XmlNode xmlNode)
- {
- IwbFunction function = new IwbFunction
- {
- FunctionId = xmlNode.Attributes != null ? xmlNode.Attributes["id"]?.Value : ""
- };
- function.Id = function.FunctionId;
- if (xmlNode.HasChildNodes)
- {
- foreach (XmlNode node in xmlNode.ChildNodes)
- {
- switch (node.Name.Trim())
- {
- case "Name":
- function.FunctionName = node.FirstChild?.Value;
- break;
- case "ClassName":
- function.FunctionClass = node.FirstChild?.Value;
- break;
- case "Description":
- function.FunctionDesc = node.FirstChild?.Value;
- break;
- case "Param":
- function.FunctionParam = node.FirstChild?.Value;
- break;
- }
- }
- }
- return function;
- }
-
- public override string ToString()
- {
- return FunctionName;
- }
- }
- }
|