|
| 1 | +package se.bjurr.violations.lib.parsers; |
| 2 | + |
| 3 | +import static se.bjurr.violations.lib.model.Violation.violationBuilder; |
| 4 | +import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.ADDED; |
| 5 | +import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.CHANGED; |
| 6 | +import static se.bjurr.violations.lib.parsers.DiffParser.DiffState.DELETED; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.TreeSet; |
| 12 | +import java.util.logging.Level; |
| 13 | +import java.util.logging.Logger; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +import se.bjurr.violations.lib.ViolationsLogger; |
| 18 | +import se.bjurr.violations.lib.model.Violation; |
| 19 | + |
| 20 | +public class DiffParser implements ViolationsParser { |
| 21 | + public class CommonParsed { |
| 22 | + private final Integer startLineBefore; |
| 23 | + private final Integer endLineBefore; |
| 24 | + private final Integer startLineAfter; |
| 25 | + private final Integer endLineAfter; |
| 26 | + |
| 27 | + public CommonParsed(Integer startLineBefore, Integer endLineBefore, Integer startLineAfter, |
| 28 | + Integer endLineAfter) { |
| 29 | + super(); |
| 30 | + this.startLineBefore = startLineBefore; |
| 31 | + this.endLineBefore = endLineBefore; |
| 32 | + this.startLineAfter = startLineAfter; |
| 33 | + this.endLineAfter = endLineAfter; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String toString() { |
| 38 | + return "CommonParsed [startLineBefore=" + startLineBefore + ", endLineBefore=" + endLineBefore |
| 39 | + + ", startLineAfter=" + startLineAfter + ", endLineAfter=" + endLineAfter + "]"; |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + private static Logger LOG = Logger.getLogger(DiffParser.class.getSimpleName()); |
| 45 | + private static final String LINE_PATTERN_STR = "([0-9]+),?([0-9]+)?"; |
| 46 | + private static final Pattern LINE_PATTERN = Pattern.compile(LINE_PATTERN_STR); |
| 47 | + |
| 48 | + public enum DiffState { |
| 49 | + ADDED("a"), DELETED("d"), CHANGED("c"); |
| 50 | + |
| 51 | + private final Pattern pattern; |
| 52 | + |
| 53 | + private DiffState(String letter) { |
| 54 | + this.pattern = Pattern.compile("^" + LINE_PATTERN_STR + letter + LINE_PATTERN_STR); |
| 55 | + } |
| 56 | + |
| 57 | + public Pattern getScopePattern() { |
| 58 | + return pattern; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean matches(String line) { |
| 62 | + return this.pattern.matcher(line).find(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Set<Violation> parseReportOutput(String reportContent, ViolationsLogger violationsLogger) throws Exception { |
| 68 | + Set<Violation> violations = new TreeSet<>(); |
| 69 | + Iterator<String> linesItr = Arrays.asList(reportContent.split("\\r?\\n")).iterator(); |
| 70 | + while (linesItr.hasNext()) { |
| 71 | + String line = linesItr.next(); |
| 72 | + if (ADDED.matches(line)) { |
| 73 | + violations.add(parseAdded(line, linesItr)); |
| 74 | + } else if (DELETED.matches(line)) { |
| 75 | + violations.add(parseDeleted(line, linesItr)); |
| 76 | + } else if (CHANGED.matches(line)) { |
| 77 | + violations.add(parseChanged(line, linesItr)); |
| 78 | + } else { |
| 79 | + LOG.log(Level.FINE, "Was unable to detect diff state " + line); |
| 80 | + } |
| 81 | + } |
| 82 | + return violations; |
| 83 | + } |
| 84 | + |
| 85 | + private CommonParsed parseCommon(String line) { |
| 86 | + String[] orignalAndNew = line.split("[acd]"); |
| 87 | + |
| 88 | + String originalLines = orignalAndNew[0]; |
| 89 | + Matcher originalMatcher = LINE_PATTERN.matcher(originalLines); |
| 90 | + int startLineBefore = Integer.parseInt(originalMatcher.group(0)); |
| 91 | + Integer endLineBefore = null; |
| 92 | + if (originalMatcher.groupCount() > 1) { |
| 93 | + endLineBefore = Integer.parseInt(originalMatcher.group(1)); |
| 94 | + } |
| 95 | + |
| 96 | + String newLines = orignalAndNew[1]; |
| 97 | + Matcher newMatcher = LINE_PATTERN.matcher(newLines); |
| 98 | + int startLineAfter = Integer.parseInt(newMatcher.group(0)); |
| 99 | + Integer endLineAfter = null; |
| 100 | + if (newMatcher.groupCount() > 1) { |
| 101 | + endLineAfter = Integer.parseInt(newMatcher.group(1)); |
| 102 | + } |
| 103 | + return new CommonParsed(startLineBefore, endLineBefore, startLineAfter, endLineAfter); |
| 104 | + } |
| 105 | + |
| 106 | + private Violation parseChanged(String line, Iterator<String> linesItr) { |
| 107 | + CommonParsed commonParsed = parseCommon(line); |
| 108 | + new StringBuilder(); |
| 109 | + while (linesItr.hasNext()) { |
| 110 | + line = linesItr.next(); |
| 111 | + if (line.equals("---")) { |
| 112 | + break; |
| 113 | + } |
| 114 | + } |
| 115 | + return violationBuilder() |
| 116 | + .setStartLine(commonParsed.startLineBefore) |
| 117 | + .setEndLine(commonParsed.endLineBefore) |
| 118 | + .setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) |
| 119 | + .setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) |
| 120 | + .build(); |
| 121 | + } |
| 122 | + |
| 123 | + private Violation parseDeleted(String line, Iterator<String> linesItr) { |
| 124 | + CommonParsed commonParsed = parseCommon(line); |
| 125 | + return violationBuilder() |
| 126 | + .setStartLine(commonParsed.startLineBefore) |
| 127 | + .setEndLine(commonParsed.endLineBefore) |
| 128 | + .setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) |
| 129 | + .setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) |
| 130 | + .build(); |
| 131 | + } |
| 132 | + |
| 133 | + private Violation parseAdded(String line, Iterator<String> linesItr) { |
| 134 | + CommonParsed commonParsed = parseCommon(line); |
| 135 | + return violationBuilder() |
| 136 | + .setStartLine(commonParsed.startLineBefore) |
| 137 | + .setEndLine(commonParsed.endLineBefore) |
| 138 | + .setSpecific("START_LINE_AFTER", commonParsed.startLineAfter) |
| 139 | + .setSpecific("END_LINE_AFTER", commonParsed.endLineAfter) |
| 140 | + .build(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments