<commit_before>package fitnesse.components;

import static fitnesse.wiki.PageData.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import fitnesse.responders.search.ExecuteSearchPropertiesResponder;
import fitnesse.wiki.PageData;
import fitnesse.wiki.PageType;
import fitnesse.wiki.WikiPage;

public class AttributeWikiPageFinder extends WikiPageFinder {

  private static List<String> setUpPageNames = Arrays.asList("SetUp", "SuiteSetUp");
  private static List<String> tearDownPageNames = Arrays.asList("TearDown", "SuiteTearDown");

  private List<PageType> requestedPageTypes;
  private Map<String, Boolean> attributes;
  private List<String> suites;

  private static List<String> splitSuitesIntoArray(String suitesInput) {
    if (suitesInput == null)
      return null;

    if (isEmpty(suitesInput))
      return Collections.emptyList();

    return Arrays.asList(suitesInput.split("\\s*,\\s*"));
  }

  private static boolean isEmpty(String checkedString) {
    for (char character : checkedString.toCharArray()) {
      if (!Character.isWhitespace(character))
        return false;
    }
    return true;
  }

  public AttributeWikiPageFinder(TraversalListener observer,
      List<PageType> requestedPageTypes, Map<String, Boolean> attributes,
      List<String> suites) {
    super(observer);

    this.requestedPageTypes = requestedPageTypes;
    this.attributes = attributes;
    this.suites = suites;
  }

  public AttributeWikiPageFinder(TraversalListener observer,
      List<PageType> pageTypes, Map<String, Boolean> attributes, String suites) {
    this(observer, pageTypes, attributes, splitSuitesIntoArray(suites));
  }

  protected boolean pageMatches(WikiPage page) {

    if (!meetsSetUpTearDownConditions(page)) {
      return false;
    }

    PageData pageData = page.getData();

    if (!pageIsOfRequestedPageType(page, requestedPageTypes)) {
      return false;
    }

    for (Map.Entry<String, Boolean> input : attributes.entrySet()) {
      if ("SetUp".equals(input.getKey()) || "TearDown".equals(input.getKey()))
        continue;

      if (!attributeMatchesInput(pageData.hasAttribute(input.getKey()), input
          .getValue()))
        return false;
    }

    return suitesMatchInput(pageData, suites);
  }

  private boolean meetsSetUpTearDownConditions(WikiPage page) {

    if (attributes.containsKey("SetUp") && attributes.containsKey("TearDown")) {
      Boolean setupWanted = attributes.get("SetUp");
      Boolean teardownWanted = attributes.get("TearDown");

      if (setupWanted && teardownWanted) {
        return isSetUpPage(page) || isTearDownPage(page);
      }

      return isSetUpPage(page) == setupWanted && isTearDownPage(page) == teardownWanted;

    }

    if (attributes.containsKey("SetUp")) {
      return isSetUpPage(page) == attributes.get("SetUp");
    }

    if (attributes.containsKey("TearDown")) {
      return isTearDownPage(page) == attributes.get("TearDown");
    }

    return true;
  }

  private boolean isTearDownPage(WikiPage page) {
    return tearDownPageNames.contains(page.getName());
  }

  private boolean isSetUpPage(WikiPage page) {
    return setUpPageNames.contains(page.getName());
  }

  private boolean pageIsOfRequestedPageType(WikiPage page,
      List<PageType> requestedPageTypesEnum) {
    PageType pageType = PageType.fromWikiPage(page);

    return (requestedPageTypesEnum.contains(pageType));
  }

  protected boolean attributeMatchesInput(boolean attributeSet,
      boolean inputValueOn) {
    return attributeSet == inputValueOn;<commit_msg>Fix NPE when searching for tags only.<commit_after>package fitnesse.components;

import static fitnesse.wiki.PageData.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import fitnesse.responders.search.ExecuteSearchPropertiesResponder;
import fitnesse.wiki.PageData;
import fitnesse.wiki.PageType;
import fitnesse.wiki.WikiPage;

public class AttributeWikiPageFinder extends WikiPageFinder {

  private static List<String> setUpPageNames = Arrays.asList("SetUp", "SuiteSetUp");
  private static List<String> tearDownPageNames = Arrays.asList("TearDown", "SuiteTearDown");

  private List<PageType> requestedPageTypes;
  private Map<String, Boolean> attributes;
  private List<String> suites;

  private static List<String> splitSuitesIntoArray(String suitesInput) {
    if (suitesInput == null)
      return null;

    if (isEmpty(suitesInput))
      return Collections.emptyList();

    return Arrays.asList(suitesInput.split("\\s*,\\s*"));
  }

  private static boolean isEmpty(String checkedString) {
    for (char character : checkedString.toCharArray()) {
      if (!Character.isWhitespace(character))
        return false;
    }
    return true;
  }

  public AttributeWikiPageFinder(TraversalListener observer,
      List<PageType> requestedPageTypes, Map<String, Boolean> attributes,
      List<String> suites) {
    super(observer);

    this.requestedPageTypes = requestedPageTypes;
    this.attributes = attributes;
    this.suites = suites;
  }

  public AttributeWikiPageFinder(TraversalListener observer,
      List<PageType> pageTypes, Map<String, Boolean> attributes, String suites) {
    this(observer, pageTypes, attributes, splitSuitesIntoArray(suites));
  }

  protected boolean pageMatches(WikiPage page) {

    if (!meetsSetUpTearDownConditions(page)) {
      return false;
    }

    PageData pageData = page.getData();

    if (!pageIsOfRequestedPageType(page, requestedPageTypes)) {
      return false;
    }

    for (Map.Entry<String, Boolean> input : attributes.entrySet()) {
      if ("SetUp".equals(input.getKey()) || "TearDown".equals(input.getKey()))
        continue;

      if (!attributeMatchesInput(pageData.hasAttribute(input.getKey()), input
          .getValue()))
        return false;
    }

    return suitesMatchInput(pageData, suites);
  }

  private boolean meetsSetUpTearDownConditions(WikiPage page) {

    if (attributes.containsKey("SetUp") && attributes.containsKey("TearDown")) {
      Boolean setupWanted = attributes.get("SetUp");
      Boolean teardownWanted = attributes.get("TearDown");

      if (setupWanted && teardownWanted) {
        return isSetUpPage(page) || isTearDownPage(page);
      }

      return isSetUpPage(page) == setupWanted && isTearDownPage(page) == teardownWanted;

    }

    if (attributes.containsKey("SetUp")) {
      return isSetUpPage(page) == attributes.get("SetUp");
    }

    if (attributes.containsKey("TearDown")) {
      return isTearDownPage(page) == attributes.get("TearDown");
    }

    return true;
  }

  private boolean isTearDownPage(WikiPage page) {
    return tearDownPageNames.contains(page.getName());
  }

  private boolean isSetUpPage(WikiPage page) {
    return setUpPageNames.contains(page.getName());
  }

  private boolean pageIsOfRequestedPageType(WikiPage page,
      List<PageType> requestedPageTypesEnum) {
	
	if (requestedPageTypesEnum == null || requestedPageTypesEnum.isEmpty()) {
		return true;
	}
	
    PageType pageType = PageType.fromWikiPage(page);

    return (requestedPageTypesEnum.contains(pageType));
  }

  protected boolean attributeMatchesInput(boolean attributeSet,
      boolean inputValueOn) {
    return attributeSet == inputValueOn;
{'fraction_non_alphanumeric': 0.10941128261166508, 'fraction_numerical': 0.0, 'mean_word_length': 4.211387900355872, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '3430090', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2148, 'n_words': 435}