/**
   * Analyze a page to check if errors are present.
   *
   * @param analysis Page analysis.
   * @param errors Errors found in the page.
   * @param onlyAutomatic True if analysis could be restricted to errors automatically fixed.
   * @return Flag indicating if the error was found.
   */
  @Override
  public boolean analyze(
      PageAnalysis analysis, Collection<CheckErrorResult> errors, boolean onlyAutomatic) {
    if ((analysis == null) || (analysis.getPage() == null)) {
      return false;
    }

    // Analyze each title
    String contents = analysis.getContents();
    boolean result = false;
    for (PageElementTitle title : analysis.getTitles()) {

      // Check if there's something in the title
      boolean textFound = false;
      int currentIndex = title.getBeginIndex();
      int lastIndex = title.getEndIndex();
      while ((currentIndex < lastIndex) && (contents.charAt(currentIndex) == '=')) {
        currentIndex++;
      }
      while (!textFound && (currentIndex < lastIndex) && (contents.charAt(currentIndex) != '=')) {
        currentIndex = getFirstIndexAfterSpace(contents, currentIndex);
        if (currentIndex < lastIndex) {
          PageElementComment comment = null;
          PageElementTag tag = null;
          char currentChar = contents.charAt(currentIndex);
          if (currentChar == '<') {
            comment = analysis.isInComment(currentIndex);
            tag = analysis.isInTag(currentIndex, PageElementTag.TAG_WIKI_NOWIKI);
          }
          if (comment != null) {
            currentIndex = comment.getEndIndex();
          } else if (tag != null) {
            currentIndex = tag.getCompleteEndIndex();
          } else if (currentChar != '=') {
            if (!Character.isWhitespace(currentChar)) {
              textFound = true;
            }
            currentIndex++;
          }
        }
      }

      // Report error
      if (!textFound) {
        if (errors == null) {
          return true;
        }
        result = true;
        if ((lastIndex < contents.length()) && (contents.charAt(lastIndex) == '\n')) {
          lastIndex++;
        }
        CheckErrorResult errorResult =
            createCheckErrorResult(analysis, title.getBeginIndex(), lastIndex);
        errorResult.addReplacement("");
        errors.add(errorResult);
      }
    }

    return result;
  }
  /**
   * Analyze a page to check if errors are present.
   *
   * @param analysis Page analysis.
   * @param errors Errors found in the page.
   * @param onlyAutomatic True if analysis could be restricted to errors automatically fixed.
   * @return Flag indicating if the error was found.
   */
  @Override
  public boolean analyze(
      PageAnalysis analysis, Collection<CheckErrorResult> errors, boolean onlyAutomatic) {
    if (analysis == null) {
      return false;
    }

    // Check level of each title
    List<PageElementTitle> titles = analysis.getTitles();
    if ((titles == null) || (titles.size() == 0)) {
      return false;
    }
    for (PageElementTitle title : titles) {
      if (title.getLevel() < 3) {
        return false;
      }
    }

    if (errors == null) {
      return true;
    }
    CheckErrorResult errorResult =
        createCheckErrorResult(
            analysis, titles.get(0).getBeginIndex(), titles.get(0).getEndIndex());
    if (titles.size() == 1) {
      errorResult.addReplacement(
          PageElementTitle.createTitle(2, titles.get(0).getTitle(), titles.get(0).getAfterTitle()));
    }
    errorResult.addEditTocAction(titles.get(0));
    errors.add(errorResult);
    return true;
  }
  /**
   * Bot fixing of all the errors in the page.
   *
   * @param analysis Page analysis.
   * @return Page contents after fix.
   */
  @Override
  protected String internalBotFix(PageAnalysis analysis) {
    String contents = analysis.getContents();
    if (!analysis.areTitlesReliable()) {
      return contents;
    }

    // Compute minimum title level
    List<PageElementTitle> titles = analysis.getTitles();
    if ((titles == null) || (titles.size() == 0)) {
      return contents;
    }
    int minTitle = Integer.MAX_VALUE;
    for (PageElementTitle title : titles) {
      if (title.getLevel() < minTitle) {
        minTitle = title.getLevel();
      }
    }
    if (minTitle < 3) {
      return contents;
    }

    // Replace titles
    StringBuilder tmp = new StringBuilder();
    int lastIndex = 0;
    int offset = minTitle - 2;
    for (PageElementTitle title : titles) {
      if (lastIndex < title.getBeginIndex()) {
        tmp.append(contents.substring(lastIndex, title.getBeginIndex()));
        lastIndex = title.getBeginIndex();
      }
      tmp.append(
          PageElementTitle.createTitle(
              title.getLevel() - offset, title.getTitle(), title.getAfterTitle()));
      if (title.getAfterTitle() != null) {
        tmp.append(title.getAfterTitle());
      }
      lastIndex = title.getEndIndex();
    }
    if (lastIndex < contents.length()) {
      tmp.append(contents.substring(lastIndex));
    }

    return tmp.toString();
  }