mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Mark Craig
30.46.2015 d928438fd1d461915babc0464c95fd98d531c4b2
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2010 Sun Microsystems, Inc.
 *      Portions copyright 2012-2015 ForgeRock AS.
 */
package com.forgerock.opendj.cli;
 
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
 
/**
 * Utility class for printing aligned columns of text.
 * <P>
 * This class allows you to specify:
 * <UL>
 * <LI>The number of columns in the output. This will determine the dimension of
 * the string arrays passed to add(String[]) or addTitle(String[]).
 * <LI>spacing/gap between columns
 * <LI>character to use for title border (null means no border)
 * <LI>column alignment. Only LEFT/CENTER is supported for now.
 * </UL>
 * <P>
 * Example usage:
 *
 * <PRE>
 * MyPrinter mp = new MyPrinter(3, 2, &quot;-&quot;);
 * String oneRow[] = new String[3];
 * oneRow[0] = &quot;User Name&quot;;
 * oneRow[1] = &quot;Email Address&quot;;
 * oneRow[2] = &quot;Phone Number&quot;;
 * mp.addTitle(oneRow);
 * oneRow[0] = &quot;Bob&quot;;
 * oneRow[1] = &quot;bob@foo.com&quot;;
 * oneRow[2] = &quot;123-4567&quot;;
 * mp.add(oneRow);
 * oneRow[0] = &quot;John&quot;;
 * oneRow[1] = &quot;john@foo.com&quot;;
 * oneRow[2] = &quot;456-7890&quot;;
 * mp.add(oneRow);
 * mp.print();
 * </PRE>
 * <P>
 * The above would print:
 * <P>
 *
 * <PRE>
 *  --------------------------------------
 *  User Name  Email Address  Phone Number
 *  --------------------------------------
 *  Bob        bob@foo.com    123-4567
 *  John       john@foo.com   456-7890
 * </PRE>
 * <P>
 * This class also supports multi-row titles and having title strings spanning
 * multiple columns. Example usage:
 *
 * <PRE>
 * TestPrinter tp = new TestPrinter(4, 2, &quot;-&quot;);
 * String oneRow[] = new String[4];
 * int[] span = new int[4];
 * span[0] = 2; // spans 2 columns
 * span[1] = 0; // spans 0 columns
 * span[2] = 2; // spans 2 columns
 * span[3] = 0; // spans 0 columns
 * tp.setTitleAlign(CENTER);
 * oneRow[0] = &quot;Name&quot;;
 * oneRow[1] = &quot;&quot;;
 * oneRow[2] = &quot;Contact&quot;;
 * oneRow[3] = &quot;&quot;;
 * tp.addTitle(oneRow, span);
 * oneRow[0] = &quot;First&quot;;
 * oneRow[1] = &quot;Last&quot;;
 * oneRow[2] = &quot;Email&quot;;
 * oneRow[3] = &quot;Phone&quot;;
 * tp.addTitle(oneRow);
 * oneRow[0] = &quot;Bob&quot;;
 * oneRow[1] = &quot;Jones&quot;;
 * oneRow[2] = &quot;bob@foo.com&quot;;
 * oneRow[3] = &quot;123-4567&quot;;
 * tp.add(oneRow);
 * oneRow[0] = &quot;John&quot;;
 * oneRow[1] = &quot;Doe&quot;;
 * oneRow[2] = &quot;john@foo.com&quot;;
 * oneRow[3] = &quot;456-7890&quot;;
 * tp.add(oneRow);
 * tp.println();
 * </PRE>
 * <P>
 * The above would print:
 * <P>
 *
 * <PRE>
 *      ------------------------------------
 *          Name             Contact
 *      First  Last      Email       Phone
 *      ------------------------------------
 *      Bob    Jones  bob@foo.com   123-4567
 *      John   Doe    john@foo.com  456-7890
 * </PRE>
 */
public final class MultiColumnPrinter {
 
    /** Left ID. */
    public static final int LEFT = 0;
    /** Center ID. */
    public static final int CENTER = 1;
    /** Right ID. */
    public static final int RIGHT = 2;
 
    private int numCol = 2;
    private int gap = 4;
 
    private int align = CENTER;
    private int titleAlign = CENTER;
 
    private String border;
    private final List<String[]> titleTable = new Vector<>();
    private final List<int[]> titleSpanTable = new Vector<>();
    private final int[] curLength;
 
