示例#1
0
  /**
   * this method returns a StringBuffer containing the contents of this ResultSetModel
   *
   * @param newLine the LineDelimiter for a newline character
   * @return a StringBuffer of the contents
   */
  public StringBuffer getStringBufferFromRSM(String newLine) {
    int columnCount = columnNames.size(); // number of columns

    if (!createSB) {
      stringBuffer = new StringBuffer();
      // print data
      Iterator<Tuple> iter = tuples.iterator();
      Tuple tuple;
      String valueFreeOfSpecialChars;
      while (iter.hasNext()) { // for each row
        tuple = iter.next();
        for (int column = 0; column < columnCount; column++) { // for each column
          // print value
          Value value = tuple.getValue(column);
          if (!value.isNull()) {
            valueFreeOfSpecialChars = StringUtils.replaceDelChars(value.toString());
            stringBuffer.append(
                StringUtils.format(
                    valueFreeOfSpecialChars, columnWidth[column], value.isNumeric()));
          } else {
            stringBuffer.append(
                StringUtils.format(Constants.PRINT_NULL, columnWidth[column], value.isNumeric()));
          }
          if (column < columnCount) {
            stringBuffer.append(' ');
          }
        }
        stringBuffer.append(newLine);
      }
      stringBuffer.append(newLine);
      // toggle flag to get into other path, when next request for this model
      createSB = true;
    }

    StringBuffer sb = new StringBuffer(); // create new StringBuffer

    // print column headers
    for (int column = 0; column < columnCount; column++) {
      // retrieve information about the column
      String columnName = columnNames.get(column);
      // print column name
      sb.append(StringUtils.format(columnName, columnWidth[column], false)).append(" ");
    }
    sb.append(newLine);

    // print separator line
    for (int column = 0; column < columnCount; column++) {
      for (int i = 0; i < columnWidth[column]; i++) {
        sb.append('-');
      }
      sb.append(' ');
    }
    sb.append(newLine);

    // use already created stringbuffer to print out results
    sb.append(stringBuffer);

    // finally print tuple count
    sb.append(newLine + tuples.size() + " row(s) selected.");
    return sb;
  }