using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace HanvonF710XAttendanceService { internal static class TemplateParser { // Mirrors UTS MainForm.removeReturn(...) public static bool TryExtractTemplatePayloadFromGetEmployeeResponse(string getEmployeeResponse, out string payload) { payload = null; if (string.IsNullOrWhiteSpace(getEmployeeResponse)) { return false; } string[] splitData = getEmployeeResponse.Split('('); for (int i = 0; i < splitData.Length; i++) { string s = splitData[i]; if (!s.Contains("Return")) { string updatedData = s.Replace("result=\"success\" ", ""); string[] updatedResult = updatedData.Split(')'); payload = updatedResult.Length > 0 ? updatedResult[0] : null; payload = string.IsNullOrWhiteSpace(payload) ? null : payload; return payload != null; } } return false; } // Mirrors UTS MainForm.SetEmployeeInfo(...) string transformation public static bool TryBuildSetEmployeeCommand(string templatePayloadOrGetEmployeeResponse, out string setEmployeeCommand) { setEmployeeCommand = null; if (string.IsNullOrWhiteSpace(templatePayloadOrGetEmployeeResponse)) { return false; } string[] splitData = templatePayloadOrGetEmployeeResponse.Split('('); for (int i = 0; i < splitData.Length; i++) { string s = splitData[i]; if (!s.Contains("Return")) { string updatedData = s.Replace("result=\"success\" ", ""); string[] updatedResult = updatedData.Split(')'); var inner = updatedResult.Length > 0 ? updatedResult[0] : null; if (!string.IsNullOrWhiteSpace(inner)) { setEmployeeCommand = "SetEmployee(" + inner + ")"; return true; } } } return false; } public static List ParseEmployeeIdsFromGetEmployeeIdResponse(string getEmployeeIdResponse) { if (string.IsNullOrWhiteSpace(getEmployeeIdResponse)) { return new List(); } // Typical responses contain many occurrences like: id="6286" var matches = Regex.Matches(getEmployeeIdResponse, "\\bid=\"([^\"]+)\""); var ids = new HashSet(StringComparer.OrdinalIgnoreCase); foreach (Match m in matches) { if (!m.Success || m.Groups.Count < 2) { continue; } var id = m.Groups[1].Value?.Trim(); if (string.IsNullOrWhiteSpace(id)) { continue; } ids.Add(id); } return ids.OrderBy(x => x, StringComparer.OrdinalIgnoreCase).ToList(); } } }