Exemplo n.º 1
0
  private int[] colWidths(ResultSetRewindable rs) {
    int numCols = rs.getResultVars().size();
    int numRows = 0;
    int[] colWidths = new int[numCols];

    // Widths at least that of the variable name.  Assumes we will print col headings.
    for (int i = 0; i < numCols; i++) colWidths[i] = (rs.getResultVars().get(i)).length();

    // Preparation pass : find the maximum width for each column
    for (; rs.hasNext(); ) {
      numRows++;
      QuerySolution rBind = rs.nextSolution();
      int col = -1;
      for (String s1 : rs.getResultVars()) {
        col++;
        String rVar = s1;
        String s = getVarValueAsString(rBind, rVar);
        if (colWidths[col] < s.length()) {
          colWidths[col] = s.length();
        }
      }
    }
    rs.reset();
    return colWidths;
  }
Exemplo n.º 2
0
  /**
   * Textual representation : layout using given separator. Ensure the PrintWriter can handle UTF-8.
   *
   * @param pw PrintWriter
   * @param colSep Column separator
   */
  public void write(
      PrintWriter pw, ResultSet resultSet, String colStart, String colSep, String colEnd) {
    if (resultSet.getResultVars().size() == 0) {
      pw.println("==== No variables ====");
      // return ;
    }

    ResultSetRewindable resultSetRewindable = ResultSetFactory.makeRewindable(resultSet);

    int numCols = resultSetRewindable.getResultVars().size();
    int[] colWidths = colWidths(resultSetRewindable);

    String row[] = new String[numCols];
    int lineWidth = 0;
    for (int col = 0; col < numCols; col++) {
      String rVar = resultSet.getResultVars().get(col);
      row[col] = rVar;
      lineWidth += colWidths[col];
      if (col > 0) lineWidth += colSep.length();
    }
    if (colStart != null) lineWidth += colStart.length();
    if (colEnd != null) lineWidth += colEnd.length();

    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();

    printRow(pw, row, colWidths, colStart, colSep, colEnd);

    for (int i = 0; i < lineWidth; i++) pw.print('=');
    pw.println();

    for (; resultSetRewindable.hasNext(); ) {
      QuerySolution rBind = resultSetRewindable.nextSolution();
      for (int col = 0; col < numCols; col++) {
        String rVar = resultSet.getResultVars().get(col);
        row[col] = this.getVarValueAsString(rBind, rVar);
      }
      printRow(pw, row, colWidths, colStart, colSep, colEnd);
    }
    for (int i = 0; i < lineWidth; i++) pw.print('-');
    pw.println();
    resultSetRewindable = null;
  }