/**
   * Simply concatenate the bloc prefix to the serialized form of each BlockContent contained in the
   * given bloc.
   *
   * @param wikiElement Block to serialize.
   * @param dispatcher Dispatcher used to call the correct serialization for each content
   * @param blockPrefix prefix of the bloc (example : '*' for a bulleted list)
   * @return serialized form of this block.
   */
  public static String renderWithBlocPrefix(
      Block wikiElement, WikiTextElementDispatcher dispatcher, String blockPrefix) {

    // Step 1 : We first render the Block's prefix and style
    String renderedContent = renderPrefixAndStyle(wikiElement, blockPrefix);

    // Step 2 : We update the lastBlocWasPreformatted field
    if (prefixMatchesWithPreformattedBloc(blockPrefix)) {
      renderedContent = WikiTextResourceSerializer.LINE_BREAK + renderedContent;
      lastBlocWasPreformatted = true;
    } else {
      lastBlocWasPreformatted = false;
      if (prefixMatchesWithQuote(blockPrefix)) {
        renderedContent = WikiTextResourceSerializer.LINE_BREAK + renderedContent;
      }
    }

    // Step 3 : Calling the dispatcher to print all contents.
    String lastContent = "";
    for (BlockContent blocContent : wikiElement.getContent()) {
      renderedContent += lastContent;
      lastContent = dispatcher.doSwitch(blocContent);
    }
    if (!lastContent.equals(WikiTextResourceSerializer.LINE_BREAK)) {
      renderedContent += lastContent;
    }

    return renderedContent;
  }
  /**
   * Returns true if the previous element of the given block's container was a paragraph.
   *
   * @param block Block to analyse.
   * @return true if the previous element of the given block's container was a paragraph, false
   *     otherwise.
   */
  private static boolean lastElementWasParagraph(Block block) {
    List<EObject> containerContent = new ArrayList<EObject>(block.eContainer().eContents());
    int indiceBlock = containerContent.indexOf(block);

    // For each text contained in the same bloc and at the same level than the given text
    ListIterator<EObject> possibleNeighbours = containerContent.listIterator(indiceBlock);
    if (possibleNeighbours.hasPrevious()) {
      EObject previous = possibleNeighbours.previous();
      if (previous instanceof Paragraph) {
        EList<BlockContent> contents = ((Block) previous).getContent();
        return !(contents.get(contents.size() - 1) instanceof Image);
      }
    }
    return false;
  }
  /**
   * Simply concatenate the serialized form of each BlockContent contained in the given bloc.
   *
   * @param wikiElement Block to serialize.
   * @param dispatcher Dispatcher used to call the correct serialization for each content
   * @return serialized form of the given wikiElement.
   */
  public static String render(Block wikiElement, WikiTextElementDispatcher dispatcher) {

    String prefix = "";
    // If the given block has style and is not a TableCell, we have to render it with a prefix.
    if (!(wikiElement instanceof TableCell)
        && (AttributeStyleSerializer.renderAttributeForBloc(wikiElement.getAttributes()).length()
            > 0)) {
      prefix =
          WikiTextResourceSerializer.LINE_BREAK
              + PARAGRAPH_PREFIX
              + " "
              + WikiTextResourceSerializer.LINE_BREAK;
      return renderWithBlocPrefix(wikiElement, dispatcher, prefix)
          + WikiTextResourceSerializer.LINE_BREAK;
    } else {
      if (lastElementWasParagraph(wikiElement)) {
        prefix = WikiTextResourceSerializer.LINE_BREAK;
      }
    }
    return renderWithBlocPrefix(wikiElement, dispatcher, prefix);
  }
  /**
   * Return the string corresponding to the serialized form of the prefix and the styles of the
   * given bloc.
   *
   * @param block block to render
   * @param blockPrefix prefix of this bloc.
   * @return the string corresponding to the serialized form of the prefix and the styles of the
   *     given bloc.
   */
  private static String renderPrefixAndStyle(Block block, String blockPrefix) {
    String renderedPrefixAndStyle = "";
    if (blockPrefix.length() > 0) {
      String blockPrefixEnd = "";

      // We first get the beginning of the blockPrefix.
      if (blockPrefix.contains(DOT)) {
        blockPrefixEnd = blockPrefix.substring(blockPrefix.indexOf(DOT), blockPrefix.length());
        renderedPrefixAndStyle = blockPrefix.substring(0, blockPrefix.indexOf(DOT));
      } else {
        renderedPrefixAndStyle = blockPrefix;
      }
      // Now that we have the prefix, we insert the style
      renderedPrefixAndStyle +=
          AttributeStyleSerializer.renderAttributeForBloc(block.getAttributes());

      // We redress the dots at the end of the blockPrefix
      renderedPrefixAndStyle += blockPrefixEnd;
    }
    return renderedPrefixAndStyle;
  }