private void createMatch()

in languagetool-core/src/main/java/org/languagetool/rules/AbstractSimpleReplaceRule2.java [228:340]


  private void createMatch(List<RuleMatch> ruleMatches, SuggestionWithMessage suggestionWithMessage, int startIndex,
                           int endIndex, String originalStr, AnalyzedTokenReadings[] tokens, AnalyzedSentence sentence,
                           int sentStart, int[] checkCaseCoveredUpto) {
    if (suggestionWithMessage == null || isException(originalStr)) {
      return;
    }
    List<String> replacements = Arrays.asList(suggestionWithMessage.getSuggestion().split("\\|"));
    int fromPos = tokens[startIndex].getStartPos();
    int toPos = tokens[endIndex].getEndPos();
    //keep only the longest match
    if (ruleMatches.size() > 0) {
      RuleMatch lastRuleMatch = ruleMatches.get(ruleMatches.size() - 1);
      if (lastRuleMatch.getFromPos() <= fromPos
        && lastRuleMatch.getToPos() >= toPos) {
        return;
      }
    }
    boolean firstWordInSuggIsCamelCase = replacements.stream().anyMatch(k -> StringTools.isCamelCase(k.split(" ")[0]));
    boolean isAllUppercase = StringTools.isAllUppercase(originalStr);
    boolean isCapitalized = StringTools.isCapitalizedWord(originalStr.split(" ")[0]);
    // exceptions for checking case
    if (isCheckingCase()) {
      if (endIndex <= checkCaseCoveredUpto[0]) {
        return;
      }
      String replacementCheckCase = replacements.get(0);
      if ((sentStart == startIndex && originalStr.equals(StringTools.uppercaseFirstChar(replacementCheckCase)))
        || originalStr.equals(replacementCheckCase)) {
        if (ruleMatches.size() > 0) {
          // remove last match if is contained in a correct phrase
          RuleMatch lastRuleMatch = ruleMatches.get(ruleMatches.size() - 1);
          if (lastRuleMatch.getToPos() > fromPos) {
            ruleMatches.remove(ruleMatches.size() - 1);
          }
        }
        checkCaseCoveredUpto[0] = endIndex;
        return;
      }
      //Allow all-upper case, except for CamelCase and short words in Dutch (length<MAX_LENGTH_SHORT_WORDS)
      if (!firstWordInSuggIsCamelCase && originalStr.equals(originalStr.toUpperCase())) {
        if (ignoreShortUppercaseWords || originalStr.length() > MAX_LENGTH_SHORT_WORDS) {
          checkCaseCoveredUpto[0] = endIndex;
          return;
        }
      }
    }
    List<String> finalReplacements = new ArrayList<>();
    // adjust casing of suggestions
    for (String repl : replacements) {
      String finalRepl = repl;
      if (!firstWordInSuggIsCamelCase && (sentStart == startIndex || (isCapitalized && !isCheckingCase()))) {
        finalRepl = StringTools.uppercaseFirstChar(repl);
      }
      if (!isCheckingCase() && isAllUppercase) {
        finalRepl = repl.toUpperCase();
      }
      if (!repl.equals(originalStr) && !finalRepl.equals(originalStr) && !finalReplacements.contains(finalRepl)) {
        finalReplacements.add(finalRepl);
      }
      if (finalRepl.equals(originalStr)) {
        // the original is correct, so there is no need to fix anything
        finalReplacements.clear();
        break;
      }
    }
    if (ruleHasSuggestions && finalReplacements.isEmpty()) {
      return;
    }
    // Begin of match creation
    String msg = suggestionWithMessage.getMessage();
    String url = null;
    if (msg != null && (msg.startsWith("http://") || msg.startsWith("https://"))) {
      msg = null;
      url = msg;
    }
    if (msg == null) {
      String msgSuggestions = "";
      for (int k = 0; k < replacements.size(); k++) {
        if (k > 0) {
          msgSuggestions += (k == replacements.size() - 1 ? getSuggestionsSeparator() : ", ");
        }
        msgSuggestions += "<suggestion>" + replacements.get(k) + "</suggestion>";
      }
      msg = getMessage().replaceFirst("\\$match", originalStr).replaceFirst("\\$suggestions", msgSuggestions);
    }
    RuleMatch ruleMatch = new RuleMatch(this, sentence, fromPos, toPos, msg, getShort());
    if (subRuleSpecificIds) {
      String id = StringTools.toId(getId() + "_" + originalStr, language);
      String desc = getDescription().replace("$match", originalStr);
      SpecificIdRule specificIdRule = new SpecificIdRule(id, desc, isPremium(), getCategory(),
        getLocQualityIssueType(), getTags());
      ruleMatch = new RuleMatch(specificIdRule, sentence, fromPos, toPos, msg, getShort());
    }
    if (url != null) {
      ruleMatch.setUrl(Tools.getUrl(url));
    }
    if (ruleHasSuggestions) {
      ruleMatch.setSuggestedReplacements(finalReplacements);
    }
    // End of match creation
    if (isRuleMatchException(ruleMatch)) {
      return;
    }
    //keep only the longest match
    if (ruleMatches.size() > 0) {
      RuleMatch lastRuleMatch = ruleMatches.get(ruleMatches.size() - 1);
      if (lastRuleMatch.getFromPos() >= fromPos
        && lastRuleMatch.getToPos() <= toPos) {
        ruleMatches.remove(ruleMatches.size() - 1);
      }
    }
    ruleMatches.add(ruleMatch);
  }