public RuleMatch acceptRuleMatch()

in languagetool-core/src/main/java/org/languagetool/rules/AbstractDateCheckWithSuggestionsFilter.java [79:212]


  public RuleMatch acceptRuleMatch(RuleMatch match, Map<String, String> args, int patternTokenPos,
                                   AnalyzedTokenReadings[] patternTokens, List<Integer> tokenPositions) {
    try {
      int dayOfWeekPos = getSkipCorrectedReference(tokenPositions, Integer.parseInt(getRequired("weekDay", args)));
      String dayOfWeekStr = patternTokens[dayOfWeekPos].getToken().replace("\u00AD", "");  // replace soft hyphen
      int dayPos;
      int monthPos;
      int yearPos;
      String dayStr;
      String monthStr;
      String yearStr;
      boolean isFullDateToken = false;
      int fullDatePos = getSkipCorrectedReference(tokenPositions, Integer.parseInt(getOptional("date", args, "-1"))); // format yyyy-mm-dd
      if (fullDatePos > -1) {
        String [] parts = patternTokens[fullDatePos].getToken().split("-");
        isFullDateToken = true;
        dayPos = fullDatePos;
        monthPos = fullDatePos;
        yearPos = fullDatePos;
        dayStr = parts[2];
        monthStr = parts[1];
        yearStr = parts[0];
      } else {
        dayPos = getSkipCorrectedReference(tokenPositions, Integer.parseInt(getRequired("day", args)));
        monthPos = getSkipCorrectedReference(tokenPositions, Integer.parseInt(getRequired("month", args)));
        yearPos = getSkipCorrectedReference(tokenPositions, Integer.parseInt(getOptional("year", args, "-1")));
        dayStr = patternTokens[dayPos].getToken();
        monthStr = patternTokens[monthPos].getToken();
        yearStr = (yearPos > -1 ? patternTokens[yearPos].getToken() : null);
      }
      int dayOfWeekFromString = getDayOfWeek(dayOfWeekStr);
      int day = getDayOfMonthFromStr(dayStr);
      int month = getMonthFromStr(monthStr);
      int year = getYearFromStr(yearStr);
      Calendar dateFromDate = getDate(day, month, year);
      int dayOfWeekFromDate = getDayOfWeekFromDate(dateFromDate);
      if (dayOfWeekFromDate == -1) {
        return null;
      }
      if (dayOfWeekFromString != dayOfWeekFromDate) {
        Calendar calFromDateString = Calendar.getInstance();
        calFromDateString.set(Calendar.DAY_OF_WEEK, dayOfWeekFromString);
        String message;
        // suggest changing the year (to current year)
        int currentYear = getYearFromStr(null);
        Calendar dateFromDateChangeYear = getDate(day, month, currentYear);
        int dayOfWeekFromDateChangeYear = getDayOfWeekFromDate(dateFromDateChangeYear);
        if (dayOfWeekFromString == dayOfWeekFromDateChangeYear) {
          message = getErrorMessageWrongYear().replace("{currentYear}", String.valueOf(currentYear));
          RuleMatch ruleMatch = new RuleMatch(match.getRule(), match.getSentence(),
            patternTokens[yearPos].getStartPos(), patternTokens[yearPos].getEndPos(), message, match.getShortMessage());
          ruleMatch.setType(match.getType());
          ruleMatch.setUrl(Tools.getUrl("https://www.timeanddate.com/calendar/?year=" + dateFromDateChangeYear.get(Calendar.YEAR)));
          if (isFullDateToken) {
            ruleMatch.setSuggestedReplacement(currentYear + "-" + monthStr + "-" + dayStr);
          } else {
            ruleMatch.setSuggestedReplacement(String.valueOf(currentYear));
          }
          return ruleMatch;
        }
        // suggest changing day of week or day of month
        message = match.getMessage()
          .replace("{realDay}", getDayOfWeek(dateFromDate))
          .replace("{day}", getDayOfWeek(calFromDateString))
          .replace("{currentYear}", Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
        int startIndex;
        int endIndex;
        if (dayOfWeekPos < dayPos) {
          startIndex = dayOfWeekPos;
          endIndex = dayPos;
        } else {
          startIndex = dayPos;
          endIndex = dayOfWeekPos;
        }
        RuleMatch ruleMatch = new RuleMatch(match.getRule(), match.getSentence(), patternTokens[startIndex].getStartPos(), patternTokens[endIndex].getEndPos(), message, match.getShortMessage());
        ruleMatch.setType(match.getType());
        ruleMatch.setUrl(Tools.getUrl("https://www.timeanddate.com/calendar/?year=" + dateFromDate.get(Calendar.YEAR)));
        // suggest changing day of week
        StringBuilder suggestion = new StringBuilder();
        boolean isFirst = true;
        for (int j = startIndex; j <= endIndex; j++) {
          if (isFirst) {
            isFirst = false;
          } else if (patternTokens[j].isWhitespaceBefore()) {
            suggestion.append(" ");
          }
          if (j == dayOfWeekPos) {
            suggestion.append(StringTools.preserveCase(getDayOfWeek(dateFromDate), dayOfWeekStr));
          } else {
            suggestion.append(patternTokens[j].getToken());
          }
        }
        if (!suggestion.toString().isEmpty()) {
          ruleMatch.setSuggestedReplacement(adjustSuggestion(suggestion.toString()));
        }
        // suggest changing day of month
        String correctedDayofMonth = findNewDayOfMonth(day, month, year, dayOfWeekFromString);
        if (!correctedDayofMonth.isEmpty()) {
          suggestion = new StringBuilder();
          isFirst = true;
          for (int j = startIndex; j <= endIndex; j++) {
            if (isFirst) {
              isFirst = false;
            } else if (patternTokens[j].isWhitespaceBefore()) {
              suggestion.append(" ");
            }
            if (j == dayPos) {
              if (isFullDateToken) {
                suggestion.append(yearStr+"-"+monthStr+"-"+correctedDayofMonth);
              } else {
                suggestion.append(getDayStrLikeOriginal(correctedDayofMonth, dayStr));
              }
            } else {
              suggestion.append(patternTokens[j].getToken());
            }
          }
          if (!suggestion.toString().isEmpty()) {
            ruleMatch.addSuggestedReplacement(adjustSuggestion(suggestion.toString()));
          }
        }
        return ruleMatch;
      } else {
        return null;
      }
    } catch (IllegalArgumentException e) {
      throw e;
    } catch (RuntimeException e) {
      // this can happen with some special characters which the Java regex matches but which the Java code
      // cannot map to days, e.g. German "Dıenstag" vs "Dienstag" (note the difference on the second character -
      // the first word is not valid, but it should not crash LT):
      //logger.warn("Skipping potential match for " + match.getRule().getFullId(), e);
      return null;
    }
  }