コード例 #1
0
ファイル: ResultSetModel.java プロジェクト: Khalefa/momo
  /**
   * this method adds a tuple
   *
   * @param t the tuple to add
   */
  public void addTuple(Tuple t) {
    // add the tuple to the list of tuples
    tuples.add(t);

    int i = 0;
    String s;
    // iterate over columns to determine maximum columnwidth
    for (Value v : t) {
      s = v.toString();
      if (s == null) {
        s = Constants.PRINT_NULL;
      } else {
        // update column width if value not null
        columnWidth[i] = Math.max(columnWidth[i], s.length());
      }

      // if stringbuffer creation is toggled on
      if (createSB) {
        stringBuffer.append(
            StringUtils.format(s, columnNames.get(i).length(), v.isNumeric()) + " ");
      }
      i++;
    }
    if (createSB) stringBuffer.append("\r\n");
  }
コード例 #2
0
ファイル: ResultSetModel.java プロジェクト: Khalefa/momo
  /**
   * 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;
  }