package edu.hm.hafner.analysis.parser;

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

import org.apache.commons.lang3.StringUtils;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.RegexpDocumentParser;
import edu.hm.hafner.util.LookaheadStream;

/**
 * A parser for messages from the NAG Fortran Compiler.
 *
 * @author Mat Cross.
 */
public class NagFortranParser extends LookaheadParser {
    private static final long serialVersionUID = 2072414911276743946L;

    private static final String NAGFOR_MSG_PATTERN = "^(Info|Warning|Questionable|Extension|Obsolescent|Deleted "
            + "feature used|Error|Runtime Error|Fatal Error|Panic): (.+\\.[^,:\\n]+)(, line (\\d+))?: (.+(\\s+detected"
            + " at .+)?)";

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

    @Deprecated
    protected Optional<Issue> createIssue(final Matcher matcher, final IssueBuilder builder) {
        String category = matcher.group(1);

        return builder.setFileName(matcher.group(2))
                .setLineStart(getLineNumber(matcher))
                .setCategory(category)
                .setMessage(matcher.group(5))
                .setSeverity(mapPriority(category))
                .buildOptional();
    }

    @Override
    protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
            final IssueBuilder builder)
            throws ParsingException {

            StringBuilder messageBuilder = new StringBuilder(matcher.group(5));
            String category = matcher.group(1);

            while (lookahead.hasNext("\\s+ .+")) {
                messageBuilder.append("\n");
                messageBuilder.append(lookahead.next());
            }

            return builder.setFileName(matcher.group(2))
                    .setLineStart(getLineNumber(matcher))
                    .setCategory(category)
                    .setMessage(messageBuilder.toString())
                    .setSeverity(mapPriority(category))
                    .buildOptional();
    }

    private Severity mapPriority(final String category) {
        switch (category) {
            case "Error":
            case "Runtime Error":
            case "Fatal Error":
            case "Panic":
                return Severity.WARNING_HIGH;
            case "Info":
                return Severity.WARNING_LOW;
            default:
                return Severity.WARNING_NORMAL;
        }
    }

    private int getLineNumber(final Matcher matcher) {
        int lineNumber;
        if (StringUtils.isEmpty(matcher.group(4))) {
            lineNumber = 0;
        }
        else {
            lineNumber = Integer.parseInt(matcher.group(4));
        }
        return lineNumber;
    }
}

@@ -97,54 +97,8 @@
 r;%0A%0A
-import org.apache.commons.lang3.StringUtils;%0A%0A
 impo
@@ -334,31 +334,20 @@
 ner.
-analysis.RegexpDocument
+util.Integer
 Pars
@@ -1026,449 +1026,8 @@
  %7D%0A%0A
-    @Deprecated%0A    protected Optional%3CIssue%3E createIssue(final Matcher matcher, final IssueBuilder builder) %7B%0A        String category = matcher.group(1);%0A%0A        return builder.setFileName(matcher.group(2))%0A                .setLineStart(getLineNumber(matcher))%0A                .setCategory(category)%0A                .setMessage(matcher.group(5))%0A                .setSeverity(mapPriority(category))%0A                .buildOptional();%0A    %7D%0A%0A
     
@@ -1217,28 +1217,24 @@
  %7B%0A%0A        
-    
 StringBuilde
@@ -1293,61 +1293,9 @@
 ));%0A
-            String category = matcher.group(1);%0A%0A    
+%0A
     
@@ -1341,36 +1341,32 @@
 ) %7B%0A            
-    
 messageBuilder.a
@@ -1378,20 +1378,16 @@
 (%22%5Cn%22);%0A
-    
         
@@ -1439,27 +1439,19 @@
         
-    %7D%0A%0A    
+%7D%0A%0A
         
@@ -1507,28 +1507,24 @@
             
-    
 .setLineStar
@@ -1529,36 +1529,50 @@
 art(
-getLineNumber(matcher))%0A    
+IntegerParser.parseInt(matcher.group(4)))%0A
     
@@ -1600,22 +1600,26 @@
 ory(
-category)%0A    
+matcher.group(1))%0A
     
@@ -1681,28 +1681,24 @@
             
-    
 .setSeverity
@@ -1714,23 +1714,27 @@
 ity(
-category))%0A    
+matcher.group(1)))%0A
     
@@ -2178,297 +2178,7 @@
    %7D
-%0A%0A    private int getLineNumber(final Matcher matcher) %7B%0A        int lineNumber;%0A        if (StringUtils.isEmpty(matcher.group(4))) %7B%0A            lineNumber = 0;%0A        %7D%0A        else %7B%0A            lineNumber = Integer.parseInt(matcher.group(4));%0A        %7D%0A        return lineNumber;%0A    %7D
 %0A%7D%0A

Remove unused method and format file