/// <summary> /// 讀取Cap檔案 /// </summary> /// <param name="path">Cap檔案路徑</param> public void LoadCapFile(string path) { CapDocument capDocument = new CapDocument(); capDocument.Load(path); CapDocuments.Add(capDocument); }
/// <summary> /// 驗證Cap物件 /// </summary> /// <param name="capDocument">Cap物件</param> /// <returns></returns> public static List <CapValidateResult> Validate(CapDocument capDocument) { var capValidateResults = new List <CapValidateResult>(); foreach (var name in ALERT_TAGS) { DateTime tester = new DateTime(); Regex regex = new Regex(@"^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d[-,+]\d\d:\d\d$"); switch (name) { case "sent": if (!string.IsNullOrEmpty(capDocument.sent)) { if (!regex.IsMatch(capDocument.sent) || !DateTime.TryParse(capDocument.sent.ToString().Replace("T", " ").Replace("+08:00", ""), out tester)) { capValidateResults.Add(new CapValidateResult(name, string.Format("{0}不是正確的時間格式:{1}", name, capDocument.sent))); } } else { capValidateResults.Add(new CapValidateResult(name, string.Format("發送的檔案中缺少{0}將造成讀取錯誤,請忽略後續錯誤並填入後重新檢核。", name))); } break; case "status": if (!string.IsNullOrEmpty(capDocument.status)) { if (!STATUS.Contains(capDocument.status)) { capValidateResults.Add(new CapValidateResult(name, string.Format("發送的檔案中缺少{0}", name))); } } else { capValidateResults.Add(new CapValidateResult(name, string.Format("發送的檔案中缺少{0}", name))); } break; case "msgType": if (!MSG_TYPE.Contains(capDocument.msgType)) { capValidateResults.Add(new CapValidateResult(name, string.Format("發送的檔案中缺少{0}", name))); } else { if (!capDocument.msgType.Equals("Alert")) { if (string.IsNullOrEmpty(capDocument.references)) { capValidateResults.Add(new CapValidateResult("references", string.Format("msgType為{0}的示警檔案必須填寫references", capDocument.msgType))); break; } StringBuilder ErrorsOfRefer = new StringBuilder(); List <string> References = new List <string>(); References = capDocument.references.Replace("\n", String.Empty).Split(new Char[] { ' ' }).ToList(); foreach (var triplet in References) { int commaCheck = 0; MatchCollection mc; Regex r = new Regex(","); if (triplet == "") { continue; } commaCheck = r.Matches(triplet).Count; if (commaCheck != 2) { capValidateResults.Add(new CapValidateResult("references", string.Format("references格式有誤(應為sender,identifier,sent三項一組並以空格分組)。此組填寫的內容為:{0}", "[" + triplet + "]"))); continue; } int firstComma = triplet.IndexOf(","); int secondComma = triplet.Substring(firstComma + 1, triplet.Length - firstComma - 1).IndexOf(",") + firstComma; if (firstComma == 0 && secondComma == 0) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組):" + capDocument.references); } if (firstComma == 0 && secondComma != 0) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組),其中缺少sender部分:" + capDocument.references); } if (firstComma != 0 && secondComma == 0) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組),其中缺少sent部分:" + capDocument.references); } if (secondComma == firstComma) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組),其中缺少identifier部分:" + capDocument.references); } string senderOfRefer = triplet.Substring(0, firstComma); try { string idOfRefer = triplet.Substring(firstComma + 1, secondComma - firstComma); } catch (Exception) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組):" + capDocument.references); } string sentOfRefer = triplet.Substring(secondComma + 2, triplet.Length - secondComma - 2); //檢查sent是否是標準時間格式 if (!regex.IsMatch(sentOfRefer) || !DateTime.TryParse(sentOfRefer.Replace("T", " ").Replace("+08:00", ""), out tester)) { ErrorsOfRefer.AppendLine("references格式有誤(應為sender,identifier,sent三項一組並以空格分組),其中sent非正確的時間格式:" + capDocument.references); } //ErrorsOfRefer.AppendLine("第一點" + firstComma + "第二點"+secondComma); } if (string.IsNullOrEmpty(ErrorsOfRefer.ToString())) { break; } capValidateResults.Add(new CapValidateResult("references", ErrorsOfRefer.ToString())); } else { if (!string.IsNullOrEmpty(capDocument.references)) { capValidateResults.Add(new CapValidateResult("references", string.Format("msgType為{0}的示警不得填寫references", capDocument.msgType))); break; } } } break; case "scope": if (!SCOPE.Contains(capDocument.scope)) { capValidateResults.Add(new CapValidateResult(name, string.Format("{0}資料錯誤{1}", name, capDocument.scope))); } else if (capDocument.scope.Equals("restricted")) { if (!string.IsNullOrEmpty(capDocument.restriction)) { capValidateResults.Add(new CapValidateResult("restriction", string.Format("當scope為restricted時必須填寫{0}", "restriction"))); } } else if (capDocument.scope.Equals("private")) { if (!string.IsNullOrEmpty(capDocument.addresses)) { capValidateResults.Add(new CapValidateResult("addresses", string.Format("當scope為private時必須填寫{0}", "addresses"))); } } break; //case "code": // if (!capDocument.code.Any()) // { // capValidateResults.Add(new CapValidateResult(name, string.Format("{0}不可為空.", name))); // } // break; } } var infos = capDocument.info; if (infos.Any()) { int InfoIndex = 1; if (infos.Count == 1) { InfoIndex = -1; } foreach (var info in infos) { string responseType = info.responseType.FirstOrDefault(); string category = info.category.FirstOrDefault(); string infoText; if (InfoIndex == -1) { infoText = ""; } else { infoText = string.Format("第{0}個", InfoIndex.ToString()); } List <EventCode> eventCode = new List <EventCode>(info.eventCode); List <Resource> resource = new List <Resource>(info.resource); List <Parameter> parameter = new List <Parameter>(info.parameter); List <Area> area = new List <Area>(info.area); if (string.IsNullOrEmpty(info.expires)) { capValidateResults.Add(new CapValidateResult("expires", infoText + "info中expires未填入任何值,請務必填入失效的日期與時間")); } if (area.Count > 0) { foreach (var itemArea in area) { capValidateResults.AddRange(areaValidator(itemArea, InfoIndex)); } } else { capValidateResults.Add(new CapValidateResult("area", infoText + "info中不含任何area,area至少要有一個以上")); } capValidateResults.AddRange(checkInfoValueByName(info, InfoIndex, "responseType", responseType)); capValidateResults.AddRange(checkInfoValueByName(info, InfoIndex, "category", category)); capValidateResults.AddRange(infoValidator(info, InfoIndex)); InfoIndex++; } } else { capValidateResults.Add(new CapValidateResult("info", string.Format("{0}至少要有一個以上,此檔案不含任何info", "info"))); } return(capValidateResults); }