2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.output.print;
19 import org.apache.commons.csv.CSVFormat;
20 import org.apache.commons.csv.CSVPrinter;
21 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
22 import org.onap.cli.fw.output.PrintDirection;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.LinkedHashMap;
30 import java.util.List;
32 import java.util.StringTokenizer;
35 * Onap Command Table print.
38 public class OnapCommandPrint {
40 public static final int MAX_COLUMN_LENGTH = 50;
42 private PrintDirection direction;
44 private Map<String, List<String>> data = new LinkedHashMap<>();
46 private boolean printTitle = true;
48 public PrintDirection getDirection() {
52 public void setDirection(PrintDirection direction) {
53 this.direction = direction;
56 public void addColumn(String header, List<String> data) {
57 this.data.put(header, data);
67 public List<String> getColumn(String header) {
68 if (this.data.get(header) == null) {
69 this.data.put(header, new ArrayList<String>());
71 return this.data.get(header);
74 public boolean isPrintTitle() {
78 public void setPrintTitle(boolean printTitle) {
79 this.printTitle = printTitle;
82 private int findMaxRows() {
84 if (!this.isPrintTitle()) {
87 for (List<String> cols : this.data.values()) {
88 if (cols != null && max < cols.size()) {
97 * Helps to form the rows from columns.
101 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
102 * +--------------+-----------+-----------------------------+ | v1 | List[line| v 3 | | | 1, line2]| |
103 * +--------------+-----------+-----------------------------+ | null | yyyyyy 2 | xxxxxx 3 |
104 * +--------------+-----------+-----------------------------+
106 private List<List<Object>> formRows(boolean isNormalize) {
107 List<List<Object>> rows = new ArrayList<>();
110 if (this.isPrintTitle()) {
111 List<Object> list = new ArrayList<>();
112 for (String key : this.data.keySet()) {
113 if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) {
114 list.add(splitIntoList(key, MAX_COLUMN_LENGTH));
123 for (int i = 0; i < this.findMaxRows(); i++) {
124 List<Object> row = new ArrayList<>();
125 for (List<String> cols : this.data.values()) {
126 if (cols.size() > i) {
127 String value = cols.get(i);
128 // split the cell into multiple sub rows
129 if (isNormalize && value != null && value.length() > MAX_COLUMN_LENGTH) {
130 row.add(splitIntoList(value, MAX_COLUMN_LENGTH));
132 // store as string (one entry)
136 // now value exist for this column
147 * Splits big strings into list of strings based on maxCharInLine size.
151 * @param maxCharInLine
153 * @return list of strings
155 public List<String> splitIntoList(String input, int maxCharInLine) {
159 if (inp == null || "".equals(inp) || maxCharInLine <= 0) {
160 return Collections.emptyList();
162 // new line is converted to space char
163 if (inp.contains("\n")) {
164 inp = inp.replaceAll("\n", "");
167 StringTokenizer tok = new StringTokenizer(inp, " ");
168 StringBuilder output = new StringBuilder(inp.length());
170 while (tok.hasMoreTokens()) {
171 String word = tok.nextToken();
173 while (word.length() >= maxCharInLine) {
174 output.append(word.substring(0, maxCharInLine - lineLen) + "\n");
175 word = word.substring(maxCharInLine - lineLen);
179 if (lineLen + word.length() >= maxCharInLine) {
183 output.append(word + " ");
185 lineLen += word.length() + 1;
187 String[] strArray = output.toString().split("\n");
189 return Arrays.asList(strArray);
193 * Helps to print table.
195 * @param printSeparator
196 * Prints with line separator
197 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
198 * +--------------+-----------+-----------------------------+ | v1 | line 1 | v 3 | | | line 2 | |
199 * +--------------+-----------+-----------------------------+ | | yyyyyy 2 | xxxxxx 3 |
200 * +--------------+-----------+-----------------------------+
202 public String printTable(boolean printSeparator) {
203 List<List<Object>> rows = this.formRows(true);
204 TableGenerator table = new TableGenerator();
205 return table.generateTable(rows, printSeparator);
209 * Print output in csv format.
212 * @throws OnapCommandOutputPrintingFailed
215 public String printCsv() throws OnapCommandOutputPrintingFailed {
216 StringWriter writer = new StringWriter();
217 CSVPrinter printer = null;
219 CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
220 printer = new CSVPrinter(writer, formattor);
222 List<List<Object>> rows = this.formRows(false);
224 for (int i = 0; i < this.findMaxRows(); i++) {
225 printer.printRecord(rows.get(i));
228 return writer.toString();
229 } catch (IOException e) {
230 throw new OnapCommandOutputPrintingFailed(e);
233 if (printer != null) {
237 } catch (IOException e) {
238 throw new OnapCommandOutputPrintingFailed(e); // NOSONAR
243 public String printJson() {
244 // (mrkanag) print in json
248 public String printYaml() {
249 // (mrkanag) print in yaml