From 4e4f047cf6533f0f01f54d5170a059c5bff5d054 Mon Sep 17 00:00:00 2001 From: sharath reddy Date: Mon, 28 Mar 2022 17:39:59 +0530 Subject: Fixed Sonar Cloud Critical Issues Issue-ID: CLI-439 Signed-off-by: sharath reddy Change-Id: I857188b246848c045171bc4b3d442f3315764274 --- .../org/onap/cli/fw/output/OnapCommandResult.java | 32 +++++++++------- .../onap/cli/fw/output/print/OnapCommandPrint.java | 44 +++++++++++++--------- 2 files changed, 46 insertions(+), 30 deletions(-) (limited to 'framework/src') diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java index b88477c0..fa9bcf02 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java @@ -186,20 +186,8 @@ public class OnapCommandResult { return noOfRecords; } - /** - * Helps to print the result based on the type. - * - * @return string - * @throws OnapCommandOutputFormatNotsupported - * excpetion - * @throws OnapCommandOutputPrintingFailed - * exception - */ - public String print() throws OnapCommandException { - if (this.getType().equals(OnapCommandResultType.TEXT)) { - return this.getOutput().toString(); - } + public OnapCommandPrint createAndLoadPrint() { OnapCommandPrint print = new OnapCommandPrint(); print.setPrintTitle(this.isIncludeTitle()); print.setDirection(this.printDirection); @@ -232,6 +220,24 @@ public class OnapCommandResult { print.addColumn(val.getName(), val.getValues()); } } + return print; + } + + /** + * Helps to print the result based on the type. + * + * @return string + * @throws OnapCommandOutputFormatNotsupported + * excpetion + * @throws OnapCommandOutputPrintingFailed + * exception + */ + public String print() throws OnapCommandException { + if (this.getType().equals(OnapCommandResultType.TEXT)) { + return this.getOutput().toString(); + } + + OnapCommandPrint print = createAndLoadPrint(); if (this.getType().equals(OnapCommandResultType.JSON)) { return print.printJson(); diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index d2bc98be..14e37c3c 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -98,6 +98,21 @@ public class OnapCommandPrint { return max; } + public List> addTitle(List> rows, boolean isNormalize){ + if (this.isPrintTitle()) { + List list = new ArrayList<>(); + for (String key : this.data.keySet()) { + if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { + list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); + } else { + list.add(key); + } + } + rows.add(list); + } + return rows; + } + /** * Helps to form the rows from columns. * @@ -112,17 +127,7 @@ public class OnapCommandPrint { List> rows = new ArrayList<>(); // add title - if (this.isPrintTitle()) { - List list = new ArrayList<>(); - for (String key : this.data.keySet()) { - if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { - list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); - } else { - list.add(key); - } - } - rows.add(list); - } + rows = addTitle(rows, isNormalize); // form row for (int i = 0; i < this.findMaxRows(); i++) { @@ -166,7 +171,7 @@ public class OnapCommandPrint { } // new line is converted to space char if (inp.contains("\n")) { - inp = inp.replaceAll("\n", ""); + inp = inp.replace("\n", ""); } StringTokenizer tok = new StringTokenizer(inp, " "); @@ -243,15 +248,20 @@ public class OnapCommandPrint { } } + public JSONObject printPortrait(List> rows){ + JSONObject result = new JSONObject(); + for (int i=1; i> rows = this.formRows(false); if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) { - JSONObject result = new JSONObject(); - for (int i=1; i