package edu.hm.hafner.analysis.parser;

import java.util.Optional;
import java.util.regex.Matcher;

import static edu.hm.hafner.analysis.Categories.guessCategoryIfEmpty;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.RegexpLineParser;

/**
 * A parser for the ruboCop warnings.
 *
 * @author David van Laatum
 */
public class RuboCopParser extends RegexpLineParser {
    private static final long serialVersionUID = 7199325311690082783L;

    private static final String RUBOCOP_WARNING_PATTERN = "^([^:]+):(\\d+):(\\d+): ([RCWEF]): (\\S+): (.*)$";
    private static final String ERROR = "E";
    private static final String FATAL = "F";

    /**
     * Creates a new instance of {@link RuboCopParser}.
     */
    public RuboCopParser() {
        super(RUBOCOP_WARNING_PATTERN);
    }

    @Override
    protected Optional<Issue> createIssue(final Matcher matcher, final IssueBuilder builder) {
        String message = matcher.group(6);
        String category = guessCategoryIfEmpty(matcher.group(5), message);

        String severity = matcher.group(4);
        Severity priority = Severity.WARNING_NORMAL;
        if (ERROR.equals(severity) || FATAL.equals(severity)) {
            priority = Severity.WARNING_HIGH;
        }

        return builder.setFileName(matcher.group(1))
                .setLineStart(matcher.group(2))
                .setCategory(category)
                .setMessage(message)
                .setSeverity(priority)
                .setColumnStart(matcher.group(3))
                .buildOptional();
    }
}


@@ -597,54 +597,122 @@
 RN =
- %22%5E(%5B%5E:%5D+):(%5C%5Cd+):(%5C%5Cd+): (%5BRCWEF%5D): (%5C%5CS+): (
+%0A            %22%5E(?%3Cfile%3E.%5B%5E:%5D+):(?%3Cline%3E%5C%5Cd+):(?%3Ccolumn%3E%5C%5Cd+): (?%3Cseverity%3E%5BRCWEF%5D): (?%3Ccategory%3E%5C%5CS+): (?%3Cmessage%3E
 .*)$
@@ -1101,17 +1101,25 @@
 r.group(
-6
+%22message%22
 );%0A     
@@ -1174,17 +1174,26 @@
 r.group(
-5
+%22category%22
 ), messa
@@ -1242,9 +1242,18 @@
 oup(
-4
+%22severity%22
 );%0A 
@@ -1474,17 +1474,22 @@
 r.group(
-1
+%22file%22
 ))%0A     
@@ -1527,17 +1527,22 @@
 r.group(
-2
+%22line%22
 ))%0A     
@@ -1697,17 +1697,24 @@
 r.group(
-3
+%22column%22
 ))%0A     

Convert RuboCop Parser to use named groups