| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- using System.Text.RegularExpressions;
- using LAPS_XMLQC_Service.Models;
- //using SystemRegexMatch = System.Text.RegularExpressions.Match;
- using MyMatch = LAPS_XMLQC_Service.Models.Matches;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- //using System.Threading;
- using System.IO;
- using System.Linq;
- using System;
- using System.Collections.Concurrent;
- //using Microsoft.AspNetCore.Mvc;
- namespace LAPS_XMLQC_Service.Services
- {
- public class FileSearchService : FileSearchServiceBase
- {
- private readonly RegexPatternService _regexPatternService;
- public enum Status
- {
- Load,
- Preview,
- Accept,
- Replaced,
- Failed,
- Deleted
- }
- public FileSearchService(RegexPatternService regexPatternService)
- {
- _regexPatternService = regexPatternService;
- }
- public async Task<List<MatchedResult>> SearchFiles(string directoryPath, string? searchTerm, string fileType, bool caseInsensitive, bool singleLine, bool multiLine, bool ignoreWhitespace, bool explicitCapture)
- {
- var regexOptions = RegexOptions.None;
- var results = new ConcurrentBag<MatchedResult>();
- if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
- if (singleLine) regexOptions |= RegexOptions.Singleline;
- if (multiLine) regexOptions |= RegexOptions.Multiline;
- if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
- if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
- regexOptions |= RegexOptions.Compiled;
- var patterns = _regexPatternService.GetRegexPatterns();
- var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
- var tasks = patterns.Select(pattern => Task.Run(async () =>
- {
- var searchTerm = pattern.Pattern;
- var regex = new Regex(searchTerm, regexOptions);
- var fileTasks = files.Select(async file =>
- {
- var content = await File.ReadAllTextAsync(file);
- 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\">");
- var matches = regex.Matches(content);
- if (matches.Count > 0)
- {
- results.Add(new MatchedResult
- {
- FileName = Path.GetFileName(file),
- FilePath = file,
- DirectoryPath = directoryPath,
- Content = content,
- PreviewContent = "",
- ResultCount = matches.Count,
- FinalContent = "",
- Status = Status.Load.ToString(),
- PatternResults = new List<PatternResult>
- {
- new PatternResult
- {
- Pattern = searchTerm,
- Matches = matches
- .Select(m => new MyMatch
- {
- Index = m.Index,
- Content = m.Value,
- LineNumber = CalculateLineNumber(content, m.Index)
- }).ToList()
- }
- }
- });
- }
- });
- await Task.WhenAll(fileTasks);
- }));
- await Task.WhenAll(tasks);
- return results.ToList();
- //var regexOptions = RegexOptions.None;
- //var results = new List<MatchedResult>();
- //if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
- //if (singleLine) regexOptions |= RegexOptions.Singleline;
- //if (multiLine) regexOptions |= RegexOptions.Multiline;
- //if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
- //if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
- //regexOptions |= RegexOptions.Compiled;
- //var patterns = _regexPatternService.GetRegexPatterns();
- //var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
- //foreach (var pattern in patterns)
- //{
- // searchTerm = pattern.Pattern;
- // var regex = new Regex(searchTerm, regexOptions);
- // foreach (var file in files)
- // {
- // var content = await File.ReadAllTextAsync(file);
- // var matches = regex.Matches(content);
- // if (matches.Count > 0)
- // {
- // results.Add(new MatchedResult
- // {
- // FileName = Path.GetFileName(file),
- // FilePath = file,
- // DirectoryPath = directoryPath,
- // Content = content,
- // PreviewContent = "",
- // ResultCount = matches.Count,
- // FinalContent = "",
- // Status = Status.Load.ToString(),
- // PatternResults = new List<PatternResult>
- // {
- // new PatternResult
- // {
- // Pattern = searchTerm,
- // Matches = matches
- // .Select(m => new MyMatch
- // {
- // Index = m.Index,
- // Content = m.Value,
- // LineNumber = CalculateLineNumber(content, m.Index)
- // }).ToList()
- // }
- // }
- // });
- // }
- // }
- //}
- //return results;
- //// Validate and format the file type
- //if (fileType.StartsWith('.'))
- //{
- // fileType = "*" + fileType;
- //}
- //else if (!fileType.StartsWith('*') && !fileType.StartsWith('.'))
- //{
- // fileType = "*." + fileType;
- //}
- //var results = new List<MatchedResult>();
- //Regex? regex = null;
- //if (!string.IsNullOrEmpty(searchTerm))
- //{
- // var regexOptions = RegexOptions.None;
- // // Set regex options based on parameters
- // if (caseInsensitive)
- // regexOptions |= RegexOptions.IgnoreCase;
- // if (singleLine)
- // regexOptions |= RegexOptions.Singleline;
- // if (multiLine)
- // regexOptions |= RegexOptions.Multiline;
- // if (ignoreWhitespace)
- // regexOptions |= RegexOptions.IgnorePatternWhitespace;
- // if (explicitCapture)
- // regexOptions |= RegexOptions.ExplicitCapture;
- // regexOptions |= RegexOptions.Compiled;
- // regex = new Regex(searchTerm, regexOptions);
- //}
- //// Limit the degree of parallelism for better resource management
- //int maxDegreeOfParallelism = 5;
- //var semaphore = new SemaphoreSlim(maxDegreeOfParallelism);
- //var fileTasks = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories)
- // .Select(async file =>
- // {
- // await semaphore.WaitAsync();
- // try
- // {
- // var matches = new List<MyMatch>();
- // var content = await File.ReadAllTextAsync(file);
- // var matchedResult = new MatchedResult
- // {
- // FileName = Path.GetFileName(file),
- // Content = content,
- // PatternResults = new List<PatternResult>()
- // };
- // // Perform regex matching on the file content
- // if (regex != null)
- // {
- // foreach (SystemRegexMatch match in regex!.Matches(content))
- // {
- // matches.Add(new MyMatch
- // {
- // Index = match.Index,
- // Content = match.Value,
- // LineNumber = CalculateLineNumber(content, match.Index)
- // });
- // }
- // }
- // // Add the pattern result
- // var patternResult = new PatternResult { Pattern = searchTerm, Matches = matches };
- // matchedResult.ResultCount = matches.Count;
- // matchedResult.PatternResults!.Add(patternResult);
- // return matchedResult;
- // }
- // catch (Exception ex)
- // {
- // // Handle the exception, possibly logging it
- // Console.WriteLine($"Error processing file {file}: {ex.Message}");
- // return null;
- // }
- // finally
- // {
- // semaphore.Release(); // Release the semaphore
- // }
- // });
- //// Execute all file processing tasks in parallel
- //var taskResults = await Task.WhenAll(fileTasks);
- //results = taskResults.Where(result => result != null).Cast<MatchedResult>().ToList();
- //return results;
- }
- 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)
- {
- // Configure regex options
- if (string.IsNullOrEmpty(searchTerm))
- throw new ArgumentException("Search term cannot be empty.");
- // Configure regex options
- if (string.IsNullOrEmpty(replacementText))
- throw new ArgumentException("Replacement text cannot be empty.");
- var regexOptions = RegexOptions.None;
- if (caseInsensitive) regexOptions |= RegexOptions.IgnoreCase;
- if (singleLine) regexOptions |= RegexOptions.Singleline;
- if (multiLine) regexOptions |= RegexOptions.Multiline;
- if (ignoreWhitespace) regexOptions |= RegexOptions.IgnorePatternWhitespace;
- if (explicitCapture) regexOptions |= RegexOptions.ExplicitCapture;
- // regexOptions |= RegexOptions.Compiled;
- var results = new List<MatchedResult>();
- try
- {
- var regex = new Regex(searchTerm, regexOptions);
-
-
-
- var files = Directory.GetFiles(directoryPath, fileType, SearchOption.AllDirectories);
- foreach (var file in files)
- {
- var content = await File.ReadAllTextAsync(file);
- // 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\">");
- var matches = regex.Matches(content);
- // Generate preview
- var previewContent = regex.Replace(content, replacementText);
- MatchCollection match= regex.Matches(previewContent);
- if (matches.Count > 0)
- {
- results.Add(new MatchedResult
- {
- FileName = Path.GetFileName(file),
- FilePath = file,
- DirectoryPath = directoryPath,
- Content = content,
- PreviewContent = previewContent,
- ResultCount = matches.Count,
- FinalContent = "",
- Status = Status.Preview.ToString(),
- PatternResults = new List<PatternResult>
- {
- new PatternResult
- {
- Pattern = searchTerm,
- Matches = matches
- .Select(m => new MyMatch
- {
- Index = m.Index,
- Content = m.Value,
- LineNumber = CalculateLineNumber(content, m.Index)
- }).ToList()
- }
- }
- });
- }
- }
- if(options.Equals("ReplaceAll"))
- {
- results = await ApplyChanges(results);
- }
- }
- catch (Exception ex)
- {
- }
- return results;
- }
- public async Task<MatchedResult> ApplyChange(MatchedResult result)
- {
- string filePath = string.Empty;
- if (!string.IsNullOrEmpty(result.PreviewContent))
- {
- //string outputDirectory = Path.Combine(Path.GetDirectoryName(result.FilePath), "OUT");
- //if (!Directory.Exists(outputDirectory))
- //{
- // Directory.CreateDirectory(outputDirectory); // Creates the folder if it doesn't exist
- //}
- // Set the file path within the "out" folder
- //filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
- string outputDirectory = result.DirectoryPath.Replace(@"\IN", @"\OUT");
- filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
- // Write the preview content to the file
- await File.WriteAllTextAsync(filePath, result.PreviewContent);
- result.OutputFilePath = filePath;
- result.Status = Status.Accept.ToString();
- }
- // Return the result
- return result;
- // string filePath = string.Empty;
- // if (!string.IsNullOrEmpty(result.PreviewContent))
- // {
- // filePath = result.FilePath;
- // await File.WriteAllTextAsync(filePath, result.PreviewContent); // Write the preview content to the file
- // // Update the final content
- // // result.FinalContent = result.PreviewContent;
- // // result.Content = result.PreviewContent;
- // // result.PreviewContent = "";
- // result.Status = "Replaced";
- //}
- //return result;
- }
- public async Task<List<MatchedResult>> ApplyChanges(List<MatchedResult> results)
- {
- string filePath = string.Empty;
- string outputDirectory = string.Empty;
- foreach (var result in results)
- {
- if (!string.IsNullOrEmpty(result.PreviewContent))
- {
- //string outputDirectory = Path.Combine(Path.GetDirectoryName(result.FilePath), "OUT");
- //if (!Directory.Exists(outputDirectory))
- //{
- // Directory.CreateDirectory(outputDirectory); // Creates the folder if it doesn't exist
- //}
- //// Set the file path within the "out" folder
- //filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
- if (string.IsNullOrEmpty(outputDirectory))
- {
- outputDirectory = result.DirectoryPath.Replace(@"\IN", @"\OUT");
- }
- filePath = Path.Combine(outputDirectory, Path.GetFileName(result.FilePath));
- // Write the preview content to the file
- await File.WriteAllTextAsync(filePath, result.PreviewContent);
- result.OutputFilePath = filePath;
- result.Status = Status.Accept.ToString();
- }
- }
- return results;
- //string filePath = string.Empty;
- //foreach (var result in results)
- //{
- // if (!string.IsNullOrEmpty(result.PreviewContent))
- // {
- // filePath = string.Empty;
- // filePath = result.FilePath;
- // await File.WriteAllTextAsync(filePath, result.PreviewContent); // Write the preview content to the file
- // // Update the final content
- // //result.FinalContent = result.PreviewContent;
- // // result.Content = result.PreviewContent;
- // // result.PreviewContent = "";
- // result.Status = "Replaced";
- // }
- //}
- //return results;
- }
- public List<MatchedResult> Accept(List<MatchedResult> results)
- {
- foreach (var result in results)
- {
- try
- {
- // Move file and overwrite if it exists
- File.Move(result.OutputFilePath, result.FilePath, true);
- result.FinalContent = result.PreviewContent;
- result.Content = result.PreviewContent;
- result.Status = Status.Replaced.ToString();
- }
- catch (Exception ex)
- {
- // Log the error and update the status to indicate failure
- Console.WriteLine($"Error processing file {result.OutputFilePath}: {ex.Message}");
- result.Status = Status.Failed.ToString();
- }
- }
- return results;
- }
- public List<MatchedResult> Reject(List<MatchedResult> results)
- {
- foreach (var result in results)
- {
- try
- {
- File.Delete(result.OutputFilePath);
- result.Status = Status.Deleted.ToString();
- }
- catch (Exception ex)
- {
- // Log the error and update the status to indicate failure
- Console.WriteLine($"Error processing file {result.OutputFilePath}: {ex.Message}");
- result.Status = Status.Failed.ToString();
- }
- }
- return results;
- }
- }
- }
|