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.conf.OnapCommandConstants;
32 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
33 import org.onap.cli.fw.output.OnapCommandPrintDirection;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
38 import net.minidev.json.JSONArray;
39 import net.minidev.json.JSONObject;
40 import net.minidev.json.JSONValue;
42 * Oclip Command Table print.
45 public class OnapCommandPrint {
47 public static final int MAX_COLUMN_LENGTH = 50;
49 private OnapCommandPrintDirection direction;
51 private Map<String, List<String>> data = new LinkedHashMap<>();
53 private boolean printTitle = true;
55 public OnapCommandPrintDirection getDirection() {
59 public void setDirection(OnapCommandPrintDirection direction) {
60 this.direction = direction;
63 public void addColumn(String header, List<String> data) {
64 this.data.put(header, data);
74 public List<String> getColumn(String header) {
75 if (this.data.get(header) == null) {
76 this.data.put(header, new ArrayList<String>());
78 return this.data.get(header);
81 public boolean isPrintTitle() {
85 public void setPrintTitle(boolean printTitle) {
86 this.printTitle = printTitle;
89 private int findMaxRows() {
91 if (!this.isPrintTitle()) {
94 for (List<String> cols : this.data.values()) {
95 if (cols != null && max < cols.size()) {
104 * Helps to form the rows from columns.
108 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
109 * +--------------+-----------+-----------------------------+ | v1 | List[line| v 3 | | | 1, line2]| |
110 * +--------------+-----------+-----------------------------+ | null | yyyyyy 2 | xxxxxx 3 |
111 * +--------------+-----------+-----------------------------+
113 private List<List<Object>> formRows(boolean isNormalize) {
114 List<List<Object>> rows = new ArrayList<>();
117 if (this.isPrintTitle()) {
118 List<Object> list = new ArrayList<>();
119 for (String key : this.data.keySet()) {
120 if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) {
121 list.add(splitIntoList(key, MAX_COLUMN_LENGTH));
130 for (int i = 0; i < this.findMaxRows(); i++) {
131 List<Object> row = new ArrayList<>();
132 for (List<String> cols : this.data.values()) {
133 if (cols != null && cols.size() > i) {
134 String value = cols.get(i);
135 // split the cell into multiple sub rows
136 if (isNormalize && value != null && value.length() > MAX_COLUMN_LENGTH) {
137 row.add(splitIntoList(value, MAX_COLUMN_LENGTH));
139 // store as string (one entry)
143 // no value exist for this column
154 * Splits big strings into list of strings based on maxCharInLine size.
158 * @param maxCharInLine
160 * @return list of strings
162 public List<String> splitIntoList(String input, int maxCharInLine) {
166 if (inp == null || "".equals(inp) || maxCharInLine <= 0) {
167 return Collections.emptyList();
169 // new line is converted to space char
170 if (inp.contains("\n")) {
171 inp = inp.replaceAll("\n", "");
174 StringTokenizer tok = new StringTokenizer(inp, " ");
175 StringBuilder output = new StringBuilder(inp.length());
177 while (tok.hasMoreTokens()) {
178 String word = tok.nextToken();
180 while (word.length() >= maxCharInLine) {
181 output.append(word.substring(0, maxCharInLine - lineLen) + "\n");
182 word = word.substring(maxCharInLine - lineLen);
186 if (lineLen + word.length() >= maxCharInLine) {
190 output.append(word + " ");
192 lineLen += word.length() + 1;
194 String[] strArray = output.toString().split("\n");
196 return Arrays.asList(strArray);
200 * Helps to print table.
202 * @param printSeparator
203 * Prints with line separator
204 * @return +--------------+-----------+-----------------------------+ | header1 | header 2 | header 3 |
205 * +--------------+-----------+-----------------------------+ | v1 | line 1 | v 3 | | | line 2 | |
206 * +--------------+-----------+-----------------------------+ | | yyyyyy 2 | xxxxxx 3 |
207 * +--------------+-----------+-----------------------------+
209 public String printTable(boolean printSeparator) {
210 List<List<Object>> rows = this.formRows(true);
211 TableGenerator table = new TableGenerator();
212 return table.generateTable(rows, printSeparator);
216 * Print output in csv format.
219 * @throws OnapCommandOutputPrintingFailed
222 public String printCsv() throws OnapCommandOutputPrintingFailed {
223 CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
225 try (StringWriter writer = new StringWriter();
226 CSVPrinter printer = new CSVPrinter(writer, formattor);) {
228 List<List<Object>> rows = this.formRows(false);
230 for (int i = 0; i < this.findMaxRows(); i++) {
231 printer.printRecord(rows.get(i));
234 return writer.toString();
235 } catch (IOException e) {
236 throw new OnapCommandOutputPrintingFailed(e);
240 public Object getJsonNodeOrString(String value) {
242 return (JSONObject) JSONValue.parse(value);
243 } catch (Exception e) {
248 public String printJson() {
249 List<List<Object>> rows = this.formRows(false);
251 if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) {
252 JSONObject result = new JSONObject();
253 for (int i=1; i<rows.size(); i++) {
254 if (rows.get(i).get(1) != null)
255 result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString()));
257 return result.toJSONString();
259 JSONArray array = new JSONArray();
261 //skip first row title
262 List<Object> titleRow = rows.get(0);
264 for (int i=1; i<rows.size(); i++) {
265 JSONObject rowO = new JSONObject();
267 for (int j=0; j<titleRow.size(); j++) {
268 if (rows.get(i).get(j) != null)
269 rowO.put(titleRow.get(j).toString(), this.getJsonNodeOrString(rows.get(i).get(j).toString()));
275 return new ObjectMapper().readTree(array.toJSONString()).toString();
276 } catch (IOException e) {
277 // TODO Auto-generated catch block
278 return array.toJSONString();
284 public String printYaml() throws OnapCommandOutputPrintingFailed {
286 return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson()));
287 } catch (IOException e) {
288 throw new OnapCommandOutputPrintingFailed(e); // NOSONAR