summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/pmd-helper-plugin/src/main/java/org/openecomp/sdc/onboarding/pmd/PMDState.java
blob: 2602fff2c60a9c7af94949410f1111e8012b37d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.openecomp.sdc.onboarding.pmd;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PMDState {

    private static Map<String, List<Violation>> data = new HashMap<>();
    private static Map<String, List<Violation>> historicState = null;
    private static Pattern p =
            Pattern.compile("\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\",\"(.*)\"");

    public static boolean addViolation(String line, String fileLocation) {
        Matcher m = p.matcher(line);
        if (m.find()) {
            if (m.group(3).indexOf("generated-sources") != -1) {
                return true;
            }
            String mainOrTest =
                    m.group(3).indexOf(File.separator + "test" + File.separator) == -1 ? "[MAIN] " : "[TEST] ";
            List<Violation> list = data.get(fileLocation + "::" + mainOrTest + m.group(2) + "." + m.group(3).substring(
                    m.group(3).lastIndexOf(File.separatorChar) + 1));
            if (list == null) {
                list = new LinkedList<>();
                data.put(fileLocation + "::" + mainOrTest + m.group(2) + "." + m.group(3).substring(
                        m.group(3).lastIndexOf(File.separatorChar) + 1), list);
            }

            list.add(new Violation(m.group(7), m.group(8), m.group(6), Integer.parseInt(m.group(4)),
                    Integer.parseInt(m.group(5))));
            return true;
        }
        return false;
    }

    public static void reset(File mainFile, File testFile, String moduleCoordinates) throws IOException {
        data.clear();
        init(mainFile, moduleCoordinates, "[MAIN] ");
        init(testFile, moduleCoordinates, "[TEST] ");
    }

    private static void init(File file, String moduleCoordinates, String maiOrTest) throws IOException {
        if (file.exists()) {
            List<String> coll = Files.readAllLines(file.toPath());
            for (String line : coll) {
                if (line.indexOf("$") == -1) {
                    data.put(moduleCoordinates + "::" + maiOrTest + line.substring(0, line.indexOf('.'))
                                                                        .replace(File.separator, ".") + ".java",
                            new LinkedList<>());
                }
            }
        }
    }

    public static Map<String, List<Violation>> getState() {
        return data;
    }

    public static void setHistoricState(Map<String, List<Violation>> data) {
        historicState = data;
    }

    public static Map<String, List<Violation>> getHistoricState() {
        return historicState;
    }

}