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

com.acrolinx.sidebar.lookup.LookupRangesDiff Maven / Gradle / Ivy

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

import com.acrolinx.sidebar.LookupRanges;
import com.acrolinx.sidebar.pojo.document.AbstractMatch;
import com.acrolinx.sidebar.pojo.document.AcrolinxMatch;
import com.acrolinx.sidebar.pojo.document.IntRange;
import com.acrolinx.sidebar.pojo.document.externalcontent.ExternalContent;
import com.acrolinx.sidebar.pojo.document.externalcontent.ExternalContentField;
import com.acrolinx.sidebar.pojo.document.externalcontent.ExternalContentMatch;
import com.acrolinx.sidebar.utils.DiffMatchPatch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LookupRangesDiff extends LookupRanges {
  private static final Logger logger = LoggerFactory.getLogger(LookupRangesDiff.class);

  @Override
  public Optional> getMatchesWithCorrectedRanges(
      String checkedText, String changedText, List abstractMatches) {
    List diffs = Lookup.getDiffs(checkedText, changedText);
    List offsetMappingArray = Lookup.createOffsetMappingArray(diffs);
    List returnValues = new ArrayList<>();
    boolean anyEmpty =
        abstractMatches.stream()
            .anyMatch(
                abstractMatch -> {
                  Optional correctedMatch =
                      Lookup.getCorrectedMatch(
                          diffs,
                          offsetMappingArray,
                          abstractMatch.getRange().getMinimumInteger(),
                          abstractMatch.getRange().getMaximumInteger());

                  if (correctedMatch.isPresent()) {
                    AbstractMatch copy = abstractMatch.setRange(correctedMatch.get());
                    returnValues.add(copy);
                    return false;
                  }

                  return true;
                });

    if (anyEmpty) {
      return Optional.empty();
    }

    return Optional.of(Collections.unmodifiableList(returnValues));
  }

  public List getMatchesIncludingCorrectedExternalMatches(
      ExternalContent checkedExternalContent,
      ExternalContent changedExternalContent,
      List abstractMatches) {
    return abstractMatches.stream()
        .map(
            abstractMatch -> {
              if (!(abstractMatch instanceof AcrolinxMatch)) {
                return abstractMatch;
              }

              AcrolinxMatch acrolinxMatch = (AcrolinxMatch) abstractMatch;

              if (!acrolinxMatch.hasExternalContentMatches()) {
                return abstractMatch;
              }

              List externalContentMatches =
                  acrolinxMatch.getExternalContentMatches();

              List correctedMatches =
                  getExternalContentMatchesWithCorrectedRanges(
                      externalContentMatches, checkedExternalContent, changedExternalContent);

              return new AcrolinxMatch(
                  abstractMatch.getRange(),
                  ((AcrolinxMatch) abstractMatch).getExtractedRange(),
                  abstractMatch.getContent(),
                  correctedMatches);
            })
        .collect(Collectors.toList());
  }

  public List getExternalContentMatchesWithCorrectedRanges(
      List matches,
      ExternalContent checkedText,
      ExternalContent changedText) {
    List checkedExternalContent = checkedText.getAll();
    List changedExternalContent = changedText.getAll();

    return matches.stream()
        .map(
            match -> {
              if (!match.getExternalContentMatches().isEmpty()) {
                List newMatches =
                    getExternalContentMatchesWithCorrectedRanges(
                        match.getExternalContentMatches(), checkedText, changedText);
                match.setExternalContentMatches(newMatches);
              }

              return adJustMatch(match, checkedExternalContent, changedExternalContent);
            })
        .collect(Collectors.toList());
  }

  public ExternalContentMatch adJustMatch(
      ExternalContentMatch match,
      List checkedExternalContent,
      List changedExternalContent) {
    Optional optionalCheckedField =
        checkedExternalContent.stream()
            .filter((ExternalContentField old) -> old.getId().equals(match.getId()))
            .findFirst();
    Optional optionalChangedField =
        changedExternalContent.stream()
            .filter((ExternalContentField old) -> old.getId().equals(match.getId()))
            .findFirst();

    if (optionalCheckedField.isEmpty() || optionalChangedField.isEmpty()) {
      return match;
    }

    ExternalContentField checkedField = optionalCheckedField.get();
    ExternalContentField changedField = optionalChangedField.get();

    List diffs =
        Lookup.getDiffs(checkedField.getContent(), changedField.getContent());
    List offsetMappingArray = Lookup.createOffsetMappingArray(diffs);

    Optional correctedMatch =
        Lookup.getCorrectedMatch(
            diffs,
            offsetMappingArray,
            match.getRange().getMinimumInteger(),
            match.getRange().getMaximumInteger());

    if (correctedMatch.isPresent()) {
      return match.setRange(correctedMatch.get());
    }

    logger.warn("Could not adjust external Content Match");
    return match;
  }

  public static Optional getOffSetDiffStart(
      String originalVersion, String changedVersion, int offsetInOriginalVersion) {
    List diffs = Lookup.getDiffs(originalVersion, changedVersion);
    List offsetMappingArray = Lookup.createOffsetMappingArray(diffs);
    return Lookup.getDiffOffsetPositionStart(offsetMappingArray, offsetInOriginalVersion);
  }

  public static Optional getOffSetDiffEnd(
      String originalVersion, String changedVersion, int offsetInOrignalVersion) {
    List diffs = Lookup.getDiffs(originalVersion, changedVersion);
    List offsetMappingArray = Lookup.createOffsetMappingArray(diffs);
    return Lookup.getDiffOffsetPositionEnd(offsetMappingArray, offsetInOrignalVersion);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy