コード例 #1
0
 public DiskService(
     IFileSystem fileSystem,
     IValidators validators)
 {
     _fileSystem = fileSystem;
     _validators = validators;
 }
コード例 #2
0
        /// <summary>
        /// 创建验证器
        /// </summary>
        /// <param name="excelID">xml节点Id</param>
        /// <returns></returns>
        public ExcelValidatorContainer CreateValidator(string excelID)
        {
            try
            {
                XmlElement excel = GetXmlNodeById("Excel", "ID", excelID);
                if (excel == null)
                {
                    throw new Exception($"找不到{excelID}的配置信息");
                }

                ExcelValidatorContainer container = new ExcelValidatorContainer();
                container.HeadRowNo         = Convert.ToInt32(excel.GetAttribute("HeadRowNo"));
                container.DataStartRowNo    = Convert.ToInt32(excel.GetAttribute("DataStartRowNo"));
                container.DynamicTable      = Convert.ToBoolean(excel.GetAttribute("DynamicTable"));
                container.DynamicStartColNo = Convert.ToInt32(excel.GetAttribute("DynamicStartColNo"));;

                XmlNodeList colList = excel.GetElementsByTagName("Col");

                foreach (XmlElement colNode in colList)
                {
                    string id        = colNode.GetAttribute("ID");
                    string colNo     = colNode.GetAttribute("ColNo");
                    string desc      = colNode.GetAttribute("Desc");
                    string type      = colNode.GetAttribute("Type");
                    string necessary = colNode.GetAttribute("Necessary");
                    int?   length    = null;
                    if (!string.IsNullOrEmpty(colNode.GetAttribute("Length")))
                    {
                        length = Convert.ToInt32(colNode.GetAttribute("Length"));
                    }

                    string regex        = colNode.GetAttribute("Regex");
                    string regexMessage = colNode.GetAttribute("RegexMessage");
                    int?   minValue     = string.IsNullOrEmpty(colNode.GetAttribute("MinValue")) ? int.MinValue : Convert.ToInt32(colNode.GetAttribute("MinValue"));
                    int?   maxValue     = string.IsNullOrEmpty(colNode.GetAttribute("MaxValue")) ? int.MaxValue : Convert.ToInt32(colNode.GetAttribute("MaxValue"));
                    int    decimals     = 0;
                    int.TryParse(colNode.GetAttribute("decimals"), out decimals);

                    container.ColsName.Add(Convert.ToInt32(colNo), id);
                    container.ColsDesc.Add(Convert.ToInt32(colNo), desc);
                    container.ColsType.Add(Convert.ToInt32(colNo), type.ToUpper());

                    IValidators validator = null;
                    switch (type.ToLower())
                    {
                    case "string":
                        validator = new StringValidator(Convert.ToBoolean(necessary), Convert.ToInt32(length), regex, regexMessage);
                        container.FormatValidators.Add(Convert.ToInt32(colNo), validator);
                        break;

                    case "int":
                        validator = new IntegerValidator(Convert.ToBoolean(necessary), minValue, maxValue);
                        container.FormatValidators.Add(Convert.ToInt32(colNo), validator);
                        break;

                    case "datetime":
                        validator = new DataTimeValidator(Convert.ToBoolean(necessary), regex, regexMessage);
                        container.FormatValidators.Add(Convert.ToInt32(colNo), validator);
                        break;

                    case "decimal":
                        validator = new DecimalValidator(Convert.ToBoolean(necessary), decimals);
                        container.FormatValidators.Add(Convert.ToInt32(colNo), validator);
                        break;

                    default:
                        throw new Exception();
                    }
                }

                #region 加载验证方法 没用到
                //加载验证方法
                XmlNodeList extList = excel.GetElementsByTagName("ExtValidator");
                foreach (XmlElement extNode in extList)
                {
                    InvokerInfo info = new InvokerInfo();
                    info.Assembly   = extNode.GetAttribute("Assembly");
                    info.ClassName  = extNode.GetAttribute("Class");
                    info.MethodName = extNode.GetAttribute("Method");

                    foreach (XmlElement paraNode in extNode.ChildNodes)
                    {
                        info.ParamsColNo.Add(Convert.ToInt32(paraNode.GetAttribute("ValueColNo")));
                        info.ParamsType.Add(paraNode.GetAttribute("Type"));
                    }
                    container.ExtValidators.Add(info);
                }
                #endregion

                return(container);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #3
0
        public Boolean Validate(int allowErrorNum, out List <string> errMsgList)
        {
            Boolean result = true;

            errMsgList = new List <string>();

            //创建扩展验证器
            foreach (InvokerInfo info in container.ExtValidators)
            {
                try
                {
                    Assembly ass = Assembly.Load(info.Assembly);
                    Type     t   = ass.GetType(info.ClassName, true);
                    typeHashtable.Add(info.ClassName, Activator.CreateInstance(t));

                    List <Type> typeList = new List <Type>();
                    for (int i = 0; i < info.ParamsType.Count; i++)
                    {
                        switch (info.ParamsType[i].ToUpper())
                        {
                        case "STRING":
                            typeList.Add(typeof(string));
                            break;

                        default:
                            throw new Exception();
                        }
                    }
                    MethodInfo methodInfo = t.GetMethod(info.MethodName, typeList.ToArray());
                    methodHashtable.Add(info.MethodName, methodInfo);
                }
                catch (Exception ex)
                {
                    result = false;
                    throw ex;
                }
            }

            //验证工作表是否存在
            ISheet st = null;

            st = workbook.GetSheet(dataSheetName);
            bool tr = true;

            if (st == null)
            {
                errMsgList.Add($"文件中必须存在名为{dataSheetName}的工作表");
                return(result);
            }


            IEnumerator it = st.GetRowEnumerator();

            //验证表头是否正确
            try
            {
                // 判断是否添加动态列方法
                if (container.DynamicTable)
                {
                    AddDynamicCols(st);
                }

                var headRowNo = container.HeadRowNo;
                foreach (KeyValuePair <int, string> name in container.ColsDesc)
                {
                    if (!name.Value.Trim().Equals(st.GetRow(headRowNo).GetCell(name.Key).StringCellValue.Trim()))
                    {
                        result = false;
                        errMsgList.Add($"表格式不正确。第{name.Key}列应该是{name.Value}");
                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //验证列表中的数据是否正确
            for (int i = container.DataStartRowNo; i < st.LastRowNum; i++)
            {
                //格式验证
                try
                {
                    foreach (KeyValuePair <int, IValidators> v in container.FormatValidators)
                    {
                        IValidators validator = v.Value;
                        if (!validator.Validate(st.GetRow(i).GetCell(v.Key)))
                        {
                            result = false;
                            errMsgList.Add($"第{i}行/第{v.Key}列格式验证不通过,原因:{validator.ErrorMessage}");
                            if (errMsgList.Count >= allowErrorNum)
                            {
                                return(result);
                            }
                        }
                    }
                }
                catch
                {
                    result = false;
                    errMsgList.Add("数据格式不正确,数据格式验证出现异常");
                    return(result);
                }

                //扩展验证(存在性验证)
                foreach (InvokerInfo info in container.ExtValidators)
                {
                    try
                    {
                        List <object> objectList = new List <object>();

                        for (int r = 0; r < info.ParamsType.Count; r++)
                        {
                            switch (info.ParamsType[r].ToUpper())
                            {
                            case "STRING":
                                objectList.Add(Convert.ToString(st.GetRow(r).GetCell(info.ParamsColNo[r])));
                                break;

                            default:
                                throw new Exception();
                            }
                        }
                        MethodInfo methodInfo = (MethodInfo)methodHashtable[info.MethodName];
                        FastInvoke.FastInvokeHandler fastInvoker = FastInvoke.GetMethodInvoker(methodInfo);
                        string extMsg = (string)fastInvoker(typeHashtable[info.ClassName], objectList.ToArray());
                        if (!string.IsNullOrEmpty(extMsg))
                        {
                            result = false;
                            errMsgList.Add($"第{i}行验证通不过 原因:{extMsg}");
                            if (errMsgList.Count >= allowErrorNum)
                            {
                                return(result);
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        result = false;
                        throw ex;
                    }
                }
            }

            return(result);
        }
コード例 #4
0
 protected ATest()
 {
     // ReSharper disable once DoNotCallOverridableMethodsInConstructor
     ValidValidators = GetValidators(true);
 }
コード例 #5
0
ファイル: ValidatorTest.cs プロジェクト: modulexcite/Cfg-NET
 public TestValidatorCfg2(string xml, IValidators validators) : base(validators) {
     Load(xml);
 }