// Path: src/main/java/com/lazerycode/jmeter/results/XMLFileScanner.java
// public static int scanXmlFileForPattern(File file, Pattern searchPattern) throws MojoExecutionException {
//     int patternMatchCount = 0;
//     try (Scanner resultFileScanner = new Scanner(file)) {
//         while (resultFileScanner.findWithinHorizon(searchPattern, 0) != null) {
//             patternMatchCount++;
//         }
//     } catch (IOException e) {
//         throw new MojoExecutionException("An unexpected error occurred while reading file " + file.getAbsolutePath(), e);
//     }
//     return patternMatchCount;
// }


import org.apache.maven.plugin.MojoExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static com.lazerycode.jmeter.results.XMLFileScanner.scanXmlFileForPattern;
    public ResultScanner(boolean countSuccesses, boolean countFailures, boolean isCsv, boolean onlyFailWhenMatchingFailureMessage, List<String> failureMessages) {
        this.csv = isCsv;
        this.countFailures = countFailures;
        this.countSuccesses = countSuccesses;
        this.onlyFailWhenMatchingFailureMessage = onlyFailWhenMatchingFailureMessage;
        this.failureMessages = failureMessages;
    }

    /**
     * Work out how to parse the file (if at all)
     *
     * @param file File to parse
     * @throws MojoExecutionException MojoExecutionException
     */
    public void parseResultFile(File file) throws MojoExecutionException {
        if (!file.exists()) {
            throw new MojoExecutionException("Unable to find " + file.getAbsolutePath());
        }
        LOGGER.info(" ");
        LOGGER.info("Parsing results file '{}' as type: {}", file, this.csv ? "CSV" : "XML");
        if (csv) {
            CSVScanResult csvScanResult = CSVFileScanner.scanCsvForValues(file, failureMessages);
            successCount = csvScanResult.getSuccessCount();
            failureCount = csvScanResult.getFailureCount();
            for (Map.Entry<String, Integer> entry : csvScanResult.getSpecificFailureMessages().entrySet()) {
                customFailureCount = customFailureCount + entry.getValue();
                LOGGER.info("Number of potential custom failures using '{}' in '{}': {}", entry.getKey(), file.getName(), customFailureCount);
            }
        } else {
            if (countSuccesses) {
// Path: src/main/java/com/lazerycode/jmeter/results/XMLFileScanner.java
// public static int scanXmlFileForPattern(File file, Pattern searchPattern) throws MojoExecutionException {
//     int patternMatchCount = 0;
//     try (Scanner resultFileScanner = new Scanner(file)) {
//         while (resultFileScanner.findWithinHorizon(searchPattern, 0) != null) {
//             patternMatchCount++;
//         }
//     } catch (IOException e) {
//         throw new MojoExecutionException("An unexpected error occurred while reading file " + file.getAbsolutePath(), e);
//     }
//     return patternMatchCount;
// }


// Path: src/main/java/com/lazerycode/jmeter/results/ResultScanner.java
import org.apache.maven.plugin.MojoExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static com.lazerycode.jmeter.results.XMLFileScanner.scanXmlFileForPattern;

    public ResultScanner(boolean countSuccesses, boolean countFailures, boolean isCsv, boolean onlyFailWhenMatchingFailureMessage, List<String> failureMessages) {
        this.csv = isCsv;
        this.countFailures = countFailures;
        this.countSuccesses = countSuccesses;
        this.onlyFailWhenMatchingFailureMessage = onlyFailWhenMatchingFailureMessage;
        this.failureMessages = failureMessages;
    }

    /**
     * Work out how to parse the file (if at all)
     *
     * @param file File to parse
     * @throws MojoExecutionException MojoExecutionException
     */
    public void parseResultFile(File file) throws MojoExecutionException {
        if (!file.exists()) {
            throw new MojoExecutionException("Unable to find " + file.getAbsolutePath());
        }
        LOGGER.info(" ");
        LOGGER.info("Parsing results file '{}' as type: {}", file, this.csv ? "CSV" : "XML");
        if (csv) {
            CSVScanResult csvScanResult = CSVFileScanner.scanCsvForValues(file, failureMessages);
            successCount = csvScanResult.getSuccessCount();
            failureCount = csvScanResult.getFailureCount();
            for (Map.Entry<String, Integer> entry : csvScanResult.getSpecificFailureMessages().entrySet()) {
                customFailureCount = customFailureCount + entry.getValue();
                LOGGER.info("Number of potential custom failures using '{}' in '{}': {}", entry.getKey(), file.getName(), customFailureCount);
            }
        } else {
            if (countSuccesses) {
                successCount = scanXmlFileForPattern(file, Pattern.compile(XML_REQUEST_SUCCESS_PATTERN, Pattern.CASE_INSENSITIVE));