Search in sources :

Example 1 with Language

use of net.sourceforge.pmd.lang.Language in project Gargoyle by callakrsos.

the class DoPMD method doPMD.

public int doPMD(GargoylePMDConfiguration configuration, List<ReportListener> listeners) {
    renerderWriter = RendererWriterFactory.get(WRITER_TYPE.StringWriter);
    // Load the RuleSets
    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration);
    RuleSets ruleSets = RulesetsFactoryUtils.getRuleSetsWithBenchmark(configuration.getRuleSets(), ruleSetFactory);
    if (ruleSets == null) {
        return 0;
    }
    Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
    List<DataSource> files = getApplicableFiles(configuration, languages);
    long reportStart = System.nanoTime();
    try {
        //			Renderer renderer = RendererFactory.createRenderer(configuration.getReportFormat(), configuration.getReportProperties()); //configuration.createRenderer();//createDefaultRenderer();
        List<Renderer> renderers = new LinkedList<>();
        //configuration.createRenderer();
        Renderer renderer = new DatabaseXmlRenderer();
        renderer.setWriter(renerderWriter.getWriter());
        renderer.start();
        renderers.add(renderer);
        Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
        RuleContext ctx = new RuleContext();
        final AtomicInteger violations = new AtomicInteger(0);
        ctx.getReport().addListener(new ReportListener() {

            @Override
            public void ruleViolationAdded(RuleViolation ruleViolation) {
                violations.incrementAndGet();
            }

            @Override
            public void metricAdded(Metric metric) {
            }
        });
        if (listeners != null && !listeners.isEmpty()) {
            for (ReportListener l : listeners) ctx.getReport().addListener(l);
        }
        processFiles(configuration, ruleSetFactory, files, ctx, renderers);
        reportStart = System.nanoTime();
        //				renderer.renderFileReport();
        renderer.end();
        renderer.flush();
        return violations.get();
    } catch (Exception e) {
        LOGGER.error(ValueUtil.toString(e));
        return 0;
    } finally {
        Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);
        try {
            close();
        } catch (IOException e) {
            LOGGER.error(ValueUtil.toString(e));
        }
    }
}
Also used : ReportListener(net.sourceforge.pmd.ReportListener) RuleContext(net.sourceforge.pmd.RuleContext) RuleViolation(net.sourceforge.pmd.RuleViolation) IOException(java.io.IOException) LinkedList(java.util.LinkedList) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) Language(net.sourceforge.pmd.lang.Language) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuleSets(net.sourceforge.pmd.RuleSets) Renderer(net.sourceforge.pmd.renderers.Renderer) Metric(net.sourceforge.pmd.stat.Metric)

Example 2 with Language

use of net.sourceforge.pmd.lang.Language in project Gargoyle by callakrsos.

the class DoPMD method getApplicableLanguages.

private Set<Language> getApplicableLanguages(GargoylePMDConfiguration configuration, RuleSets ruleSets) {
    Set<Language> languages = new HashSet<>();
    LanguageVersionDiscoverer discoverer = configuration.getLanguageVersionDiscoverer();
    for (Rule rule : ruleSets.getAllRules()) {
        Language language = rule.getLanguage();
        if (languages.contains(language)) {
            continue;
        }
        LanguageVersion version = discoverer.getDefaultLanguageVersion(language);
        if (RuleSet.applies(rule, version)) {
            languages.add(language);
        }
    }
    return languages;
}
Also used : Language(net.sourceforge.pmd.lang.Language) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) Rule(net.sourceforge.pmd.Rule) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) HashSet(java.util.HashSet)

Aggregations

Language (net.sourceforge.pmd.lang.Language)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReportListener (net.sourceforge.pmd.ReportListener)1 Rule (net.sourceforge.pmd.Rule)1 RuleContext (net.sourceforge.pmd.RuleContext)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 RuleSets (net.sourceforge.pmd.RuleSets)1 RuleViolation (net.sourceforge.pmd.RuleViolation)1 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)1 LanguageVersionDiscoverer (net.sourceforge.pmd.lang.LanguageVersionDiscoverer)1 Renderer (net.sourceforge.pmd.renderers.Renderer)1 Metric (net.sourceforge.pmd.stat.Metric)1 DataSource (net.sourceforge.pmd.util.datasource.DataSource)1 ReaderDataSource (net.sourceforge.pmd.util.datasource.ReaderDataSource)1