<commit_before>package ca.corefacility.bioinformatics.irida.ria.config.thymeleaf.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.engine.AbstractTemplateHandler;
import org.thymeleaf.model.*;

import ca.corefacility.bioinformatics.irida.ria.config.thymeleaf.utilities.WebpackerUtilities;

import com.google.common.collect.ImmutableMap;

public class WebpackerTagHandler extends AbstractTemplateHandler {
	private IModelFactory modelFactory;
	private final String WEBPACKER_TAG_PATTERN = "webpacker:(css|js)";
	private final Pattern webpacker_pattern = Pattern.compile(WEBPACKER_TAG_PATTERN);
	private final Map<String, String> typeToElementMap = ImmutableMap.of("css", "link", "js", "script");
	private Map<String, Map<String, List<String>>> entryMap;
	private HttpServletRequest request;
	private List<String> addedChunks;

	public WebpackerTagHandler() {
		super();
		entryMap = WebpackerUtilities.getEntryMap();
	}

	@Override
	public void handleStandaloneElement(IStandaloneElementTag standaloneElementTag) {
		final String elementName = standaloneElementTag.getElementCompleteName();
		Matcher matcher = webpacker_pattern.matcher(elementName);
		if (matcher.find()) {
			String type = matcher.group(1);
			if (standaloneElementTag.hasAttribute("entry")) {
				String entry = standaloneElementTag.getAttributeValue("entry");
				if (entryMap.containsKey(entry) && entryMap.get(entry).containsKey(type)) {
					List<String> chunks = entryMap.get(entry).get(type);

					/*
					If this is a javascript entry, check to see if there are translations available.
					 */
					if (type.equals("js") && WebpackerUtilities.doesTranslationsFileExist(entry)) {
						String path = "../dist/i18n/" + entry + " :: i18n";

						// Add in translations block for js bundle
						super.handleOpenElement(modelFactory.createOpenElementTag("th:block", "th:replace", path, false));
						super.handleCloseElement(modelFactory.createCloseElementTag("th:block"));
					}

					/*
					Add new elements for each chunk.
					 */
					chunks.forEach(chunk -> {

						if (!addedChunks.contains(chunk)) {
							IOpenElementTag openElementTag = modelFactory
									.createOpenElementTag(typeToElementMap.get(type), getAttributesForType(type, chunk), AttributeValueQuotes.DOUBLE, false);
							ICloseElementTag closeElementTag = modelFactory.createCloseElementTag("script");
							super.handleOpenElement(openElementTag);
							super.handleCloseElement(closeElementTag);
							addedChunks.add(chunk);
							request.setAttribute("added-chunks", addedChunks);
						}

					});
				} else {
					IOpenElementTag warningDiv = modelFactory
							.createOpenElementTag("div", "style", "background-color: 'red'; ");
				}
			}
		} else {
			super.handleStandaloneElement(standaloneElementTag);
		}
	}

	@Override<commit_msg>Make sure the runtime bundle is only added once.<commit_after>package ca.corefacility.bioinformatics.irida.ria.config.thymeleaf.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.engine.AbstractTemplateHandler;
import org.thymeleaf.model.*;

import ca.corefacility.bioinformatics.irida.ria.config.thymeleaf.utilities.WebpackerUtilities;

import com.google.common.collect.ImmutableMap;

public class WebpackerTagHandler extends AbstractTemplateHandler {
	private IModelFactory modelFactory;
	private final String WEBPACKER_TAG_PATTERN = "webpacker:(css|js)";
	private final Pattern webpacker_pattern = Pattern.compile(WEBPACKER_TAG_PATTERN);
	private final Map<String, String> typeToElementMap = ImmutableMap.of("css", "link", "js", "script");
	private Map<String, Map<String, List<String>>> entryMap;
	private HttpServletRequest request;
	private List<String> addedChunks;

	public WebpackerTagHandler() {
		super();
		entryMap = WebpackerUtilities.getEntryMap();
	}

	@Override
	public void handleStandaloneElement(IStandaloneElementTag standaloneElementTag) {
		final String elementName = standaloneElementTag.getElementCompleteName();
		Matcher matcher = webpacker_pattern.matcher(elementName);
		if (matcher.find()) {
			String type = matcher.group(1);
			if (standaloneElementTag.hasAttribute("entry")) {
				String entry = standaloneElementTag.getAttributeValue("entry");
				if (entryMap.containsKey(entry) && entryMap.get(entry).containsKey(type)) {
					List<String> chunks = entryMap.get(entry).get(type);

					/*
					If this is a javascript entry, check to see if there are translations available.
					 */
					if (type.equals("js") && WebpackerUtilities.doesTranslationsFileExist(entry)) {
						String path = "../dist/i18n/" + entry + " :: i18n";

						// Add in translations block for js bundle
						super.handleOpenElement(modelFactory.createOpenElementTag("th:block", "th:replace", path, false));
						super.handleCloseElement(modelFactory.createCloseElementTag("th:block"));
					}

					/*
					Add new elements for each chunk.
					 */
					chunks.forEach(chunk -> {

						if (!addedChunks.contains(chunk)) {
							IOpenElementTag openElementTag = modelFactory
									.createOpenElementTag(typeToElementMap.get(type), getAttributesForType(type, chunk), AttributeValueQuotes.DOUBLE, false);
							ICloseElementTag closeElementTag = modelFactory.createCloseElementTag("script");
							super.handleOpenElement(openElementTag);
							super.handleCloseElement(closeElementTag);
							addedChunks.add(chunk);
						}

					});
				} else {
					IOpenElementTag warningDiv = modelFactory
							.createOpenElementTag("div", "style", "background-color: 'red'; ");
				}
			}
		} else {
			super.handleStandaloneElement(standaloneElementTag);
		}
	}

	@Override
	public void handleTemplateEnd(ITemplateEnd templateEnd) {
		request.setAttribute("added-chunks", addedChunks);
		super.handleTemplateEnd(templateEnd);
	}

	@Override
{'fraction_non_alphanumeric': 0.11082802547770701, 'fraction_numerical': 0.0015923566878980893, 'mean_word_length': 4.794280442804428, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '3115881', 'n_tokens_mistral': 2161, 'n_tokens_neox': 1836, 'n_words': 347}