summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/ioutil/ColumnFormatter.java
blob: ae855c7474d8330f7d790d7c72e2cdb98e52d8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.ioutil;

import java.io.PrintWriter;

/**
 * Simple output formatter for column-oriented data, writing its output to a {@link
 * java.io.PrintWriter} object. Handles escaping of the column data so that the resulting output is
 * unambiguous and reasonably safe and machine parsable.
 */
public class ColumnFormatter {
  private char columnSeparator;
  private boolean firstColumn;
  private final PrintWriter out;

  /**
   * @param out The writer to which output should be sent.
   * @param columnSeparator A character that should serve as the separator token between columns of
   *     output. As only non-printable characters in the column text are ever escaped, the column
   *     separator must be a non-printable character if the output needs to be unambiguously parsed.
   */
  public ColumnFormatter(PrintWriter out, char columnSeparator) {
    this.out = out;
    this.columnSeparator = columnSeparator;
    this.firstColumn = true;
  }

  /**
   * Adds a text string as a new column in the current line of output, taking care of escaping as
   * necessary.
   *
   * @param content the string to add.
   */
  public void addColumn(String content) {
    if (!firstColumn) {
      out.print(columnSeparator);
    }
    out.print(StringUtil.escapeString(content));
    firstColumn = false;
  }

  /**
   * Finishes the output by flushing the current line and takes care of any other cleanup action.
   */
  public void finish() {
    nextLine();
    out.flush();
  }

  /**
   * Flushes the current line of output and makes the formatter ready to start receiving new column
   * data for a new line (or end-of-file). If the current line is empty nothing is done, i.e.
   * consecutive calls to this method without intervening calls to {@link #addColumn} will be
   * squashed.
   */
  public void nextLine() {
    if (!firstColumn) {
      out.print('\n');
      firstColumn = true;
    }
  }
}