    private final ConsoleApplication app;
 
    /**
     * Creates a sorted new MultiColumnPrinter class using LEFT alignment and
     * with no title border.
     *
     * @param numCol
     *            number of columns
     * @param gap
     *            gap between each column
     * @param app
     *            the console application to use for outputting data
     */
    public MultiColumnPrinter(final int numCol, final int gap, final ConsoleApplication app) {
        this(numCol, gap, null, LEFT, app);
    }
 
    /**
     * Creates a sorted new MultiColumnPrinter class using LEFT alignment.
     *
     * @param numCol
     *            number of columns
     * @param gap
     *            gap between each column
     * @param border
     *            character used to frame the titles
     * @param app
     *            the console application to use for outputting data
     */
    public MultiColumnPrinter(final int numCol, final int gap, final String border,
            final ConsoleApplication app) {
        this(numCol, gap, border, LEFT, app);
    }
 
    /**
     * Creates a new MultiColumnPrinter class.
     *
     * @param numCol
     *            number of columns
     * @param gap
     *            gap between each column
     * @param border
     *            character used to frame the titles
     * @param align
     *            type of alignment within columns
     * @param app
     *            the console application to use for outputting data
     */
    public MultiColumnPrinter(final int numCol, final int gap, final String border, final int align,
            final ConsoleApplication app) {
        curLength = new int[numCol];
 
        this.numCol = numCol;
        this.gap = gap;
        this.border = border;
        this.align = align;
        this.titleAlign = LEFT;
 
        this.app = app;
    }
 
    /**
     * Adds to the row of strings to be used as the title for the table.
     *
     * @param row
     *            Array of strings to print in one row of title.
     */
    public void addTitle(final String[] row) {
        if (row == null) {
            return;
        }
 
        final int[] span = new int[row.length];
        for (int i = 0; i < row.length; i++) {
            span[i] = 1;
        }
 
        addTitle(row, span);
    }
 
    /**
     * Adds to the row of strings to be used as the title for the table. Also
     * allows for certain title strings to span multiple columns The span
     * parameter is an array of integers which indicate how many columns the
     * corresponding title string will occupy. For a row that is 4 columns
     * wide, it is possible to have some title strings in a row to 'span'
     * multiple columns:
     * <P>
     *
     * <PRE>
     * ------------------------------------
     *     Name             Contact
     * First  Last      Email       Phone
     * ------------------------------------
     * Bob    Jones  bob@foo.com   123-4567
     * John   Doe    john@foo.com  456-7890
     * </PRE>
     *
     * In the example above, the title row has a string 'Name' that spans 2
     * columns. The string 'Contact' also spans 2 columns. The above is done
     * by passing in to addTitle() an array that contains:
     *
     * <PRE>
     * span[0] = 2; // spans 2 columns
     * span[1] = 0; // spans 0 columns, ignore
     * span[2] = 2; // spans 2 columns
     * span[3] = 0; // spans 0 columns, ignore
     * </PRE>
     * <P>
     * A span value of 1 is the default. The method addTitle(String[] row)
     * basically does:
     *
     * <PRE>
     * int[] span = new int[row.length];
     * for (int i = 0; i &lt; row.length; i++) {
     *     span[i] = 1;
     * }
     * addTitle(row, span);
     * </PRE>
     *
     * @param row
     *            Array of strings to print in one row of title.
     * @param span
     *            Array of integers that reflect the number of columns the
     *            corresponding title string will occupy.
     */
    public void addTitle(final String[] row, final int[] span) {
        // Need to create a new instance of it, otherwise the new values
        // will always overwrite the old values.
        titleTable.add(Arrays.copyOf(row, row.length));
        titleSpanTable.add(span);
    }
 
    /**
     * Clears title strings.
     */
    public void clearTitle() {
        titleTable.clear();
        titleSpanTable.clear();
    }
 
