<commit_before>package io;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * Generates reports in HTML form.
 */
public class HTMLReportGenerator {

    private static final String resourcesDirectory = "reports/html/";
    private static final String modelHtmlFile = resourcesDirectory + "index.html";

    private static final Pattern dataFieldPattern = Pattern.compile("\\s*var\\s*(?<dataType>\\w+)\\s*=\\s*\\[\\s*\\];");
    private static final Pattern javaScriptLinkPattern = Pattern.compile(
            "<script\\s+type=\"text/javascript\"\\s+src=\"(?<scriptFile>[\\w/\\.]+)\"(\\s+|)>(\\s+|)</script>(\\s+|)");

    private List<Integer> messageCounts = new ArrayList<>();
    private List<Integer> detectionCounts = new ArrayList<>();

    public void addMessageCount(int count) {
        messageCounts.add(count);
    }

    public void addDetectionCount(int count) {
        detectionCounts.add(count);
    }

    /**
     * Generates the report based on the current available data.
     *
     * @param outputFile file to output the report.
     * @throws IOException
     */
    public void generate(File outputFile) throws IOException {

        try (BufferedReader reader = new BufferedReader(new FileReader(modelHtmlFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)) ) {

            String line;
            while ((line = reader.readLine()) != null) {
                Matcher javaScriptLinkMatcher = javaScriptLinkPattern.matcher(line);

                if(javaScriptLinkMatcher.matches()) {
                    linkScript(new File(resourcesDirectory + javaScriptLinkMatcher.group("scriptFile")), writer);
                } else {
                    writer.write(line); writer.newLine();
                }
            }

        }
    }

    /**
     * Includes the script included in the HTML report.
     *
     * @param scriptFile script file to include in the HTML report.
     * @param writer used to write the script file.
     * @throws IOException
     */
    private void linkScript(File scriptFile, BufferedWriter writer) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(scriptFile))) {

            writer.write("<script>"); writer.newLine(); // begin script element

            // copy the script inside the script element
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher dataFieldMatcher = dataFieldPattern.matcher(line);

                if (dataFieldMatcher.matches()) {
                    writeData(writer, dataFieldMatcher.group("dataType"));
                } else {
                    writer.write(line); writer.newLine();
                }
            }

            writer.write("</script>"); writer.newLine(); // and script element

        } catch (FileNotFoundException e) {
            // file does not exist
            writer.write("<!-- script '" + scriptFile + "' does not exist -->");
            writer.newLine();
        }
    }

    /**
     * Writes data to the report file according to the given data type. If the data type is invalid
     *<commit_msg>fixed the report generator ignoring all variables starting with empty array. For example: var name = [];<commit_after>package io;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

/**
 * Generates reports in HTML form.
 */
public class HTMLReportGenerator {

    private static final String resourcesDirectory = "reports/html/";
    private static final String modelHtmlFile = resourcesDirectory + "index.html";

    private static final Pattern dataFieldPattern = Pattern.compile("\\s*var\\s*(?<dataType>\\w+)\\s*=\\s*\\[\\s*\\];");
    private static final Pattern javaScriptLinkPattern = Pattern.compile(
            "<script\\s+type=\"text/javascript\"\\s+src=\"(?<scriptFile>[\\w/\\.]+)\"(\\s+|)>(\\s+|)</script>(\\s+|)");

    private List<Integer> messageCounts = new ArrayList<>();
    private List<Integer> detectionCounts = new ArrayList<>();

    public void addMessageCount(int count) {
        messageCounts.add(count);
    }

    public void addDetectionCount(int count) {
        detectionCounts.add(count);
    }

    /**
     * Generates the report based on the current available data.
     *
     * @param outputFile file to output the report.
     * @throws IOException
     */
    public void generate(File outputFile) throws IOException {

        try (BufferedReader reader = new BufferedReader(new FileReader(modelHtmlFile));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)) ) {

            String line;
            while ((line = reader.readLine()) != null) {
                Matcher javaScriptLinkMatcher = javaScriptLinkPattern.matcher(line);

                if(javaScriptLinkMatcher.matches()) {
                    linkScript(new File(resourcesDirectory + javaScriptLinkMatcher.group("scriptFile")), writer);
                } else {
                    writer.write(line); writer.newLine();
                }
            }

        }
    }

    /**
     * Includes the script included in the HTML report.
     *
     * @param scriptFile script file to include in the HTML report.
     * @param writer used to write the script file.
     * @throws IOException
     */
    private void linkScript(File scriptFile, BufferedWriter writer) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(scriptFile))) {

            writer.write("<script>"); writer.newLine(); // begin script element

            // copy the script inside the script element
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher dataFieldMatcher = dataFieldPattern.matcher(line);

                if (dataFieldMatcher.matches()) {
                    if (!writeData(writer, dataFieldMatcher.group("dataType"))) {
                        writer.write(line); writer.newLine();
                    }
                } else {
                    writer.write(line); writer.newLine();
                }
            }

            writer.write("</script>"); writer.newLine(); // and script element

        } catch (FileNotFoundException e) {
            // file does not exist
            writer.write("<!-- script '" + scriptFile + "' does not exist -->");
            writer.newLine();
        }
    }

    /**
     * Writes data to the report file according to the given data type. If the data type is invalid
     *
{'fraction_non_alphanumeric': 0.12024756852343059, 'fraction_numerical': 0.0, 'mean_word_length': 2.2962603205439533, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2246436', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1692, 'n_words': 476}