Fix sonar issues
[dcaegen2/platform.git] / mod / bpgenerator / common / src / main / java / org / onap / blueprintgenerator / service / base / FixesService.java
index d848cac..961528b 100644 (file)
@@ -4,6 +4,7 @@
  *  *  org.onap.dcae
  *  *  ================================================================================
  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
+ *  *  Copyright (c) 2021 Nokia. All rights reserved.
  *  *  ================================================================================
  *  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  *  you may not use this file except in compliance with the License.
@@ -26,7 +27,6 @@ package org.onap.blueprintgenerator.service.base;
 import org.onap.blueprintgenerator.exception.FixesException;
 import org.springframework.stereotype.Service;
 
-
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.FileWriter;
@@ -38,133 +38,168 @@ import java.util.List;
 
 /**
  * @author : Ravi Mantena
- * @date 10/16/2020
- * Application: DCAE/ONAP - Blueprint Generator
- * Common Module: Used by both ONAp and DCAE Blueprint Applications
- * Service: For Blueprint Quotes Fixes
+ * @date 10/16/2020 Application: DCAE/ONAP - Blueprint Generator Common Module: Used by both ONAp
+ * and DCAE Blueprint Applications Service: For Blueprint Quotes Fixes
  */
-
 @Service
 public class FixesService {
 
- public void fixDcaeSingleQuotes(File file) {
-  List<String> lines = new ArrayList<>();
-  try {
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- for (String line = br.readLine(); line != null; line = br.readLine()){
-  if(line.contains("'")) {
-   line = line.replaceAll("'\\{", "{");
-   line = line.replaceAll("}'", "}");
-   line = line.replaceAll("'\\[", "[");
-   line = line.replaceAll("]'", "]");
-   line = line.replaceAll("'''''", "'");
-   line = line.replaceAll("'''", "'");
-   line = line.replaceAll("'''", "");
-   line = line.replaceAll("''\\{", "'{");
-   line = line.replaceAll("}''", "}'");
-   line = line.replaceAll("''\\[", "'[");
-   line = line.replaceAll("]''", "]'");
-   line = line.replaceAll("\"''", "'");
-   line = line.replaceAll("''\"", "'");
-  }
-  if(line.contains("get_input") || line.contains("get_secret") || line.contains("envs"))
-   line = line.replaceAll("'", "");
-
-  lines.add(line);
- }
-
- fr.close();
- br.close();
-
- FileWriter fw = new FileWriter(file);
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
- for(String s: lines) {
-  out.println();
-  out.write(s);
-  out.flush();
- }
- out.close();
- fw.close();
-  } catch (Exception e) {
- throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
-  }
- }
-
- public String fixStringQuotes(String string) {
-  String sLines[] = string.split("\n");
-  String ret = "";
-  for(String line: sLines) {
- if(line.contains("get_input") || line.contains("get_secret") || ((line.contains("concat") || line.contains("default: ") || line.contains("description") || line.contains("dmaap") || line.contains(".\"'")) && line.contains("'")))
-  line = line.replaceAll("'", "");
-
- if(line.contains("'")) {
-  line = line.replaceAll("'\\{", "{");
-  line = line.replaceAll("}'", "}");
-  line = line.replaceAll("'\\[", "[");
-  line = line.replaceAll("]'", "]");
-  line = line.replaceAll("'''''", "'");
-  line = line.replaceAll("'''", "'");
-  line = line.replaceAll("'''", "");
-  line = line.replaceAll("''\\{", "'{");
-  line = line.replaceAll("}''", "}'");
-  line = line.replaceAll("''\\[", "'[");
-  line = line.replaceAll("]''", "]'");
-  line = line.replaceAll("\"''", "'");
-  line = line.replaceAll("''\"", "'");
- }
- ret = ret + "\n" + line;
-  }
-  return ret;
- }
-
- public void fixOnapSingleQuotes(File file)  {
-  List<String> lines = new ArrayList<>();
-  try {
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- for (String line = br.readLine(); line != null; line = br.readLine()){
-  if(line.contains("'")) {
-   line = line.replace("'", "");
-  }
-  if(line.contains("\"\"") && (line.contains("m") || line.contains("M"))) {
-   line = line.replaceAll("\"\"", "\"");
-  }
-  lines.add(line);
-
- }
- fr.close();
- br.close();
- FileWriter fw = new FileWriter(file);
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
- for(String s: lines) {
-  out.println();
-  out.write(s);
-  out.flush();
- }
-
- out.close();
- fw.close();
-
-  } catch (Exception e) {
- throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
-  }
- }
-
- private String ensureNoSingleQuotes(String line) {
-  if ((line.contains("concat") || line.contains("default: ") || line.contains("description") || line.contains("dmaap") || line.contains(".\"'")) && line.contains("'"))
- return line.replace("'", "");
-  else
- return line;
- }
-
- public String applyFixes(String bp) {
-  List<String> lines = new ArrayList<>();
-  String[] linesPre = bp.split("\n");
-  for (String line : linesPre) {
- lines.add(ensureNoSingleQuotes(line));
-  }
-  return String.join("\n", lines);
- }
+    /**
+     * Interface to fix Single Quotes in the generated Blueprint
+     *
+     * @param file File
+     * @return
+     */
+    public void fixDcaeSingleQuotes(File file) {
+        List<String> lines = new ArrayList<>();
+        try {
+            FileReader fr = new FileReader(file);
+            BufferedReader br = new BufferedReader(fr);
+            for (String line = br.readLine(); line != null; line = br.readLine()) {
+                String newLine = line;
+                if (newLine.contains("'")) {
+                    newLine = processLine(newLine);
+                }
+                if (newLine.contains("get_input") || newLine.contains("get_secret") || newLine
+                    .contains("envs")) {
+                    newLine = newLine.replace("'", "");
+                }
+                lines.add(newLine);
+            }
+
+            fr.close();
+            br.close();
+
+            FileWriter fw = new FileWriter(file);
+            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
+            for (String s : lines) {
+                out.println();
+                out.write(s);
+                out.flush();
+            }
+            out.close();
+            fw.close();
+        } catch (Exception e) {
+            throw new FixesException("Unable to Fix Single Quotes in Final DCAE Blueprint", e);
+        }
+    }
+
+    /**
+     * Interface to fix String Quotes in the generated Blueprint
+     *
+     * @param string String
+     * @return
+     */
+    public String fixStringQuotes(String string) {
+        String[] sLines = string.split("\n");
+        StringBuilder ret = new StringBuilder();
+        for (String line : sLines) {
+            if (line.contains("get_input")
+                || line.contains("get_secret")
+                || ((line.contains("concat")
+                || line.contains("default: ")
+                || line.contains("description")
+                || line.contains("dmaap")
+                || line.contains(".\"'"))
+                && line.contains("'"))) {
+                line = line.replace("'", "");
+            }
+
+            if (line.contains("'")) {
+                line = processLine(line);
+            }
+            ret.append("\n");
+            ret.append(line);
+        }
+        return ret.toString();
+    }
+
+    /**
+     * Interface to fix Single Quotes in the generated ONAP Blueprint
+     *
+     * @param file File
+     * @return
+     */
+    public void fixOnapSingleQuotes(File file) {
+        List<String> lines = new ArrayList<>();
+        try {
+            FileReader fr = new FileReader(file);
+            BufferedReader br = new BufferedReader(fr);
+            for (String line = br.readLine(); line != null; line = br.readLine()) {
+                String newLine = line;
+                if (newLine.contains("'")) {
+                    newLine = newLine.replace("'", "");
+                }
+                if (newLine.contains("\"\"") && (newLine.contains("m") || newLine.contains("M"))) {
+                    newLine = newLine.replace("\"\"", "\"");
+                }
+                lines.add(newLine);
+            }
+            fr.close();
+            br.close();
+            FileWriter fw = new FileWriter(file);
+            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
+            for (String s : lines) {
+                out.println();
+                out.write(s);
+                out.flush();
+            }
+
+            out.close();
+            fw.close();
+
+        } catch (Exception e) {
+            throw new FixesException("Unable to Fix Single Quotes in final ONAP Blueprint", e);
+        }
+    }
+
+    /**
+     * Interface to fix Single Quotes for given line
+     *
+     * @param line Line
+     * @return
+     */
+    private String ensureNoSingleQuotes(String line) {
+        if ((line.contains("concat")
+            || line.contains("default: ")
+            || line.contains("description")
+            || line.contains("dmaap")
+            || line.contains(".\"'"))
+            && line.contains("'")) {
+            return line.replace("'", "");
+        } else {
+            return line;
+        }
+    }
+
+    /**
+     * Interface to Applt fixes for Quotes
+     *
+     * @param bp Blueprint
+     * @return
+     */
+    public String applyFixes(String bp) {
+        List<String> lines = new ArrayList<>();
+        String[] linesPre = bp.split("\n");
+        for (String line : linesPre) {
+            lines.add(ensureNoSingleQuotes(line));
+        }
+        return String.join("\n", lines);
+    }
+
+    private String processLine(String line) {
+        return line.replace("'\\{", "{")
+            .replace("}'", "}")
+            .replace("'\\[", "[")
+            .replace("]'", "]")
+            .replace("'''''", "'")
+            .replace("'''", "'")
+            .replace("'''", "")
+            .replace("''\\{", "'{")
+            .replace("}''", "}'")
+            .replace("''\\[", "'[")
+            .replace("]''", "]'")
+            .replace("\"''", "'")
+            .replace("''\"", "'");
+    }
 }