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 com.google.gson.JsonParser;
36 import net.minidev.json.JSONArray;
37 import net.minidev.json.JSONObject;
38 import net.minidev.json.JSONValue;
40 * Oclip Command Table print.
43 public class OnapCommandPrint {
46 public static final int MAX_COLUMN_LENGTH = 50;
48 private OnapCommandPrintDirection direction;
50 private Map<String, List<String>> data = new LinkedHashMap<>();
52 private boolean printTitle = true;
54 public OnapCommandPrintDirection getDirection() {
58 public void setDirection(OnapCommandPrintDirection direction) {
59 this.direction = direction;
62 public void addColumn(String header, List<String> data) {
63 this.data.put(header, data);
73 public List<String> getColumn(String header) {
74 return this.data.computeIfAbsent(header, k -> new ArrayList<String>());
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 CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
221 try (StringWriter writer = new StringWriter();
222 CSVPrinter printer = new CSVPrinter(writer, formattor);) {
224 List<List<Object>> rows = this.formRows(false);
226 for (int i = 0; i < this.findMaxRows(); i++) {
227 printer.printRecord(rows.get(i));
230 return writer.toString();
231 } catch (IOException e) {
232 throw new OnapCommandOutputPrintingFailed(e);
236 public Object getJsonNodeOrString(String value) {
238 return JSONValue.parse(value);
239 } catch (Exception e) {
244 public String printJson() {
245 List<List<Object>> rows = this.formRows(false);
247 if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) {
248 JSONObject result = new JSONObject();
249 for (int i=1; i<rows.size(); i++) {
250 if (rows.get(i).get(1) != null)
251 result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString()));
253 return result.toJSONString();
255 JSONArray array = new JSONArray();
257 //skip first row title
258 List<Object> titleRow = rows.get(0);
260 for (int i=1; i<rows.size(); i++) {
261 JSONObject rowO = new JSONObject();
263 for (int j=0; j<titleRow.size(); j++) {
264 if (rows.get(i).get(j) != null)
265 rowO.put(titleRow.get(j).toString(), this.getJsonNodeOrString(rows.get(i).get(j).toString()));
271 return new JsonParser().parse(array.toJSONString()).toString();
272 } catch (Exception e) { // NOSONAR
273 // TODO Auto-generated catch block
274 return array.toJSONString();
281 required vulnerable fix
282 jackson-dataformat-yaml:YAMLMapper is a sub component of jackson-databind
283 jackson-databind is replaced with gson
286 public String printYaml() throws OnapCommandOutputPrintingFailed {
288 return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson()));
289 } catch (IOException e) {
290 throw new OnapCommandOutputPrintingFailed(e); // NOSONAR