Search in sources :

Example 11 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class PMDTaskImpl method doTask.

private void doTask() {
    setupClassLoader();
    // Setup RuleSetFactory and validate RuleSets
    final ResourceLoader rl = setupResourceLoader();
    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration, rl);
    try {
        // This is just used to validate and display rules. Each thread will create its own ruleset
        String ruleSets = configuration.getRuleSets();
        if (StringUtils.isNotBlank(ruleSets)) {
            // Substitute env variables/properties
            configuration.setRuleSets(project.replaceProperties(ruleSets));
        }
        RuleSets rules = ruleSetFactory.createRuleSets(configuration.getRuleSets());
        logRulesUsed(rules);
    } catch (RuleSetNotFoundException e) {
        throw new BuildException(e.getMessage(), e);
    }
    if (configuration.getSuppressMarker() != null) {
        project.log("Setting suppress marker to be " + configuration.getSuppressMarker(), Project.MSG_VERBOSE);
    }
    // Start the Formatters
    for (Formatter formatter : formatters) {
        project.log("Sending a report to " + formatter, Project.MSG_VERBOSE);
        formatter.start(project.getBaseDir().toString());
    }
    // log("Setting Language Version " + languageVersion.getShortName(),
    // Project.MSG_VERBOSE);
    // TODO Do we really need all this in a loop over each FileSet? Seems
    // like a lot of redundancy
    RuleContext ctx = new RuleContext();
    Report errorReport = new Report();
    final AtomicInteger reportSize = new AtomicInteger();
    final String separator = System.getProperty("file.separator");
    for (FileSet fs : filesets) {
        List<DataSource> files = new LinkedList<>();
        DirectoryScanner ds = fs.getDirectoryScanner(project);
        String[] srcFiles = ds.getIncludedFiles();
        for (String srcFile : srcFiles) {
            File file = new File(ds.getBasedir() + separator + srcFile);
            files.add(new FileDataSource(file));
        }
        final String inputPaths = ds.getBasedir().getPath();
        configuration.setInputPaths(inputPaths);
        Renderer logRenderer = new AbstractRenderer("log", "Logging renderer") {

            @Override
            public void start() {
            // Nothing to do
            }

            @Override
            public void startFileAnalysis(DataSource dataSource) {
                project.log("Processing file " + dataSource.getNiceFileName(false, inputPaths), Project.MSG_VERBOSE);
            }

            @Override
            public void renderFileReport(Report r) {
                int size = r.size();
                if (size > 0) {
                    reportSize.addAndGet(size);
                }
            }

            @Override
            public void end() {
            // Nothing to do
            }

            @Override
            public String defaultFileExtension() {
                return null;
            }
        };
        List<Renderer> renderers = new ArrayList<>(formatters.size() + 1);
        renderers.add(logRenderer);
        for (Formatter formatter : formatters) {
            renderers.add(formatter.getRenderer());
        }
        try {
            PMD.processFiles(configuration, ruleSetFactory, files, ctx, renderers);
        } catch (RuntimeException pmde) {
            handleError(ctx, errorReport, pmde);
        }
    }
    int problemCount = reportSize.get();
    project.log(problemCount + " problems found", Project.MSG_VERBOSE);
    for (Formatter formatter : formatters) {
        formatter.end(errorReport);
    }
    if (failuresPropertyName != null && problemCount > 0) {
        project.setProperty(failuresPropertyName, String.valueOf(problemCount));
        project.log("Setting property " + failuresPropertyName + " to " + problemCount, Project.MSG_VERBOSE);
    }
    if (failOnRuleViolation && problemCount > maxRuleViolations) {
        throw new BuildException("Stopping build since PMD found " + problemCount + " rule violations in the code");
    }
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) RuleContext(net.sourceforge.pmd.RuleContext) FileSet(org.apache.tools.ant.types.FileSet) Report(net.sourceforge.pmd.Report) Formatter(net.sourceforge.pmd.ant.Formatter) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DataSource(net.sourceforge.pmd.util.datasource.DataSource) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractRenderer(net.sourceforge.pmd.renderers.AbstractRenderer) RuleSets(net.sourceforge.pmd.RuleSets) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) AbstractRenderer(net.sourceforge.pmd.renderers.AbstractRenderer) Renderer(net.sourceforge.pmd.renderers.Renderer) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 12 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class AbstractLanguageVersionTest method testRegisteredRulesets.

/**
 * Makes sure, that for each language a "rulesets.properties" file exists.
 *
 * @throws Exception
 *             any error
 */
@Test
public void testRegisteredRulesets() throws Exception {
    // only check for languages, that support rules
    if (expected == null || expected.getLanguage().getRuleChainVisitorClass() == null) {
        return;
    }
    ResourceLoader rl = new ResourceLoader();
    Properties props = new Properties();
    String rulesetsProperties = "rulesets/" + simpleTerseName + "/rulesets.properties";
    try (InputStream inputStream = rl.loadClassPathResourceAsStreamOrThrow(rulesetsProperties)) {
        props.load(inputStream);
    }
    String rulesetFilenames = props.getProperty("rulesets.filenames");
    assertNotNull(rulesetFilenames);
    RuleSetFactory factory = new RuleSetFactory();
    if (rulesetFilenames.trim().isEmpty()) {
        return;
    }
    String[] rulesets = rulesetFilenames.split(",");
    for (String r : rulesets) {
        InputStream stream = rl.loadClassPathResourceAsStream(r);
        assertNotNull(stream);
        stream.close();
        RuleSet ruleset = factory.createRuleSet(r);
        assertNotNull(ruleset);
    }
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) InputStream(java.io.InputStream) Properties(java.util.Properties) Test(org.junit.Test)

Example 13 with ResourceLoader

use of net.sourceforge.pmd.util.ResourceLoader in project pmd by pmd.

the class AbstractRuleSetFactoryTest method getRuleSetFileNames.

private List<String> getRuleSetFileNames(String language) throws IOException, RuleSetNotFoundException {
    List<String> ruleSetFileNames = new ArrayList<>();
    try {
        Properties properties = new Properties();
        try (InputStream is = new ResourceLoader().loadClassPathResourceAsStreamOrThrow("rulesets/" + language + "/rulesets.properties")) {
            properties.load(is);
        }
        String fileNames = properties.getProperty("rulesets.filenames");
        StringTokenizer st = new StringTokenizer(fileNames, ",");
        while (st.hasMoreTokens()) {
            ruleSetFileNames.add(st.nextToken());
        }
    } catch (RuleSetNotFoundException e) {
        // this might happen if a language is only support by CPD, but not
        // by PMD
        System.err.println("No ruleset found for language " + language);
    }
    return ruleSetFileNames;
}
Also used : ResourceLoader(net.sourceforge.pmd.util.ResourceLoader) StringTokenizer(java.util.StringTokenizer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Properties(java.util.Properties)

Aggregations

ResourceLoader (net.sourceforge.pmd.util.ResourceLoader)13 Test (org.junit.Test)9 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JavaUtilLoggingRule (net.sourceforge.pmd.junit.JavaUtilLoggingRule)2 MockRule (net.sourceforge.pmd.lang.rule.MockRule)2 Renderer (net.sourceforge.pmd.renderers.Renderer)2 DataSource (net.sourceforge.pmd.util.datasource.DataSource)2 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1 Logger (java.util.logging.Logger)1 Report (net.sourceforge.pmd.Report)1 RuleContext (net.sourceforge.pmd.RuleContext)1