Search in sources :

Example 1 with RuleSetFactory

use of net.sourceforge.pmd.RuleSetFactory 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 RuleSetFactory

use of net.sourceforge.pmd.RuleSetFactory in project maven-plugins by apache.

the class PmdReport method processFilesWithPMD.

private void processFilesWithPMD(PMDConfiguration pmdConfiguration, List<DataSource> dataSources) throws MavenReportException {
    RuleSetFactory ruleSetFactory = new RuleSetFactory(RuleSetFactory.class.getClassLoader(), RulePriority.valueOf(this.minimumPriority), false, true);
    RuleContext ruleContext = new RuleContext();
    try {
        getLog().debug("Executing PMD...");
        PMD.processFiles(pmdConfiguration, ruleSetFactory, dataSources, ruleContext, Arrays.<Renderer>asList(renderer));
        if (getLog().isDebugEnabled()) {
            getLog().debug("PMD finished. Found " + renderer.getViolations().size() + " violations.");
        }
    } catch (Exception e) {
        String message = "Failure executing PMD: " + e.getLocalizedMessage();
        if (!skipPmdError) {
            throw new MavenReportException(message, e);
        }
        getLog().warn(message, e);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleContext(net.sourceforge.pmd.RuleContext) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException) IOException(java.io.IOException) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 3 with RuleSetFactory

use of net.sourceforge.pmd.RuleSetFactory in project Gargoyle by callakrsos.

the class RulesetsFactoryUtils method getRulesetFactory.

public static RuleSetFactory getRulesetFactory(GargoylePMDConfiguration configuration) {
    RuleSetFactory ruleSetFactory = new RuleSetFactory();
    ruleSetFactory.setMinimumPriority(configuration.getMinimumPriority());
    if (!configuration.isRuleSetFactoryCompatibilityEnabled()) {
        ruleSetFactory.disableCompatibilityFilter();
    }
    return ruleSetFactory;
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory)

Aggregations

RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)3 IOException (java.io.IOException)2 RuleContext (net.sourceforge.pmd.RuleContext)2 FileNotFoundException (java.io.FileNotFoundException)1 LinkedList (java.util.LinkedList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReportListener (net.sourceforge.pmd.ReportListener)1 RuleSets (net.sourceforge.pmd.RuleSets)1 RuleViolation (net.sourceforge.pmd.RuleViolation)1 Language (net.sourceforge.pmd.lang.Language)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 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1 FileResourceCreationException (org.codehaus.plexus.resource.loader.FileResourceCreationException)1 ResourceNotFoundException (org.codehaus.plexus.resource.loader.ResourceNotFoundException)1