    /**
     * Adds one row of text to output.
     *
     * @param row
     *            Array of strings to print in one row.
     */
    public void printRow(final String... row) {
        for (int i = 0; i < numCol; i++) {
            if (titleAlign == RIGHT) {
                final int spaceBefore = curLength[i] - row[i].length();
                printSpaces(spaceBefore);
                app.getOutputStream().print(row[i]);
                if (i < numCol - 1) {
                    printSpaces(gap);
                }
            } else if (align == CENTER) {
                int space1, space2;
                space1 = (curLength[i] - row[i].length()) / 2;
                space2 = curLength[i] - row[i].length() - space1;
 
                printSpaces(space1);
                app.getOutputStream().print(row[i]);
                printSpaces(space2);
                if (i < numCol - 1) {
                    printSpaces(gap);
                }
            } else {
                app.getOutputStream().print(row[i]);
                if (i < numCol - 1) {
                    printSpaces(curLength[i] - row[i].length() + gap);
                }
            }
        }
        app.getOutputStream().println("");
    }
 
    /**
     * Prints the table title.
     */
    public void printTitle() {
        // Get the longest string for each column and store in curLength[]
 
        // Scan through title rows
        Iterator<int[]> spanEnum = titleSpanTable.iterator();
        for (String[] row : titleTable) {
            final int[] curSpan = spanEnum.next();
 
            for (int i = 0; i < numCol; i++) {
                // None of the fields should be null, but if it
                // happens to be so, replace it with "-".
                if (row[i] == null) {
                    row[i] = "-";
                }
 
                int len = row[i].length();
 
                /*
                 * If a title string spans multiple columns, then the space it
                 * occupies in each column is at most len/span (since we have
                 * gap to take into account as well).
                 */
                final int span = curSpan[i];
                int rem = 0;
                if (span > 1) {
                    rem = len % span;
                    len = len / span;
                }
 
                if (curLength[i] < len) {
                    curLength[i] = len;
 
                    if ((span > 1) && ((i + span) <= numCol)) {
                        for (int j = i + 1; j < (i + span); ++j) {
                            curLength[j] = len;
                        }
 
                        /*
                         * Add remainder to last column in span to avoid
                         * round-off errors.
                         */
                        curLength[(i + span) - 1] += rem;
                    }
                }
            }
        }
 
        printBorder();
 
        spanEnum = titleSpanTable.iterator();
        for (String[] row : titleTable) {
            final int[] curSpan = spanEnum.next();
 
            for (int i = 0; i < numCol; i++) {
                int availableSpace = 0;
                final int span = curSpan[i];
 
                if (span == 0) {
                    continue;
                }
 
                availableSpace = curLength[i];
 
                if ((span > 1) && ((i + span) <= numCol)) {
                    for (int j = i + 1; j < (i + span); ++j) {
                        availableSpace += gap;
                        availableSpace += curLength[j];
                    }
                }
 
                if (titleAlign == RIGHT) {
                    final int spaceBefore = availableSpace - row[i].length();
                    printSpaces(spaceBefore);
                    app.getOutputStream().print(row[i]);
                    if (i < numCol - 1) {
                        printSpaces(gap);
                    }
                } else if (titleAlign == CENTER) {
                    int spaceBefore, spaceAfter;
                    spaceBefore = (availableSpace - row[i].length()) / 2;
                    spaceAfter = availableSpace - row[i].length() - spaceBefore;
 
                    printSpaces(spaceBefore);
                    app.getOutputStream().print(row[i]);
                    printSpaces(spaceAfter);
                    if (i < numCol - 1) {
                        printSpaces(gap);
                    }
                } else {
                    app.getOutputStream().print(row[i]);
                    if (i < numCol - 1) {
                        printSpaces(availableSpace - row[i].length() + gap);
                    }
                }
 
            }
            app.getOutputStream().println("");
        }
        printBorder();
    }
 
    /**
     * Set alignment for title strings.
     *
     * @param titleAlign
     *            The alignment which should be one of {@code LEFT},
     *            {@code RIGHT}, or {@code CENTER}.
     */
    public void setTitleAlign(final int titleAlign) {
        this.titleAlign = titleAlign;
    }
 
    private void printBorder() {
        if (border == null) {
            return;
        }
 
        // For the value in each column
        for (int i = 0; i < numCol; i++) {
            for (int j = 0; j < curLength[i]; j++) {
                app.getOutputStream().print(border);
            }
        }
 
        // For the gap between each column
        for (int i = 0; i < numCol - 1; i++) {
            for (int j = 0; j < gap; j++) {
                app.getOutputStream().print(border);
            }
        }
        app.getOutputStream().println("");
    }
 
    private void printSpaces(final int count) {
        for (int i = 0; i < count; ++i) {
            app.getOutputStream().print(" ");
        }
    }
}