/**
   * Returns the IndexEntry describing the given section's table of contents.
   *
   * @param containerIndexEntry the {@link IntentIndexEntry} of the element containing the given
   *     section
   * @param candidateSectionEntry the candidate {@link IntentIndexEntry} to use for building this
   *     entry (can be null)
   * @param chapterIndex index of the chapter containing this section
   * @param section the section to consider
   * @return the entry corresponding to the given chapter's toc
   */
  private IntentIndexEntry computeEntryForSection(
      IntentIndexEntry containerIndexEntry,
      IntentIndexEntry candidateSectionEntry,
      String chapterIndex,
      IntentSection section) {
    // Step 1 : use an existing index entry if any
    IntentIndexEntry sectionEntry = candidateSectionEntry;
    if (candidateSectionEntry == null) {
      sectionEntry = IntentIndexerFactory.eINSTANCE.createIntentIndexEntry();
      containerIndexEntry.getSubEntries().add(sectionEntry);
    }

    // Step 2 : compute the title
    String sectionIndex = chapterIndex + getIndex(section);
    String title = StructuredElementHelper.getTitle(section);
    if ((title == null) || (title.length() < 1)) {
      title = "Untitled Section";
    }

    // Step 3 : setting index entry informations
    sectionEntry.setName(sectionIndex + " " + title);
    sectionEntry.setReferencedElement(section);
    sectionEntry.setType(INDEX_ENTRY_TYPE.INTENT_SECTION);

    // Step 4 : for each sub-section contained in this section
    int subSectionIt = 0;
    for (IntentSection subSection : section.getSubSections()) {
      // Step 4.1 : we use an existing index entry if any
      IntentIndexEntry candidateSubSectionEntry = null;
      if (sectionEntry.getSubEntries().size() > subSectionIt) {
        candidateSubSectionEntry = sectionEntry.getSubEntries().get(subSectionIt);
      }
      // Step 4.2 : and compute the entry for this sub-section
      computeEntryForSection(sectionEntry, candidateSubSectionEntry, sectionIndex, subSection);
      subSectionIt++;
    }
    // Step 5 : Removing old entries that are not used any more
    while (sectionEntry.getSubEntries().size() > subSectionIt) {
      sectionEntry.getSubEntries().remove(subSectionIt);
      subSectionIt++;
    }
    return sectionEntry;
  }
  /**
   * Replace the given index content with the given document's table of contents.
   *
   * @param index the index to update
   * @param document the Intent document
   */
  public void computeIndex(IntentIndex index, IntentDocument document) {
    // Step 1 : use an existing index entry if any
    IntentIndexEntry documentIndexEntry = null;
    if (index.getEntries().isEmpty()) {
      documentIndexEntry = IntentIndexerFactory.eINSTANCE.createIntentIndexEntry();
      index.getEntries().add(documentIndexEntry);
    } else {
      documentIndexEntry = index.getEntries().get(0);
    }

    // Step 2 : compute the title
    String title = StructuredElementHelper.getTitle(document);
    if ((title == null) || (title.length() < 1)) {
      title = "Document";
    }

    // Step 3 : setting index entry informations
    documentIndexEntry.setName(title);
    documentIndexEntry.setReferencedElement(document);
    documentIndexEntry.setType(INDEX_ENTRY_TYPE.INTENT_DOCUMENT);
    int chapterIt = 0;

    // Step 4 : for each chapter contained in this document
    for (IntentChapter chapter : document.getChapters()) {
      IntentIndexEntry candidateChapterEntry = null;
      // Step 4.1 : we use an existing index entry if any
      if (documentIndexEntry.getSubEntries().size() > chapterIt) {
        candidateChapterEntry = documentIndexEntry.getSubEntries().get(chapterIt);
      }
      // Step 4.2 : and compute the entry for this chapter
      computeEntryForChapter(documentIndexEntry, candidateChapterEntry, chapter);
      chapterIt++;
    }
    // Step 5 : Removing old entries that are not used any more
    while (documentIndexEntry.getSubEntries().size() > chapterIt) {
      documentIndexEntry.getSubEntries().remove(chapterIt);
      chapterIt++;
    }
  }