All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.acrolinx.sidebar.utils.MatchUtils Maven / Gradle / Ivy

/* Copyright (c) 2018 Acrolinx GmbH */
package com.acrolinx.sidebar.utils;

import com.acrolinx.sidebar.pojo.document.AbstractMatch;
import com.acrolinx.sidebar.pojo.document.ExternalAbstractMatch;
import com.acrolinx.sidebar.pojo.document.externalcontent.ExternalContentField;
import com.acrolinx.sidebar.pojo.document.externalcontent.ExternalContentMatch;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class MatchUtils {
  private static final Logger logger = LoggerFactory.getLogger(MatchUtils.class);

  private MatchUtils() {
    throw new IllegalStateException();
  }

  public static List filterDangerousMatches(
      List list, String lastCheckedContent) {
    return filterDangerousMatches(list, lastCheckedContent, new ArrayList<>());
  }

  public static List filterDangerousMatches(
      List list,
      String lastCheckedContent,
      List lastCheckedExternalContent) {
    return list.stream()
        .filter(
            abstractMatch -> {
              // todo: does the external content contain tags that are not external content matches?
              if (abstractMatch instanceof ExternalAbstractMatch
                  && ((ExternalAbstractMatch) abstractMatch).hasExternalContentMatches()) {
                return isExternalContentMatchAXMLTag(
                    (ExternalAbstractMatch) abstractMatch, lastCheckedExternalContent);
              }

              String matchContent =
                  lastCheckedContent.substring(
                      abstractMatch.getRange().getMinimumInteger(),
                      abstractMatch.getRange().getMaximumInteger());
              logger.debug("Checking if match is a tag");

              boolean isTag = matchContent.matches("");
              logger.debug("Is match a tag: {}", isTag);
              return !isTag;
            })
        .collect(Collectors.toList());
  }

  public static boolean isExternalContentMatchAXMLTag(
      ExternalAbstractMatch externalAbstractMatch,
      List lastCheckedExternalContent) {
    ExternalContentMatch externalContentMatch =
        externalAbstractMatch.getExternalContentMatches().get(0);

    while (!externalContentMatch.getExternalContentMatches().isEmpty()) {
      externalContentMatch = externalContentMatch.getExternalContentMatches().get(0);
    }

    final String externalContentId = externalContentMatch.getId();
    Optional optionalExternalContentField =
        lastCheckedExternalContent.stream()
            .filter(externalContentField -> externalContentField.getId().equals(externalContentId))
            .findFirst();

    if (optionalExternalContentField.isEmpty()) {
      logger.warn("Field for externalContentMatch has not been found");
      return false;
    }

    ExternalContentField externalContentField = optionalExternalContentField.get();
    String content = externalContentField.getContent();
    String matchContent =
        content.substring(
            externalContentMatch.getRange().getMinimumInteger(),
            externalContentMatch.getRange().getMaximumInteger());
    return !matchContent.matches("");
  }

  public static  List sortByOffsetDesc(final List matches) {
    final List sortedMatches = new ArrayList<>(matches);
    sortedMatches.sort(
        (first, second) -> {
          final int start =
              first.getRange().getMinimumInteger() - second.getRange().getMinimumInteger();

          if (start == 0) {
            final int end =
                first.getRange().getMaximumInteger() - second.getRange().getMaximumInteger();

            if (end == 0) {
              return 0;
            }

            return (end > 0) ? -1 : +1;
          }

          return start > 0 ? -1 : +1;
        });

    return sortedMatches;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy