All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cj.jshintmojo.reporter.CheckStyleReporter Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
package com.cj.jshintmojo.reporter;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cj.jshintmojo.cache.Result;
import com.cj.jshintmojo.jshint.JSHint;

/**
 * CheckStyle style xml reporter class.
 */
public class CheckStyleReporter implements JSHintReporter {

    /**
     * format type of this reporter.
     */
    public static final String FORMAT = "checkstyle";

    @Override
    public String report(Map results) {
        if(results == null){
            return "";
        }
        StringBuilder buf = new StringBuilder();
        buf.append("\n");
        buf.append("\n");
        String[] files = results.keySet().toArray(new String[0]);
        Arrays.sort(files);
        for(String file : files){
            Result result = results.get(file);
            buf.append("\t\n");
            for(JSHint.Error error : result.errors){
                buf.append(String.format("\t\t\n",
                        error.line.intValue(), error.character.intValue(), encode(error.reason), encode(error.code), severity(error.code)));
            }
            buf.append("\t\n");
        }
        buf.append("\n");

        return buf.toString();
    }
    
    private String severity(String errorCode) {
        if(StringUtils.isNotEmpty(errorCode)){
            switch(errorCode.charAt(0)){
            case 'E':
                return "error";
            case 'I':
                return "info";
            case 'W':
            default:
                break;
            }
        }
        return "warning";
    }
    
    private String encode(String str) {
        if(str == null){
            return "";
        }
        return str
                .replaceAll("&", "&")
                .replaceAll("\"", """)
                .replaceAll("'", "'")
                .replaceAll("<", "<")
                .replaceAll(">", ">");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy