FileSearchService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using System.Text.RegularExpressions;
  2. using LAPS_XMLQC_Service.Models;
  3. //using SystemRegexMatch = System.Text.RegularExpressions.Match;
  4. using MyMatch = LAPS_XMLQC_Service.Models.Matches;
  5. using System.Collections.Generic;
  6. using System.Threading.Tasks;
  7. //using System.Threading;
  8. using System.IO;
  9. using System.Linq;
  10. using System;
  11. using System.Collections.Concurrent;
  12. //using Microsoft.AspNetCore.Mvc;
  13. namespace LAPS_XMLQC_Service.Services
  14. {
  15. public class FileSearchService : FileSearchServiceBase
  16. {
  17. private readonly RegexPatternService _regexPatternService;
  18. public enum Status
  19. {
  20. Load,
  21. Preview,
  22. Accept,
  23. Replaced,
  24. Failed,
  25. Deleted
  26. }
  27. public FileSearchService(RegexPatternService regexPatternService)
  28. {
  29. _regexPatternService = regexPatternService;
  30. }
  31. public async Task<List<MatchedResult>> SearchFiles(string directoryPath, string? searchTerm, string fileType, bool caseInsensitive, bool singleLine, bool multiLine, bool ignoreWhitespace, bool explicitCapture)
  32. {
  33. var regexOptions = RegexOptions.None;
  34. var results = new ConcurrentBag<MatchedResult>();
  35. if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
  36. if (singleLine) regexOptions |= RegexOptions.Singleline;
  37. if (multiLine) regexOptions |= RegexOptions.Multiline;
  38. if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
  39. if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
  40. regexOptions |= RegexOptions.Compiled;
  41. var patterns = _regexPatternService.GetRegexPatterns();
  42. var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
  43. var tasks = patterns.Select(pattern => Task.Run(async () =>
  44. {
  45. var searchTerm = pattern.Pattern;
  46. var regex = new Regex(searchTerm, regexOptions);
  47. var fileTasks = files.Select(async file =>
  48. {
  49. var content = await File.ReadAllTextAsync(file);
  50. content = content.Replace("<COURTCASE>", "<COURTCASE xmlns:lndocmeta=\"http://www.lexis-nexis.com/lndocmeta\" xmlns:docinfo=\"http://www.lexis-nexis.com/glp/docinfo\" xmlns:lnvxe=\"http://www.lexis-nexis.com/lnvx\" xmlns:lnv=\"http://www.lexis-nexis.com/lnv\">");
  51. var matches = regex.Matches(content);
  52. if (matches.Count > 0)
  53. {
  54. results.Add(new MatchedResult
  55. {
  56. FileName = Path.GetFileName(file),
  57. FilePath = file,
  58. DirectoryPath = directoryPath,
  59. Content = content,
  60. PreviewContent = "",
  61. ResultCount = matches.Count,
  62. FinalContent = "",
  63. Status = Status.Load.ToString(),
  64. PatternResults = new List<PatternResult>
  65. {
  66. new PatternResult
  67. {
  68. Pattern = searchTerm,
  69. Matches = matches
  70. .Select(m => new MyMatch
  71. {
  72. Index = m.Index,
  73. Content = m.Value,
  74. LineNumber = CalculateLineNumber(content, m.Index)
  75. }).ToList()
  76. }
  77. }
  78. });
  79. }
  80. });
  81. await Task.WhenAll(fileTasks);
  82. }));
  83. await Task.WhenAll(tasks);
  84. return results.ToList();
  85. //var regexOptions = RegexOptions.None;
  86. //var results = new List<MatchedResult>();
  87. //if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
  88. //if (singleLine) regexOptions |= RegexOptions.Singleline;
  89. //if (multiLine) regexOptions |= RegexOptions.Multiline;
  90. //if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
  91. //if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
  92. //regexOptions |= RegexOptions.Compiled;
  93. //var patterns = _regexPatternService.GetRegexPatterns();
  94. //var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
  95. //foreach (var pattern in patterns)
  96. //{
  97. // searchTerm = pattern.Pattern;
  98. // var regex = new Regex(searchTerm, regexOptions);
  99. // foreach (var file in files)
  100. // {
  101. // var content = await File.ReadAllTextAsync(file);
  102. // var matches = regex.Matches(content);
  103. // if (matches.Count > 0)
  104. // {
  105. // results.Add(new MatchedResult
  106. // {
  107. // FileName = Path.GetFileName(file),
  108. // FilePath = file,
  109. // DirectoryPath = directoryPath,
  110. // Content = content,
  111. // PreviewContent = "",
  112. // ResultCount = matches.Count,
  113. // FinalContent = "",
  114. // Status = Status.Load.ToString(),
  115. // PatternResults = new List<PatternResult>
  116. // {
  117. // new PatternResult
  118. // {
  119. // Pattern = searchTerm,
  120. // Matches = matches
  121. // .Select(m => new MyMatch
  122. // {
  123. // Index = m.Index,
  124. // Content = m.Value,
  125. // LineNumber = CalculateLineNumber(content, m.Index)
  126. // }).ToList()
  127. // }
  128. // }
  129. // });
  130. // }
  131. // }
  132. //}
  133. //return results;
  134. //// Validate and format the file type
  135. //if (fileType.StartsWith('.'))
  136. //{
  137. // fileType = "*" + fileType;
  138. //}
  139. //else if (!fileType.StartsWith('*') && !fileType.StartsWith('.'))
  140. //{
  141. // fileType = "*." + fileType;
  142. //}
  143. //var results = new List<MatchedResult>();
  144. //Regex? regex = null;
  145. //if (!string.IsNullOrEmpty(searchTerm))
  146. //{
  147. // var regexOptions = RegexOptions.None;
  148. // // Set regex options based on parameters
  149. // if (caseInsensitive)
  150. // regexOptions |= RegexOptions.IgnoreCase;
  151. // if (singleLine)
  152. // regexOptions |= RegexOptions.Singleline;
  153. // if (multiLine)
  154. // regexOptions |= RegexOptions.Multiline;
  155. // if (ignoreWhitespace)
  156. // regexOptions |= RegexOptions.IgnorePatternWhitespace;
  157. // if (explicitCapture)
  158. // regexOptions |= RegexOptions.ExplicitCapture;
  159. // regexOptions |= RegexOptions.Compiled;
  160. // regex = new Regex(searchTerm, regexOptions);
  161. //}
  162. //// Limit the degree of parallelism for better resource management
  163. //int maxDegreeOfParallelism = 5;
  164. //var semaphore = new SemaphoreSlim(maxDegreeOfParallelism);
  165. //var fileTasks = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories)
  166. // .Select(async file =>
  167. // {
  168. // await semaphore.WaitAsync();
  169. // try
  170. // {
  171. // var matches = new List<MyMatch>();
  172. // var content = await File.ReadAllTextAsync(file);
  173. // var matchedResult = new MatchedResult
  174. // {
  175. // FileName = Path.GetFileName(file),
  176. // Content = content,
  177. // PatternResults = new List<PatternResult>()
  178. // };
  179. // // Perform regex matching on the file content
  180. // if (regex != null)
  181. // {
  182. // foreach (SystemRegexMatch match in regex!.Matches(content))
  183. // {
  184. // matches.Add(new MyMatch
  185. // {
  186. // Index = match.Index,
  187. // Content = match.Value,
  188. // LineNumber = CalculateLineNumber(content, match.Index)
  189. // });
  190. // }
  191. // }
  192. // // Add the pattern result
  193. // var patternResult = new PatternResult { Pattern = searchTerm, Matches = matches };
  194. // matchedResult.ResultCount = matches.Count;
  195. // matchedResult.PatternResults!.Add(patternResult);
  196. // return matchedResult;
  197. // }
  198. // catch (Exception ex)
  199. // {
  200. // // Handle the exception, possibly logging it
  201. // Console.WriteLine($"Error processing file {file}: {ex.Message}");
  202. // return null;
  203. // }
  204. // finally
  205. // {
  206. // semaphore.Release(); // Release the semaphore
  207. // }
  208. // });
  209. //// Execute all file processing tasks in parallel
  210. //var taskResults = await Task.WhenAll(fileTasks);
  211. //results = taskResults.Where(result => result != null).Cast<MatchedResult>().ToList();
  212. //return results;
  213. }
  214. public async Task<List<MatchedResult>> PreviewOrReplaceAll(string directoryPath, string? searchTerm, string replacementText, string fileType, bool caseInsensitive, bool singleLine, bool multiLine, bool ignoreWhitespace, bool explicitCapture, string options)
  215. {
  216. // Configure regex options
  217. if (string.IsNullOrEmpty(searchTerm))
  218. throw new ArgumentException("Search term cannot be empty.");
  219. // Configure regex options
  220. if (string.IsNullOrEmpty(replacementText))
  221. throw new ArgumentException("Replacement text cannot be empty.");
  222. var regexOptions = RegexOptions.None;
  223. if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
  224. if (singleLine) regexOptions |= RegexOptions.Singleline;
  225. if (multiLine) regexOptions |= RegexOptions.Multiline;
  226. if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
  227. if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
  228. // regexOptions |= RegexOptions.Compiled;
  229. var results = new List<MatchedResult>();
  230. try
  231. {
  232. var regex = new Regex(searchTerm, regexOptions);
  233. var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
  234. foreach (var file in files)
  235. {
  236. var content = await File.ReadAllTextAsync(file);
  237. // content = content.Replace("<COURTCASE>", "<COURTCASE xmlns:lndocmeta=\"http://www.lexis-nexis.com/lndocmeta\" xmlns:docinfo=\"http://www.lexis-nexis.com/glp/docinfo\" xmlns:lnvxe=\"http://www.lexis-nexis.com/lnvx\" xmlns:lnv=\"http://www.lexis-nexis.com/lnv\">");
  238. var matches = regex.Matches(content);
  239. // Generate preview
  240. var previewContent = regex.Replace(content, replacementText);
  241. MatchCollection match= regex.Matches(previewContent);
  242. if (matches.Count > 0)
  243. {
  244. results.Add(new MatchedResult
  245. {
  246. FileName = Path.GetFileName(file),
  247. FilePath = file,
  248. DirectoryPath = directoryPath,
  249. Content = content,
  250. PreviewContent = previewContent,
  251. ResultCount = matches.Count,
  252. FinalContent = "",
  253. Status = Status.Preview.ToString(),
  254. PatternResults = new List<PatternResult>
  255. {
  256. new PatternResult
  257. {
  258. Pattern = searchTerm,
  259. Matches = matches
  260. .Select(m => new MyMatch
  261. {
  262. Index = m.Index,
  263. Content = m.Value,
  264. LineNumber = CalculateLineNumber(content, m.Index)
  265. }).ToList()
  266. }
  267. }
  268. });
  269. }
  270. }
  271. if(options.Equals("ReplaceAll"))
  272. {
  273. results = await ApplyChanges(results);
  274. }
  275. }
  276. catch (Exception ex)
  277. {
  278. }
  279. return results;
  280. }
  281. public async Task<MatchedResult> ApplyChange(MatchedResult result)
  282. {
  283. string filePath = string.Empty;
  284. if (!string.IsNullOrEmpty(result.PreviewContent))
  285. {
  286. //string outputDirectory = Path.Combine(Path.GetDirectoryName(result.FilePath), "OUT");
  287. //if (!Directory.Exists(outputDirectory))
  288. //{
  289. // Directory.CreateDirectory(outputDirectory); // Creates the folder if it doesn't exist
  290. //}
  291. // Set the file path within the "out" folder
  292. //filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
  293. string outputDirectory = result.DirectoryPath.Replace(@"\IN", @"\OUT");
  294. filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
  295. // Write the preview content to the file
  296. await File.WriteAllTextAsync(filePath, result.PreviewContent);
  297. result.OutputFilePath = filePath;
  298. result.Status = Status.Accept.ToString();
  299. }
  300. // Return the result
  301. return result;
  302. // string filePath = string.Empty;
  303. // if (!string.IsNullOrEmpty(result.PreviewContent))
  304. // {
  305. // filePath = result.FilePath;
  306. // await File.WriteAllTextAsync(filePath, result.PreviewContent); // Write the preview content to the file
  307. // // Update the final content
  308. // // result.FinalContent = result.PreviewContent;
  309. // // result.Content = result.PreviewContent;
  310. // // result.PreviewContent = "";
  311. // result.Status = "Replaced";
  312. //}
  313. //return result;
  314. }
  315. public async Task<List<MatchedResult>> ApplyChanges(List<MatchedResult> results)
  316. {
  317. string filePath = string.Empty;
  318. string outputDirectory = string.Empty;
  319. foreach (var result in results)
  320. {
  321. if (!string.IsNullOrEmpty(result.PreviewContent))
  322. {
  323. //string outputDirectory = Path.Combine(Path.GetDirectoryName(result.FilePath), "OUT");
  324. //if (!Directory.Exists(outputDirectory))
  325. //{
  326. // Directory.CreateDirectory(outputDirectory); // Creates the folder if it doesn't exist
  327. //}
  328. //// Set the file path within the "out" folder
  329. //filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
  330. if (string.IsNullOrEmpty(outputDirectory))
  331. {
  332. outputDirectory = result.DirectoryPath.Replace(@"\IN", @"\OUT");
  333. }
  334. filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
  335. // Write the preview content to the file
  336. await File.WriteAllTextAsync(filePath, result.PreviewContent);
  337. result.OutputFilePath = filePath;
  338. result.Status = Status.Accept.ToString();
  339. }
  340. }
  341. return results;
  342. //string filePath = string.Empty;
  343. //foreach (var result in results)
  344. //{
  345. // if (!string.IsNullOrEmpty(result.PreviewContent))
  346. // {
  347. // filePath = string.Empty;
  348. // filePath = result.FilePath;
  349. // await File.WriteAllTextAsync(filePath, result.PreviewContent); // Write the preview content to the file
  350. // // Update the final content
  351. // //result.FinalContent = result.PreviewContent;
  352. // // result.Content = result.PreviewContent;
  353. // // result.PreviewContent = "";
  354. // result.Status = "Replaced";
  355. // }
  356. //}
  357. //return results;
  358. }
  359. public List<MatchedResult> Accept(List<MatchedResult> results)
  360. {
  361. foreach (var result in results)
  362. {
  363. try
  364. {
  365. // Move file and overwrite if it exists
  366. File.Move(result.OutputFilePath, result.FilePath, true);
  367. result.FinalContent = result.PreviewContent;
  368. result.Content = result.PreviewContent;
  369. result.Status = Status.Replaced.ToString();
  370. }
  371. catch (Exception ex)
  372. {
  373. // Log the error and update the status to indicate failure
  374. Console.WriteLine($"Error processing file {result.OutputFilePath}: {ex.Message}");
  375. result.Status = Status.Failed.ToString();
  376. }
  377. }
  378. return results;
  379. }
  380. public List<MatchedResult> Reject(List<MatchedResult> results)
  381. {
  382. foreach (var result in results)
  383. {
  384. try
  385. {
  386. File.Delete(result.OutputFilePath);
  387. result.Status = Status.Deleted.ToString();
  388. }
  389. catch (Exception ex)
  390. {
  391. // Log the error and update the status to indicate failure
  392. Console.WriteLine($"Error processing file {result.OutputFilePath}: {ex.Message}");
  393. result.Status = Status.Failed.ToString();
  394. }
  395. }
  396. return results;
  397. }
  398. }
  399. }