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 java.io.IOException;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import java.util.StringTokenizer;
29 import org.apache.commons.csv.CSVFormat;
30 import org.apache.commons.csv.CSVPrinter;
31 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
32 import org.onap.cli.fw.output.OnapCommandPrintDirection;
34 import net.minidev.json.JSONArray;
35 import net.minidev.json.JSONObject;
38 * Oclip Command Table print.
41 public class OnapCommandPrint {
43 public static final int MAX_COLUMN_LENGTH = 50;
45 private OnapCommandPrintDirection direction;
47 private Map<String, List<String>> data = new LinkedHashMap<>();
49 private boolean printTitle = true;
51 public OnapCommandPrintDirection getDirection() {
55 public void setDirection(OnapCommandPrintDirection direction) {
56 this.direction = direction;
59 public void addColumn(String header, List<String> data) {
60 this.data.put(header, data);
70 public List<String> getColumn(String header) {
71 if (this.data.get(header) == null) {
72 this.data.put(header, new ArrayList<String>());
74 return this.data.get(header);
77 public boolean isPrintTitle() {
81 public void setPrintTitle(boolean printTitle) {
82 this.printTitle = printTitle;
85 private int findMaxRows() {
87 if (!this.isPrintTitle()) {
90 for (List<String> cols : this.data.values()) {
91 if (cols != null && max < cols.size()) {
100 * Helps to form the rows from columns.
104 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
105 * +--------------+-----------+-----------------------------+ | v1 | List[line| v 3 | | | 1, line2]| |
106 * +--------------+-----------+-----------------------------+ | null | yyyyyy 2 | xxxxxx 3 |
107 * +--------------+-----------+-----------------------------+
109 private List<List<Object>> formRows(boolean isNormalize) {
110 List<List<Object>> rows = new ArrayList<>();
113 if (this.isPrintTitle()) {
114 List<Object> list = new ArrayList<>();
115 for (String key : this.data.keySet()) {
116 if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) {
117 list.add(splitIntoList(key, MAX_COLUMN_LENGTH));
126 for (int i = 0; i < this.findMaxRows(); i++) {
127 List<Object> row = new ArrayList<>();
128 for (List<String> cols : this.data.values()) {
129 if (cols != null && cols.size() > i) {
130 String value = cols.get(i);
131 // split the cell into multiple sub rows
132 if (isNormalize && value != null && value.length() > MAX_COLUMN_LENGTH) {
133 row.add(splitIntoList(value, MAX_COLUMN_LENGTH));
135 // store as string (one entry)
139 // no value exist for this column
150 * Splits big strings into list of strings based on maxCharInLine size.
154 * @param maxCharInLine
156 * @return list of strings
158 public List<String> splitIntoList(String input, int maxCharInLine) {
162 if (inp == null || "".equals(inp) || maxCharInLine <= 0) {
163 return Collections.emptyList();
165 // new line is converted to space char
166 if (inp.contains("\n")) {
167 inp = inp.replaceAll("\n", "");
170 StringTokenizer tok = new StringTokenizer(inp, " ");
171 StringBuilder output = new StringBuilder(inp.length());
173 while (tok.hasMoreTokens()) {
174 String word = tok.nextToken();
176 while (word.length() >= maxCharInLine) {
177 output.append(word.substring(0, maxCharInLine - lineLen) + "\n");
178 word = word.substring(maxCharInLine - lineLen);
182 if (lineLen + word.length() >= maxCharInLine) {
186 output.append(word + " ");
188 lineLen += word.length() + 1;
190 String[] strArray = output.toString().split("\n");
192 return Arrays.asList(strArray);
196 * Helps to print table.
198 * @param printSeparator
199 * Prints with line separator
200 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
201 * +--------------+-----------+-----------------------------+ | v1 | line 1 | v 3 | | | line 2 | |
202 * +--------------+-----------+-----------------------------+ | | yyyyyy 2 | xxxxxx 3 |
203 * +--------------+-----------+-----------------------------+
205 public String printTable(boolean printSeparator) {
206 List<List<Object>> rows = this.formRows(true);
207 TableGenerator table = new TableGenerator();
208 return table.generateTable(rows, printSeparator);
212 * Print output in csv format.
215 * @throws OnapCommandOutputPrintingFailed
218 public String printCsv() throws OnapCommandOutputPrintingFailed {
219 StringWriter writer = new StringWriter();
220 CSVPrinter printer = null;
222 CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
223 printer = new CSVPrinter(writer, formattor);
225 List<List<Object>> rows = this.formRows(false);
227 for (int i = 0; i < this.findMaxRows(); i++) {
228 printer.printRecord(rows.get(i));
231 return writer.toString();
232 } catch (IOException e) {
233 throw new OnapCommandOutputPrintingFailed(e);
236 if (printer != null) {
240 } catch (IOException e) {
241 throw new OnapCommandOutputPrintingFailed(e); // NOSONAR
246 public String printJson() {
247 List<List<Object>> rows = this.formRows(false);
249 JSONArray array = new JSONArray();
251 //skip first row title
252 for (int i=1; i<rows.size(); i++) {
253 JSONObject rowO = new JSONObject();
254 for (Object col: rows.get(0)) {
255 rowO.put(col.toString(), rows.get(i).toString());
260 return array.toJSONString();
263 public String printYaml() {
264 // (mrkanag) print in yaml