<commit_before>package i5.las2peer.services.codeGenerationService.templateEngine;

import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import i5.las2peer.logging.L2pLogger;
import i5.las2peer.services.codeGenerationService.generators.ApplicationGenerator;
import i5.las2peer.services.codeGenerationService.generators.Generator;
import i5.las2peer.services.codeGenerationService.traces.segments.CompositeSegment;
import i5.las2peer.services.codeGenerationService.traces.segments.Segment;
import i5.las2peer.services.codeGenerationService.traces.segments.SegmentFactory;
import i5.las2peer.services.codeGenerationService.traces.segments.UnprotectedSegment;

/**
 * The model checker class is responsible to find not allowed elements in unprotected segments of
 * the traced model elements.
 * 
 * @author <NAME>
 *
 */

public class ModelChecker {

  private static final L2pLogger logger =
      L2pLogger.getInstance(ApplicationGenerator.class.getName());

  /**
   * Retrieves the guidance model from the template repository model
   * 
   * @param templateRepositoryName The name of the template repository
   * @param gitHubOrganization The organization that is used in the CAE
   * @return A guidance model
   */

  public static GuidanceModel getGuidances(String templateRepositoryName,
      String gitHubOrganization) {
    GuidanceModel guidanceModel = new GuidanceModel();
    try (TreeWalk treeWalk =
        Generator.getTemplateRepositoryContent(templateRepositoryName, gitHubOrganization)) {

      treeWalk.setFilter(PathFilter.create("frontend/"));
      ObjectReader reader = treeWalk.getObjectReader();

      // walk through the tree and retrieve the guidances
      while (treeWalk.next()) {
        ObjectId objectId = treeWalk.getObjectId(0);
        ObjectLoader loader = reader.open(objectId);

        switch (treeWalk.getNameString()) {
          case "guidances.json":
            String guidances = new String(loader.getBytes(), "UTF-8");
            guidanceModel.addGuidances(guidances);
            return guidanceModel;
        }
      }
    } catch (Exception e) {
      logger.printStackTrace(e);
    }

    return guidanceModel;
  }

  /**
   * The actual method that performs the checking.
   * <commit_msg>correct path filter for microservice guidances<commit_after>package i5.las2peer.services.codeGenerationService.templateEngine;

import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import i5.las2peer.logging.L2pLogger;
import i5.las2peer.services.codeGenerationService.generators.ApplicationGenerator;
import i5.las2peer.services.codeGenerationService.generators.Generator;
import i5.las2peer.services.codeGenerationService.traces.segments.CompositeSegment;
import i5.las2peer.services.codeGenerationService.traces.segments.Segment;
import i5.las2peer.services.codeGenerationService.traces.segments.SegmentFactory;
import i5.las2peer.services.codeGenerationService.traces.segments.UnprotectedSegment;

/**
 * The model checker class is responsible to find not allowed elements in unprotected segments of
 * the traced model elements.
 * 
 * @author <NAME>
 *
 */

public class ModelChecker {

  private static final L2pLogger logger =
      L2pLogger.getInstance(ApplicationGenerator.class.getName());

  /**
   * Retrieves the guidance model from the template repository model
   * 
   * @param templateRepositoryName The name of the template repository
   * @param gitHubOrganization The organization that is used in the CAE
   * @return A guidance model
   */

  public static GuidanceModel getGuidances(String templateRepositoryName,
      String gitHubOrganization) {
    GuidanceModel guidanceModel = new GuidanceModel();
    try (TreeWalk treeWalk =
        Generator.getTemplateRepositoryContent(templateRepositoryName, gitHubOrganization)) {
      String pathFilter =
          templateRepositoryName.indexOf("frontend") > -1 ? "frontend/" : "backend/";
      treeWalk.setFilter(PathFilter.create(pathFilter));
      ObjectReader reader = treeWalk.getObjectReader();

      // walk through the tree and retrieve the guidances
      while (treeWalk.next()) {
        ObjectId objectId = treeWalk.getObjectId(0);
        ObjectLoader loader = reader.open(objectId);

        switch (treeWalk.getNameString()) {
          case "guidances.json":
            String guidances = new String(loader.getBytes(), "UTF-8");
            guidanceModel.addGuidances(guidances);
            return guidanceModel;
        }
      }
    } catch (Exception e) {
      logger.printStackTrace(e);
    }

    return guidanceModel;
  }

  /**
   * The actual method that performs the checking.
   * 
{'fraction_non_alphanumeric': 0.08922904610789621, 'fraction_numerical': 0.00877356729512787, 'mean_word_length': 4.073863636363637, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, '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': '5137296', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1471, 'n_words': 335}