From 17116cc208810e274c30fc7cc985d55f36d21b23 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Thu, 15 Feb 2018 12:28:40 +0100 Subject: JSONTool sonar fixes Change-Id: I457ac55fa17475a19a2e511a6e7813070ffd6d2e Issue-ID: APPC-634 Signed-off-by: Jakub Dudycz --- .../onap/sdnc/config/generator/tool/JSONTool.java | 76 +++++++++++----------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java index a4731099d..faa9c6fb0 100644 --- a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java +++ b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/JSONTool.java @@ -24,6 +24,8 @@ package org.onap.sdnc.config.generator.tool; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -32,71 +34,71 @@ import java.util.Map; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - public class JSONTool { private static final EELFLogger log = EELFManager.getInstance().getLogger(JSONTool.class); + private JSONTool() { + } + public static Map convertToProperties(String s) throws JSONException { return convertToProperties(s, null); } public static Map convertToProperties(String s, List blockKeys) - throws JSONException { + throws JSONException { JSONObject json = new JSONObject(s); - Map mm = new HashMap(); + Map mm = new HashMap<>(); + Map wm = new HashMap<>(); - Map wm = new HashMap(); Iterator ii = json.keys(); while (ii.hasNext()) { String key1 = ii.next(); wm.put(key1, json.get(key1)); - - } - - while (!wm.isEmpty()) + while (!wm.isEmpty()) { for (String key : new ArrayList<>(wm.keySet())) { Object o = wm.get(key); wm.remove(key); - - - if (blockKeys != null && blockKeys.contains(key) && o != null) { - // log.info("Adding JSON Block Keys : " + key + "=" + o.toString()); - mm.put("block_" + key, o.toString()); - } - + tryAddBlockKeys(blockKeys, mm, key, o); if (o instanceof Boolean || o instanceof Number || o instanceof String) { mm.put(key, o.toString()); - // log.info("Added property: " + key + ": " + o.toString()); + log.info("Added property: " + key + ": " + o.toString()); + } else if (o instanceof JSONObject) { + fill(wm, key, (JSONObject) o); + } else if (o instanceof JSONArray) { + fill(mm, wm, key, (JSONArray) o); } + } + } + return mm; + } - else if (o instanceof JSONObject) { - JSONObject jo = (JSONObject) o; - Iterator i = jo.keys(); - while (i.hasNext()) { - String key1 = i.next(); - wm.put(key + "." + key1, jo.get(key1)); - } - } + private static void tryAddBlockKeys(List blockKeys, Map mm, String key, Object o) { + if (blockKeys != null && blockKeys.contains(key) && o != null) { + mm.put("block_" + key, o.toString()); + log.info("Adding JSON Block Keys : " + key + "=" + o.toString()); + } + } - else if (o instanceof JSONArray) { - JSONArray ja = (JSONArray) o; - mm.put("size_" + key, String.valueOf(ja.length())); + private static void fill(Map mm, Map wm, String key, JSONArray array) + throws JSONException { + mm.put("size_" + key, String.valueOf(array.length())); + log.info("Added property: " + key + "_length" + ": " + array.length()); - // log.info("Added property: " + key + "_length" + ": " + - // String.valueOf(ja.length())); + for (int i = 0; i < array.length(); i++) { + wm.put(key + '[' + i + ']', array.get(i)); + } + } - for (int i = 0; i < ja.length(); i++) - wm.put(key + '[' + i + ']', ja.get(i)); - } - } + private static void fill(Map wm, String key, JSONObject object) throws JSONException { - return mm; + Iterator i = object.keys(); + while (i.hasNext()) { + String key1 = i.next(); + wm.put(key + "." + key1, object.get(key1)); + } } - } -- cgit 1.2.3-korg