Fixed Sonar Cloud Critical Issues
[cli.git] / framework / src / main / java / org / onap / cli / fw / output / print / OnapCommandPrint.java
index 3946c5d..14e37c3 100644 (file)
@@ -31,12 +31,20 @@ import org.apache.commons.csv.CSVPrinter;
 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
 import org.onap.cli.fw.output.OnapCommandPrintDirection;
 
+import com.google.gson.JsonParser;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
+
+import net.minidev.json.JSONArray;
+import net.minidev.json.JSONObject;
+import net.minidev.json.JSONValue;
 /**
  * Oclip Command Table print.
  *
  */
 public class OnapCommandPrint {
 
+
     public static final int MAX_COLUMN_LENGTH = 50;
 
     private OnapCommandPrintDirection direction;
@@ -65,10 +73,7 @@ public class OnapCommandPrint {
      * @return list
      */
     public List<String> getColumn(String header) {
-        if (this.data.get(header) == null) {
-            this.data.put(header, new ArrayList<String>());
-        }
-        return this.data.get(header);
+       return this.data.computeIfAbsent(header, k -> new ArrayList<String>());
     }
 
     public boolean isPrintTitle() {
@@ -93,6 +98,21 @@ public class OnapCommandPrint {
         return max;
     }
 
+    public List<List<Object>>  addTitle(List<List<Object>> rows, boolean isNormalize){
+        if (this.isPrintTitle()) {
+            List<Object> 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.
      *
@@ -107,17 +127,7 @@ public class OnapCommandPrint {
         List<List<Object>> rows = new ArrayList<>();
 
         // add title
-        if (this.isPrintTitle()) {
-            List<Object> 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++) {
@@ -161,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, " ");
@@ -213,11 +223,10 @@ public class OnapCommandPrint {
      *             exception
      */
     public String printCsv() throws OnapCommandOutputPrintingFailed {
-        StringWriter writer = new StringWriter();
-        CSVPrinter printer = null;
-        try {
-            CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
-            printer = new CSVPrinter(writer, formattor);
+        CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator"));
+
+        try (StringWriter writer = new StringWriter();
+             CSVPrinter printer = new CSVPrinter(writer, formattor);) {
 
             List<List<Object>> rows = this.formRows(false);
 
@@ -228,25 +237,63 @@ public class OnapCommandPrint {
             return writer.toString();
         } catch (IOException e) {
             throw new OnapCommandOutputPrintingFailed(e);
-        } finally {
-            try {
-                if (printer != null) {
-                    printer.close();
-                }
-                writer.close();
-            } catch (IOException e) {
-                throw new OnapCommandOutputPrintingFailed(e);  // NOSONAR
-            }
         }
     }
 
+    public Object getJsonNodeOrString(String value) {
+        try {
+            return JSONValue.parse(value);
+        } catch (Exception e) {
+            return value;
+        }
+    }
+
+    public JSONObject printPortrait(List<List<Object>> rows){
+        JSONObject result = new JSONObject();
+        for (int i=1; i<rows.size(); i++) {
+            if (rows.get(i).get(1) != null)
+                result.put(rows.get(i).get(0).toString(), this.getJsonNodeOrString(rows.get(i).get(1).toString()));
+        }
+        return result;
+    }
+
     public String printJson() {
-        // (mrkanag) print in json
-        return null;
+        List<List<Object>> rows = this.formRows(false);
+
+        if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) {
+            JSONObject result = printPortrait(rows);
+            return result.toJSONString();
+        } else {
+            JSONArray array = new JSONArray();
+
+            //skip first row title
+            List<Object> titleRow = rows.get(0);
+
+            for (int i=1; i<rows.size(); i++) {
+                JSONObject rowO = new JSONObject();
+
+                for (int j=0; j<titleRow.size(); j++) {
+                    if (rows.get(i).get(j) != null)
+                        rowO.put(titleRow.get(j).toString(), this.getJsonNodeOrString(rows.get(i).get(j).toString()));
+                }
+
+                array.add(rowO);
+            }
+            try {
+                return JsonParser.parseString(array.toJSONString()).toString();
+            } catch (Exception e) { // NOSONAR
+                return array.toJSONString();
+            }
+
+        }
     }
+    
+    public String printYaml() throws OnapCommandOutputPrintingFailed {
+        try {
+            return new YAMLMapper().writeValueAsString(new ObjectMapper().readTree(this.printJson()));
+        } catch (IOException  e) {
+            throw new OnapCommandOutputPrintingFailed(e);  // NOSONAR
+        }
 
-    public String printYaml() {
-        // (mrkanag) print in yaml
-        return null;
     }
 }