package hudson.plugins.warnings.parser;

import java.util.regex.Matcher;

import org.apache.commons.lang.StringUtils;

import hudson.Extension;

import hudson.plugins.analysis.util.model.Priority;


/**
 * A parser for Eclipse compiler warnings.
 *
 * @author Ulli Hafner
 */
@Extension
public class EclipseParser extends RegexpDocumentParser {
    private static final long serialVersionUID = 425883472788422955L;
    private static final String ANT_ECLIPSE_WARNING_PATTERN =
            "\\[?(WARNING|ERROR)\\]?\\s*(?:in)?\\s*(.*)(?:\\(at line\\s*(\\d+)\\)|:\\[(\\d+)).*" +
            "(?:\\r?\\n[^\\^\\n]*){1,3}" +
            "\\r?\\n(.*)([\\^]+).*" +
            "\\r?\\n(?:\\s*\\[.*\\]\\s*)?(.*)";

    /**
     * Creates a new instance of {@link EclipseParser}.
     */
    public EclipseParser() {
        super(Messages._Warnings_EclipseParser_ParserName(),
                Messages._Warnings_EclipseParser_LinkName(),
                Messages._Warnings_EclipseParser_TrendName(),
                ANT_ECLIPSE_WARNING_PATTERN, true);
    }

    @Override
    protected String getId() {
        return "Eclipse Java Compiler";
    }

    @Override
    public String getSmallImage() {
        return JavacParser.JAVA_SMALL_ICON;
    }

    @Override
    public String getLargeImage() {
        return JavacParser.JAVA_LARGE_ICON;
    }

    @Override
    protected Warning createWarning(final Matcher matcher) {
        String type = StringUtils.capitalize(matcher.group(1));
        Priority priority;
        if ("warning".equalsIgnoreCase(type)) {
            priority = Priority.NORMAL;
        }
        else {
            priority = Priority.HIGH;
        }
        Warning warning = createWarning(matcher.group(2), getLineNumber(getLine(matcher)), matcher.group(7), priority);

        int columnStart = StringUtils.defaultString(matcher.group(5)).length();
        int columnEnd = columnStart + matcher.group(6).length();
        warning.setColumnPosition(columnStart, columnEnd);

        return warning;
    }

    private String getLine(final Matcher matcher) {
        String eclipse34 = matcher.group(3);
        String eclipse38 = matcher.group(4);

        return StringUtils.defaultIfEmpty(eclipse34, eclipse38);
    }
}


@@ -530,202 +530,847 @@
 %5C%5C%5D?
-%5C%5Cs*(?:in)?%5C%5Cs*(.*)(?:%5C%5C(at line%5C%5Cs*(%5C%5Cd+)%5C%5C)%7C:%5C%5C%5B(%5C%5Cd+)).*%22 +%0D%0A            %22(?:%5C%5Cr?%5C%5Cn%5B%5E%5C%5C%5E%5C%5Cn%5D*)%7B1,3%7D%22 +%0D%0A            %22%5C%5Cr?%5C%5Cn(.*)(%5B%5C%5C%5E%5D+).*%22 +%0D%0A            %22%5C%5Cr?%5C%5Cn(?:%5C%5Cs*%5C%5C%5B.*%5C%5C%5D%5C%5Cs*)?(.*)%22;
+%22 +      // group 1 'type': WARNING or ERROR in optional %5B%5D%0D%0A            %22%5C%5Cs*(?:in)?%22 +                  // optional %22 in%22%0D%0A            %22%5C%5Cs*(.*)%22 +                     // group 2 'filename'%0D%0A            %22(?:%5C%5C(at line%5C%5Cs*(%5C%5Cd+)%5C%5C)%7C%22 +  // either group 3 'lineNumber': at line dd%0D%0A            %22:%5C%5C%5B(%5C%5Cd+)).*%22 +                // or group 4 'rowNumber': eg :%5Brow,col%5D - col ignored%0D%0A            %22(?:%5C%5Cr?%5C%5Cn%5B%5E%5C%5C%5E%5C%5Cn%5D*)%7B1,3%7D%22 +   // 1 or 3 ignored lines (no column pointer) eg source excerpt%0D%0A            %22%5C%5Cr?%5C%5Cn(.*)%22 +                  // newline then group 5 (indent for column pointers)%0D%0A            %22(%5B%5C%5C%5E%5D+).*%22 +                   // group 6 column pointers (%5E%5E%5E%5E%5E)%0D%0A            %22%5C%5Cr?%5C%5Cn(?:%5C%5Cs*%5C%5C%5B.*%5C%5C%5D%5C%5Cs*)?%22 + // newline then optional ignored text in %5B%5D (eg %5Bjavac%5D)%0D%0A            %22(.*)%22;                          // group 7 'message'
 %0D%0A%0D%0A

Add comments to Eclipse regex