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; }
///
/// 根据ID加载函数
///
///
///
public static IwbFunction LoadById(string functionId)
{
return QueryFunById(LoadFunXmlNode(), functionId);
}
///
/// 查询函数
///
///
///
///
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;
}
///
/// 执行函数
///
///
///
public virtual string InvokeFunction(ExprObject poRoot)
{
string result = "";
if (Function == null)
{
LoadFunction();
}
if (Function != null)
{
result = Function.Invoke(poRoot);
}
return result;
}
///
/// 加载函数
///
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 FunctionList => QueryAllFuns();
public static List 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;
}
///
/// 加载Fun资源
///
///
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;
}
///
/// 加载函数
///
///
///
public static List LoadByXml(XmlNode xmlNode)
{
if (!xmlNode.HasChildNodes)
{
return null;
}
var funList = new List();
foreach (XmlNode child in xmlNode.ChildNodes)
{
if (child.Attributes != null)
{
IwbFunction function = Xml2Fun(child);
funList.Add(function);
}
}
return funList;
}
///
/// XML 转成 FUN
///
///
///
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;
}
}
}