您的位置:首页 > 其它

用来验证字符串的规则引擎

2009-12-23 23:56 211 查看
规则引擎:用来验证字符串是否符合预配置的规则,可以通过配置文件动态定义不同的字符串验证条件

配置信息缓存类(RuleEngine_ConfigCache.cs)

using System;
using System.IO;
using System.Xml;
using EA.Common.Util;
namespace Rule
{
/// <summary>
/// Config Cache Class used for cache config file
/// </summary>
public class ConfigCache
{
/// <summary>
/// Config Name: this value is defined in Web Config file, used for define RuleEngine configue file name
/// </summary>
private const string _configName = "RuleEngine";
/// <summary>
/// Full File Name
/// </summary>
private static string _fullFileName = "";
/// <summary>
/// Config file root node name
/// </summary>
private const string _root = "configuration";
/// <summary>
/// FileInfo used for check file change
/// </summary>
private static FileInfo _fileInfo;
/// <summary>
/// Last check
/// </summary>
private static DateTime _lastCheck;
/// <summary>
/// last modify time of config file
/// </summary>
private static DateTime _lastModified;

/// <summary>
/// Default Error Code will throw when checking a rule fail and set ThrowExceptionCodeDirectly to true
/// </summary>
private static string _defaultErrorCode;
public static string DefaultErrorCode
{
set
{
_defaultErrorCode = value;
}
get
{
return _defaultErrorCode;
}
}
/// <summary>
/// Rule Array
/// </summary>
private static Rule[] _ruleArray;
public static Rule[] RuleArray
{
set
{
_ruleArray = value;
}
get
{
return _ruleArray;
}
}
/// <summary>
/// Rule Set Array
/// </summary>
private static RuleSet[] _ruleSetArray;
public static RuleSet[] RuleSetArray
{
set
{
_ruleSetArray = value;
}
get
{
return _ruleSetArray;
}
}
/// <summary>
/// Static Constructor
/// </summary>
static ConfigCache()
{
string fileName = ServiceConfiguration.GetInstance().GetAppSetting(_configName);
string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\";
_fullFileName = filePath + fileName;
_fileInfo = new FileInfo(_fullFileName);
Load();
_lastModified = GetLastModified();
_lastCheck = DateTime.Now;
}
/// <summary>
/// Load config data from xml file
/// </summary>
private static void Load()
{
XmlDocument doc = new XmlDocument();
doc.Load(_fullFileName);
if (doc != null)
{
//Load DefaultErrorCode
XmlNode ruleNode = doc.SelectSingleNode("/" + _root + "/defaultErrorCode");
if (ruleNode!=null)
{
_defaultErrorCode = ruleNode.InnerText;
}
//Load Rule List
_ruleArray = GetRuleList(doc);
//Load Rule Set List
_ruleSetArray = GetRuleSetList(doc);
}
}
/// <summary>
/// Get arrtibute name of XmlNode object
/// </summary>
/// <param name="attName"></param>
/// <returns></returns>
private static string GetAttribute(XmlNode node, string attName)
{
if (node != null && node.Attributes[attName]!=null)
{
return node.Attributes[attName].Value;
}
return null;
}
/// <summary>
/// Get Rule List
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private static Rule[] GetRuleList(XmlDocument doc)
{
Rule[] array = null;
XmlNode ruleNode = doc.SelectSingleNode("/" + _root + "/rules");
if (ruleNode.ChildNodes.Count > 0)
{
array = new Rule[ruleNode.ChildNodes.Count];
Rule rule;
int index = 0;
for (int i = 0; i < ruleNode.ChildNodes.Count; i++)
{
//skip comment
if (ruleNode.ChildNodes[i].NodeType == XmlNodeType.Comment)
{
continue;
}
rule = new Rule();
rule.ID = GetAttribute(ruleNode.ChildNodes[i],"id");
rule.Name = GetAttribute(ruleNode.ChildNodes[i],"name");
string strType = GetAttribute(ruleNode.ChildNodes[i],"type");
if (!String.IsNullOrEmpty(strType))
{
rule.Type = (RuleType)Enum.Parse(typeof(RuleType), strType);
}
string strIsNot = GetAttribute(ruleNode.ChildNodes[i],"isNot");
if (!String.IsNullOrEmpty(strIsNot))
{
rule.IsNot = Boolean.Parse(strIsNot);
}
rule.ErrorCode = GetAttribute(ruleNode.ChildNodes[i],"errorCode");
rule.Value = GetAttribute(ruleNode.ChildNodes[i], "value");

array[index] = rule;
index++;
}
}

return array;
}
/// <summary>
/// Get Rule Set List
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
private static RuleSet[] GetRuleSetList(XmlDocument doc)
{
RuleSet[] setArray = null;
XmlNode setNode = doc.SelectSingleNode("/" + _root + "/ruleSets");
if (setNode.ChildNodes.Count > 0)
{
setArray = new RuleSet[setNode.ChildNodes.Count];
RuleSet set;
for (int i = 0; i < setNode.ChildNodes.Count; i++)
{
XmlNode ruleNode = setNode.ChildNodes[i];
//skip comment
if (ruleNode.NodeType == XmlNodeType.Comment)
{
continue;
}
set = new RuleSet();
set.SetId = GetAttribute(ruleNode,"id");
if (ruleNode.ChildNodes.Count > 0)
{
set.RuleIdList = new string[ruleNode.ChildNodes.Count];
for (int j = 0; j < ruleNode.ChildNodes.Count; j++)
{
//skip comment
if (ruleNode.ChildNodes[j].NodeType == XmlNodeType.Comment)
{
continue;
}
set.RuleIdList[j] = GetAttribute(ruleNode.ChildNodes[j],"id");
}
}
setArray[i] = set;
}
}
return setArray;
}
/// <summary>
/// Get tule object by rule id
/// </summary>
/// <param name="ruleId"></param>
public static Rule GetRule(string ruleId)
{
Rule result = null;
if (_ruleArray != null)
{
for (int i = 0; i < _ruleArray.Length; i++)
{
if (_ruleArray[i].ID == ruleId)
{
result = _ruleArray[i];
break;
}
}
}
return result;
}
/// <summary>
/// Get rule set object by set id
/// </summary>
/// <param name="setId"></param>
public static RuleSet GetRuleSet(string setId)
{
CheckFileChanged();
RuleSet result = null;
if (_ruleSetArray != null)
{
for (int i = 0; i < _ruleSetArray.Length; i++)
{
if (_ruleSetArray[i].SetId == setId)
{
result = _ruleSetArray[i];
break;
}
if (i==(_ruleSetArray.Length-1))
{
throw new Exception("RuleEngine can't find set id:" + setId);
}
}
}
return result;
}
/// <summary>
/// if config file changed, invoke this method will reload config data from disk
/// </summary>
private static void CheckFileChanged()
{
//Less than 10mins, do not check
if (DateTime.Now.Subtract(_lastCheck).Minutes < 10)
{
return;
}
DateTime check = GetLastModified();
if (_lastModified != check)
{
Load();
_lastModified = check;
_lastCheck = DateTime.Now;
}
}
/// <summary>
/// Get last modify time of config file
/// </summary>
/// <returns></returns>
private static DateTime GetLastModified()
{
_fileInfo.Refresh();
return _fileInfo.LastWriteTime;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: