diff options
author | Jakub Dudycz <jakub.dudycz@nokia.com> | 2018-02-14 13:15:22 +0100 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2018-02-14 17:02:47 +0000 |
commit | 3e9e8ed362bc349a5f7a2871512bcf151192174d (patch) | |
tree | 2330d3df3126c87af893f56621d7043fd48ebc70 /appc-config | |
parent | 8fab95487543a101357ae4945e1d7e261acd3441 (diff) |
EscapeUtils sonar fixes
Change-Id: I77e46ec0e71cd37071c725877621fa28c092bfa9
Issue-ID: APPC-633
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-config')
2 files changed, 29 insertions, 40 deletions
diff --git a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/EscapeUtils.java b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/EscapeUtils.java index bd8b2f56b..177716187 100644 --- a/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/EscapeUtils.java +++ b/appc-config/appc-config-generator/provider/src/main/java/org/onap/sdnc/config/generator/tool/EscapeUtils.java @@ -36,8 +36,8 @@ public class EscapeUtils { if (str == null) { return null; } - String searchList[] = new String[] {"'", "\\"}; - String replacementList[] = new String[] {"''", "\\\\"}; + String[] searchList = new String[]{"'", "\\"}; + String[] replacementList = new String[]{"''", "\\\\"}; return StringUtils.replaceEach(str, searchList, replacementList); } @@ -46,33 +46,28 @@ public class EscapeUtils { if (str == null) { return null; } - /* - * String searchList[] = new String[]{"''", "\\\\"}; String replacementList[] = new - * String[]{ "'", "\\"}; - */ - String searchList[] = new String[] {"''"}; - String replacementList[] = new String[] {"'"}; + String[] searchList = new String[]{"''"}; + String[] replacementList = new String[]{"'"}; return StringUtils.replaceEach(str, searchList, replacementList); } // For Generic Purpose - public static String escapeSQL(String s) { + public static String escapeString(String s) { if (s == null) { return null; } - int length = s.length(); int newLength = length; - for (int i = 0; i < length; i++) { - char c = s.charAt(i); - switch (c) { + for (char ch : s.toCharArray()) { + switch (ch) { case '\\': case '\"': case '\'': - case '\0': { + case '\0': newLength += 1; - } + break; + default: break; } } @@ -80,31 +75,25 @@ public class EscapeUtils { // nothing to escape in the string return s; } - StringBuffer sb = new StringBuffer(newLength); - for (int i = 0; i < length; i++) { - char c = s.charAt(i); - switch (c) { - case '\\': { - sb.append("\\\\"); - } + StringBuilder builder = new StringBuilder(newLength); + for (char ch : s.toCharArray()) { + switch (ch) { + case '\\': + builder.append("\\\\"); break; - case '\"': { - sb.append("\\\""); - } + case '\"': + builder.append("\\\""); break; - case '\'': { - sb.append("\\\'"); - } + case '\'': + builder.append("\\\'"); break; - case '\0': { - sb.append("\\0"); - } + case '\0': + builder.append("\\0"); break; - default: { - sb.append(c); - } + default: + builder.append(ch); } } - return sb.toString(); + return builder.toString(); } } diff --git a/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestDataTool.java b/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestDataTool.java index 6e1d525c2..2a416f7b2 100644 --- a/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestDataTool.java +++ b/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestDataTool.java @@ -85,12 +85,12 @@ public class TestDataTool { String st = "test\"test"; String str = "test\'" + "test"; String strng = "test\0"; - EscapeUtils.escapeSQL(s); + EscapeUtils.escapeString(s); EscapeUtils.escapeSql(s); - EscapeUtils.escapeSQL(st); - EscapeUtils.escapeSQL(str); - EscapeUtils.escapeSQL(strng); - EscapeUtils.escapeSQL(null); + EscapeUtils.escapeString(st); + EscapeUtils.escapeString(str); + EscapeUtils.escapeString(strng); + EscapeUtils.escapeString(null); } // @Test(expected = Exception.class) |