Ejemplo n.º 1
0
  public void initialiseWithLines(ArrayList<StringBuilder> lines, ProcessingOptions options)
      throws UnsupportedEncodingException {

    // remove blank rows at the bottom
    boolean done = false;
    int i;
    for (i = lines.size() - 1; i >= 0 && !done; i--) {
      StringBuilder row = lines.get(i);
      if (!StringUtils.isBlank(row.toString())) done = true;
    }
    rows = new ArrayList<StringBuilder>(lines.subList(0, i + 2));

    if (options != null) fixTabs(options.getTabSize());
    else fixTabs(ProcessingOptions.DEFAULT_TAB_SIZE);

    // make all lines of equal length
    // add blank outline around the buffer to prevent fill glitch
    // convert tabs to spaces (or remove them if setting is 0)

    int blankBorderSize = 2;

    int maxLength = 0;
    int index = 0;

    String encoding = null;
    if (options != null) encoding = options.getCharacterEncoding();

    Iterator<StringBuilder> it = rows.iterator();
    while (it.hasNext()) {
      String row = it.next().toString();
      if (encoding != null) {
        byte[] bytes = row.getBytes();
        row = new String(bytes, encoding);
      }
      if (row.length() > maxLength) maxLength = row.length();
      rows.set(index, new StringBuilder(row));
      index++;
    }

    it = rows.iterator();
    ArrayList<StringBuilder> newRows = new ArrayList<StringBuilder>();
    // TODO: make the following depend on blankBorderSize

    StringBuilder topBottomRow =
        new StringBuilder(StringUtils.repeatString(" ", maxLength + blankBorderSize * 2));

    newRows.add(topBottomRow);
    newRows.add(topBottomRow);
    while (it.hasNext()) {
      StringBuilder row = it.next();

      if (row.length() < maxLength) {
        String borderString = StringUtils.repeatString(" ", blankBorderSize);
        StringBuilder newRow = new StringBuilder();

        newRow.append(borderString);
        newRow.append(row);
        newRow.append(StringUtils.repeatString(" ", maxLength - row.length()));
        newRow.append(borderString);

        newRows.add(newRow);
      } else { // TODO: why is the following line like that?
        newRows.add(new StringBuilder("  ").append(row).append("  "));
      }
    }
    // TODO: make the following depend on blankBorderSize
    newRows.add(topBottomRow);
    newRows.add(topBottomRow);
    rows = newRows;

    replaceBullets();
    replaceHumanColorCodes();
  }