Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.selesse.jxlint.report;
  
 
 import java.net.URL;
 import java.util.*;
 
 public class HtmlTemplatedReporter extends Reporter {
     private static final Logger LOGGER = LoggerFactory.getLogger(HtmlTemplatedReporter.class);
     private static final String WEB_JAR_RESOURCE_PATH = "META-INF/resources/webjars";
 
     private List<Enum<?>> violatedCategoryList;
     private Set<LintRulelintRuleSet;
     private Map<LintRuleIntegersummaryMap;
 
     public HtmlTemplatedReporter(PrintStream outProgramSettings settingsList<LintErrorlintErrorList) {
         super(outsettingslintErrorList);
         Set<Enum<?>> violatedCategories = Sets.newTreeSet(new Comparator<Enum<?>>() {
             @Override
             public int compare(Enum<?> o1Enum<?> o2) {
                 return o1.toString().compareToIgnoreCase(o2.toString());
             }
         });
          = Maps.<LintRuleLintRuleInteger>newTreeMap(new Comparator<LintRule>() {
             @Override
             public int compare(LintRule o1LintRule o2) {
                 return o1.getName().compareToIgnoreCase(o2.getName());
             }
         });
          = Sets.newLinkedHashSet();
 
         for (LintError lintError : lintErrorList) {
             LintRule violatedRule = lintError.getViolatedRule();
             .add(violatedRule);
 
             violatedCategories.add(violatedRule.getCategory());
             if (!.containsKey(violatedRule)) {
                 .put(violatedRule, 1);
             }
             else {
                 int occurrences = .get(violatedRule);
                 .put(violatedRuleoccurrences + 1);
             }
         }
 
         Collections.sort(lintErrorList, LintErrorOrderings.getCategoryNameFileLineNumberOrdering());
          = Lists.newArrayList(violatedCategories);
     }
 
     @Override
     public void writeReport() {
         Properties velocityProperties = new Properties();
         velocityProperties.setProperty(."classpath");
         velocityProperties.setProperty("classpath.resource.loader.class"ClasspathResourceLoader.class.getName());
         VelocityEngine velocityEngine = new VelocityEngine(velocityProperties);
         velocityEngine.init();
 
         Template template = velocityEngine.getTemplate("velocity/report.vm");
 
         VelocityContext context = getVelocityContext();
 
         StringWriter stringWriter = new StringWriter();
         template.merge(contextstringWriter);
 
         .println(stringWriter.toString());
         .close();
     }
 
     private VelocityContext getVelocityContext() {
         VelocityContext context = new VelocityContext();
 
         context.put("TemplateHelper"HtmlTemplateHelper.class);
 
         context.put("allCss"getAllCss());
         context.put("allJs"getAllJs());
         context.put("nameAndVersion".getProgramName() + " " + .getProgramVersion());
         context.put("date"new Date());
         context.put("lintErrorList");
         context.put("categoryList");
         context.put("navDataTargets"generateNavListString());
        context.put("summaryMap");
        context.put("errorSummaryString"getErrorReportString());
        context.put("lintRuleSet");
        return context;
    }
    private String generateNavListString(List<Enum<?>> categoryList) {
        StringBuilder dataTarget = new StringBuilder("#summary, ");
        for (int i = 0; i < categoryList.size(); i++) {
            Enum<?> enumType = categoryList.get(i);
            dataTarget.append("#").append(HtmlTemplateHelper.getHrefSafeName(enumType.toString()));
            if (i + 1 < categoryList.size()) {
                dataTarget.append(", ");
            }
        }
        return dataTarget.toString();
    }
    private String getAllCss() {
        List<StringcssFiles = Lists.newArrayList("bootstrap.min.css""prettify.css");
        return concatenateVendorResources(cssFiles);
    }
    private String getAllJs() {
        List<StringjsFiles = Lists.newArrayList(
                "prettify.js""jquery.min.js""jquery.tablesorter.min.js""bootstrap.min.js"
        );
        return concatenateVendorResources(jsFiles);
    }
    private String concatenateVendorResources(List<Stringresources) {
        StringBuilder concatenatedResource = new StringBuilder();
        for (String resource : resources) {
            URL resourceUrl = getResourcePath(resource);
            String tag = getTagFromResource(resource);
            try {
                concatenatedResource.append("<").append(tag).append(">");
                String resourceToString = Resources.toString(resourceUrl.);
                concatenatedResource.append(resourceToString);
                concatenatedResource.append("</").append(tag).append(">");
            }
            catch (IOException e) {
                .error("Error reading template resource"e);
            }
            concatenatedResource.append("\n");
        }
        return concatenatedResource.toString();
    }
    private String getTagFromResource(String resource) {
        String tag = "";
        if (resource.endsWith("css")) {
            tag = "style";
        }
        else if (resource.endsWith("js")) {
            tag = "script";
        }
        return tag;
    }
    private URL getResourcePath(String resourceName) {
        for (ClassPath.ResourceInfo resourceInfo : getClassPathWebJarResources()) {
            if (resourceInfo.getResourceName().endsWith("/" + resourceName)) {
                return resourceInfo.url();
            }
        }
        return null;
    }
        if ( == null) {
            try {
                ClassLoader classLoader = this.getClass().getClassLoader();
                if (classLoader == null) {
                    throw new IOException("ClassLoader was null for " + this.getClass().getName());
                }
                ImmutableSet<ClassPath.ResourceInforesources = ClassPath.from(classLoader).getResources();
                 = Collections2.filter(resourcesnew Predicate<ClassPath.ResourceInfo>() {
                    @Override
                    public boolean apply(ClassPath.ResourceInfo input) {
                        return input != null && input.getResourceName().startsWith();
                    }
                });
            }
            catch (IOException e) {
                 = ImmutableSet.of();
            }
        }
        return ;
    }
    @Override
    protected void printHeader() {}
    @Override
    protected void printCategoryHeader(Enum<?> category) {}
    @Override
    protected void printError(LintError error) {}
    @Override
    protected void printFooter() {}
New to GrepCode? Check out our FAQ X