Search in sources :

Example 1 with LanguageFilenameFilter

use of net.sourceforge.pmd.lang.LanguageFilenameFilter in project pmd by pmd.

the class FileSelectorTest method testUnwantedJavaFile.

/**
 * Test unwanted selection of a java file.
 */
@Test
public void testUnwantedJavaFile() {
    LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    File javaFile = new File("/path/to/MyClass.java");
    boolean selected = fileSelector.accept(javaFile.getParentFile(), javaFile.getName());
    assertEquals("Unwanted java file must not be selected!", false, selected);
}
Also used : File(java.io.File) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) Test(org.junit.Test)

Example 2 with LanguageFilenameFilter

use of net.sourceforge.pmd.lang.LanguageFilenameFilter in project pmd by pmd.

the class FileSelectorTest method testWantedFile.

/**
 * Test wanted selection of a source file.
 */
@Test
public void testWantedFile() {
    LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    File javaFile = new File("/path/to/myFile.dummy");
    boolean selected = fileSelector.accept(javaFile.getParentFile(), javaFile.getName());
    assertEquals("This file should be selected !", true, selected);
}
Also used : File(java.io.File) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) Test(org.junit.Test)

Example 3 with LanguageFilenameFilter

use of net.sourceforge.pmd.lang.LanguageFilenameFilter in project pmd by pmd.

the class FileSelectorTest method testUnwantedFile.

/**
 * Test unwanted selection of a non source file.
 */
@Test
public void testUnwantedFile() {
    LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
    File javaFile = new File("/path/to/myFile.txt");
    boolean selected = fileSelector.accept(javaFile.getParentFile(), javaFile.getName());
    assertEquals("Not-source file must not be selected!", false, selected);
}
Also used : File(java.io.File) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) Test(org.junit.Test)

Example 4 with LanguageFilenameFilter

use of net.sourceforge.pmd.lang.LanguageFilenameFilter in project pmd by pmd.

the class Benchmarker method main.

/**
 * @param args
 *            String[]
 * @throws RuleSetNotFoundException
 * @throws IOException
 * @throws PMDException
 */
public static void main(String[] args) throws RuleSetNotFoundException, IOException, PMDException {
    String targetjdk = findOptionalStringValue(args, "--targetjdk", "1.4");
    Language language = LanguageRegistry.getLanguage("Java");
    LanguageVersion languageVersion = language.getVersion(targetjdk);
    if (languageVersion == null) {
        languageVersion = language.getDefaultVersion();
    }
    String srcDir = findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/");
    List<DataSource> dataSources = FileUtil.collectFiles(srcDir, new LanguageFilenameFilter(language));
    boolean debug = findBooleanSwitch(args, "--debug");
    boolean parseOnly = findBooleanSwitch(args, "--parse-only");
    if (debug) {
        System.out.println("Using " + language.getName() + " " + languageVersion.getVersion());
    }
    if (parseOnly) {
        Parser parser = PMD.parserFor(languageVersion, null);
        parseStress(parser, dataSources, debug);
    } else {
        String ruleset = findOptionalStringValue(args, "--ruleset", "");
        if (debug) {
            System.out.println("Checking directory " + srcDir);
        }
        Set<RuleDuration> results = new TreeSet<>();
        RuleSetFactory factory = new RuleSetFactory();
        if (StringUtils.isNotBlank(ruleset)) {
            stress(languageVersion, factory.createRuleSet(ruleset), dataSources, results, debug);
        } else {
            Iterator<RuleSet> i = factory.getRegisteredRuleSets();
            while (i.hasNext()) {
                stress(languageVersion, i.next(), dataSources, results, debug);
            }
        }
        TextReport report = new TextReport();
        report.generate(results, System.err);
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) DataSource(net.sourceforge.pmd.util.datasource.DataSource) Parser(net.sourceforge.pmd.lang.Parser) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) Language(net.sourceforge.pmd.lang.Language) TreeSet(java.util.TreeSet) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion)

Example 5 with LanguageFilenameFilter

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

the class DoPMD method internalGetApplicableFiles.

private List<DataSource> internalGetApplicableFiles(GargoylePMDConfiguration configuration, Set<Language> languages) {
    List<DataSource> files = new ArrayList<>();
    LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(languages);
    if (null != configuration.getInputPaths()) {
        files.addAll(FileUtil.collectFiles(configuration.getInputPaths(), fileSelector));
    }
    if (null != configuration.getSourceText()) {
        String filePaths = "SourceBase";
        if (null != configuration.getSourceFileName()) {
            filePaths = configuration.getSourceFileName();
        }
        filePaths = filePaths.replaceAll("\\r?\\n", ",");
        filePaths = filePaths.replaceAll(",+", ",");
        String sourceText = configuration.getSourceText();
        try {
            Reader reader = new StringReader(sourceText);
            files.addAll(Arrays.asList(new ReaderDataSource(reader, filePaths)));
        } catch (Exception e) {
            LOGGER.error(ValueUtil.toString(e));
        }
    }
    return files;
}
Also used : ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) LanguageFilenameFilter(net.sourceforge.pmd.lang.LanguageFilenameFilter) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource)

Aggregations

LanguageFilenameFilter (net.sourceforge.pmd.lang.LanguageFilenameFilter)6 File (java.io.File)4 DataSource (net.sourceforge.pmd.util.datasource.DataSource)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ReaderDataSource (net.sourceforge.pmd.util.datasource.ReaderDataSource)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 TreeSet (java.util.TreeSet)1 RuleSet (net.sourceforge.pmd.RuleSet)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 Language (net.sourceforge.pmd.lang.Language)1 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)1 Parser (net.sourceforge.pmd.lang.Parser)1