Search in sources :

Example 1 with CFLintConfig

use of com.cflint.config.CFLintConfig in project CFLint by cflint.

the class CFLintMain method main.

public static void main(final String[] args) throws Exception {
    final Options options = new Options();
    options.addOption(RULES, false, "list of all supported rules");
    options.addOption("config", false, "list of rules in config file");
    options.addOption(INCLUDE_RULE, true, "specify rules to include");
    options.addOption(EXCLUDE_RULE, true, "specify rules to exclude");
    options.addOption(FOLDER, true, "folder(s) to scan");
    options.addOption("file", true, "file(s) to scan");
    options.addOption(FILTER_FILE, true, "filter file");
    options.addOption("v", false, VERBOSE);
    options.addOption("version", false, "show the version number");
    options.addOption("ui", false, "show UI");
    options.addOption(VERBOSE, false, VERBOSE);
    options.addOption(SHOWPROGRESS, false, "show progress bar");
    options.addOption("singlethread", false, "show progress bar");
    options.addOption("logerror", false, "log parsing errors as bugs");
    options.addOption("e", false, "log parsing errors as bugs");
    options.addOption("q", false, QUIET);
    options.addOption(QUIET, false, QUIET);
    options.addOption("?", false, DISPLAY_THIS_HELP);
    options.addOption("h", false, DISPLAY_THIS_HELP);
    options.addOption("help", false, DISPLAY_THIS_HELP);
    options.addOption("xml", false, "output in xml format");
    options.addOption(XMLFILE, true, "specify the output xml file (default: cflint-results.xml)");
    options.addOption(XMLSTYLE, true, "cflint,findbugs");
    options.addOption("html", false, "output in html format (default)");
    options.addOption(HTMLFILE, true, "specify the output html file (default: cflint-results.html)");
    // fancy,fancy-hist,summary
    options.addOption(HTMLSTYLE, true, "default,plain");
    options.addOption("json", false, "output in json format");
    options.addOption(JSONFILE, true, "specify the output json file (default: cflint-results.json)");
    options.addOption("text", false, "output in plain text");
    options.addOption(TEXTFILE, true, "specify the output text file (default: cflint-results.txt)");
    options.addOption("stats", false, "show bug count statstics");
    options.addOption(EXTENSIONS, true, "specify the extensions of the CF source files (default: .cfm,.cfc)");
    options.addOption(CONFIGFILE, true, "specify the location of the config file");
    options.addOption(STDIN, true, "use stdin for file input (default: source.cfc)");
    options.addOption("stdout", false, "output to stdout only");
    options.addOption("listrulegroups", false, "list rule groups");
    options.addOption("rulegroups", true, "rule groups");
    final CommandLineParser parser = new GnuParser();
    final CommandLine cmd = parser.parse(options, args);
    final CFLintMain main = new CFLintMain();
    if (cmd.hasOption('h') || cmd.hasOption("help") || cmd.hasOption("?")) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(CFLINT, options);
        return;
    }
    if (cmd.hasOption("version")) {
        System.out.println("CFLint " + Version.getVersion());
        System.out.println("CFParser " + cfml.parsing.Version.getVersion());
        return;
    }
    if (cmd.hasOption(RULES) || cmd.hasOption("config")) {
        final CFLintPluginInfo pluginInfo = cmd.hasOption(RULES) ? ConfigUtils.loadDefaultPluginInfo() : new CFLintPluginInfo();
        main.defaultConfig = new CFLintConfig();
        main.defaultConfig.setRules(pluginInfo.getRules());
        CFLintChainedConfig myConfig = new CFLintChainedConfig(main.defaultConfig);
        if (cmd.hasOption(CONFIGFILE)) {
            final String configfile = cmd.getOptionValue(CONFIGFILE);
            myConfig = myConfig.createNestedConfig(loadConfig(configfile));
        }
        final HashMap<String, String> descriptions = ConfigUtils.loadDescriptions();
        System.out.println("Supported rules");
        for (final PluginInfoRule rule : myConfig.getRules()) {
            System.out.println("  " + rule.getName());
            for (final PluginMessage message : rule.getMessages()) {
                System.out.println("    " + message.getCode() + " - " + descriptions.get(message.getCode()));
            }
        }
        return;
    }
    final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
    main.defaultConfig = new CFLintConfig();
    main.defaultConfig.setRules(pluginInfo.getRules());
    if (cmd.hasOption("listrulegroups")) {
        listRuleGroups(pluginInfo);
        return;
    }
    // groups are specified
    if (cmd.hasOption("rulegroups")) {
        final String rulegroups = cmd.getOptionValue("rulegroups");
        applyRuleGroups(main, pluginInfo, rulegroups);
    } else {
        // Exclude the experimental by default.
        applyRuleGroups(main, pluginInfo, "!Experimental");
    }
    main.verbose = (cmd.hasOption('v') || cmd.hasOption(VERBOSE));
    main.quiet = (cmd.hasOption('q') || cmd.hasOption(QUIET));
    main.logerror = (cmd.hasOption('e') || cmd.hasOption("logerror"));
    main.xmlOutput = cmd.hasOption("xml") || cmd.hasOption(XMLSTYLE) || cmd.hasOption(XMLFILE);
    main.textOutput = cmd.hasOption("text") || cmd.hasOption(TEXTFILE);
    main.jsonOutput = cmd.hasOption("json") || cmd.hasOption("jsonFile");
    main.showStats = cmd.hasOption("stats");
    if (cmd.hasOption("ui")) {
        main.ui();
    }
    // If an output is specified, htmlOutput is not defaulted to true.
    if (main.xmlOutput || main.textOutput || main.jsonOutput) {
        main.htmlOutput = cmd.hasOption("html") || cmd.hasOption(HTMLSTYLE) || cmd.hasOption(HTMLFILE);
    }
    if (main.verbose) {
        System.out.println("XML Output " + main.xmlOutput);
        System.out.println("Text Output " + main.textOutput);
        System.out.println("JSON Output " + main.jsonOutput);
        System.out.println("HTML Output " + main.htmlOutput);
    }
    if (cmd.hasOption(FOLDER)) {
        main.folder.addAll(Arrays.asList(cmd.getOptionValue(FOLDER).split(",")));
    }
    if (cmd.hasOption("file")) {
        main.folder.addAll(Arrays.asList(cmd.getOptionValue("file").split(",")));
    }
    if (cmd.hasOption(HTMLSTYLE)) {
        main.htmlStyle = cmd.getOptionValue(HTMLSTYLE);
        if (!main.htmlStyle.endsWith(".xsl") && !main.htmlStyle.endsWith(".xslt")) {
            main.htmlStyle = main.htmlStyle + ".xsl";
        }
    }
    if (cmd.hasOption(XMLSTYLE)) {
        main.xmlstyle = cmd.getOptionValue(XMLSTYLE);
    }
    if (cmd.hasOption(FILTER_FILE)) {
        main.filterFile = cmd.getOptionValue(FILTER_FILE);
    }
    if (cmd.hasOption(XMLFILE)) {
        main.xmlOutFile = cmd.getOptionValue(XMLFILE);
    }
    if (cmd.hasOption(JSONFILE)) {
        main.jsonOutFile = cmd.getOptionValue(JSONFILE);
    }
    if (cmd.hasOption(CONFIGFILE)) {
        final String configfile = cmd.getOptionValue(CONFIGFILE);
        main.configFileConfig = loadConfig(configfile);
    }
    if (cmd.hasOption(HTMLFILE)) {
        main.htmlOutFile = cmd.getOptionValue(HTMLFILE);
    }
    if (cmd.hasOption(TEXTFILE)) {
        main.textOutFile = cmd.getOptionValue(TEXTFILE);
    }
    if (cmd.hasOption(JSONFILE)) {
        main.jsonOutFile = cmd.getOptionValue(JSONFILE);
    }
    if (cmd.hasOption(EXTENSIONS)) {
        main.extensions = cmd.getOptionValue(EXTENSIONS);
    }
    if (cmd.hasOption(INCLUDE_RULE) || cmd.hasOption(EXCLUDE_RULE)) {
        main.cmdLineConfig = new CFLintConfig();
        if (cmd.hasOption(INCLUDE_RULE)) {
            for (final String code : cmd.getOptionValue(INCLUDE_RULE).split(",")) {
                main.cmdLineConfig.addInclude(new PluginMessage(code));
                main.cmdLineConfig.setInheritParent(false);
            }
        }
        if (cmd.hasOption(EXCLUDE_RULE)) {
            for (final String code : cmd.getOptionValue(EXCLUDE_RULE).split(",")) {
                main.cmdLineConfig.addExclude(new PluginMessage(code));
            }
        }
    }
    main.showprogress = cmd.hasOption(SHOWPROGRESS) || (!cmd.hasOption(SHOWPROGRESS) && cmd.hasOption("ui"));
    main.progressUsesThread = !cmd.hasOption("singlethread");
    main.stdIn = cmd.hasOption(STDIN);
    if (main.stdIn) {
        final String stdInOptionValue = cmd.getOptionValue(STDIN);
        if (stdInOptionValue != null) {
            main.stdInFile = stdInOptionValue;
        }
    }
    main.stdOut = cmd.hasOption("stdout");
    if (main.isValid()) {
        main.execute();
        if (cmd.hasOption("ui")) {
            main.open();
        }
    } else {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(CFLINT, options);
    }
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) CFLintChainedConfig(com.cflint.config.CFLintChainedConfig) HelpFormatter(org.apache.commons.cli.HelpFormatter) CommandLine(org.apache.commons.cli.CommandLine) CFLintPluginInfo(com.cflint.config.CFLintPluginInfo) CFLintConfig(com.cflint.config.CFLintConfig) PluginMessage(com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage) CommandLineParser(org.apache.commons.cli.CommandLineParser) PluginInfoRule(com.cflint.config.CFLintPluginInfo.PluginInfoRule)

Example 2 with CFLintConfig

use of com.cflint.config.CFLintConfig in project CFLint by cflint.

the class CFLintMain method loadConfig.

private static CFLintConfig loadConfig(final String configfile) {
    if (configfile != null) {
        try {
            CFLintPluginInfo pluginInfo = null;
            if (configfile.toLowerCase().endsWith(".xml")) {
                final Object configObj = ConfigUtils.unmarshal(new FileInputStream(configfile));
                if (configObj instanceof CFLintPluginInfo)
                    pluginInfo = (CFLintPluginInfo) configObj;
                else if (configObj instanceof CFLintConfig) {
                    return (CFLintConfig) configObj;
                }
            } else {
                pluginInfo = ConfigUtils.unmarshalJson(new FileInputStream(configfile), CFLintPluginInfo.class);
            }
            CFLintConfig returnVal = new CFLintConfig();
            returnVal.setRules(pluginInfo.getRules());
            return returnVal;
        } catch (final Exception e) {
            System.err.println("Unable to load config file. " + e.getMessage());
        }
    }
    return null;
}
Also used : CFLintPluginInfo(com.cflint.config.CFLintPluginInfo) CFLintConfig(com.cflint.config.CFLintConfig) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) MarshallerException(com.cflint.xml.MarshallerException)

Example 3 with CFLintConfig

use of com.cflint.config.CFLintConfig in project CFLint by cflint.

the class TestCLintConfigXml method createDefaultLimited.

public static CFLintConfiguration createDefaultLimited(final String... rulenames) {
    final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
    CFLintConfig defaultConfig = new CFLintConfig();
    for (CFLintPluginInfo.PluginInfoRule rule : pluginInfo.getRules()) {
        for (String rulename : rulenames) {
            if (rule.getName().equalsIgnoreCase(rulename)) {
                defaultConfig.getRules().add(rule);
            }
        }
    }
    return defaultConfig;
}
Also used : CFLintPluginInfo(com.cflint.config.CFLintPluginInfo) CFLintConfig(com.cflint.config.CFLintConfig)

Example 4 with CFLintConfig

use of com.cflint.config.CFLintConfig in project CFLint by cflint.

the class CFLint method setConfiguration.

public void setConfiguration(final CFLintConfiguration configFile) throws IOException {
    configuration = configFile == null ? new CFLintConfig() : configFile;
    extensions.clear();
    allowedExtensions.clear();
    scanProgressListeners.clear();
    exceptionListeners.clear();
    processed.clear();
    for (final PluginInfoRule ruleInfo : configuration.getRules()) {
        // TODO load them all
        addScanner(ConfigUtils.loadPlugin(ruleInfo));
    }
    allowedExtensions.addAll(AllowedExtensionsLoader.init(RESOURCE_BUNDLE_NAME));
    bugs.clearBugList();
}
Also used : CFLintConfig(com.cflint.config.CFLintConfig) PluginInfoRule(com.cflint.config.CFLintPluginInfo.PluginInfoRule)

Example 5 with CFLintConfig

use of com.cflint.config.CFLintConfig in project CFLint by cflint.

the class CFLintTask method execute.

@Override
public void execute() {
    FileInputStream fis = null;
    try {
        CFLintConfiguration config = null;
        if (configFile != null) {
            if (configFile.getName().toLowerCase().endsWith(".xml")) {
                config = ConfigUtils.unmarshal(configFile, CFLintConfig.class);
            } else {
                config = ConfigUtils.unmarshalJson(new FileInputStream(configFile), CFLintConfig.class);
            }
        }
        CFLintConfiguration cmdLineConfig = null;
        if ((excludeRule != null && excludeRule.trim().length() > 0) || (includeRule != null && includeRule.trim().length() > 0)) {
            cmdLineConfig = new CFLintConfig();
            if (includeRule != null && includeRule.trim().length() > 0) {
                for (final String code : includeRule.trim().split(",")) {
                    cmdLineConfig.addInclude(new PluginMessage(code));
                }
            }
            if (excludeRule != null && excludeRule.trim().length() > 0) {
                for (final String code : excludeRule.trim().split(",")) {
                    cmdLineConfig.addExclude(new PluginMessage(code));
                }
            }
        }
        // TODO combine configs
        final CFLint cflint = new CFLint(config);
        cflint.setVerbose(verbose);
        cflint.setQuiet(quiet);
        if (extensions != null && extensions.trim().length() > 0) {
            cflint.setAllowedExtensions(Arrays.asList(extensions.trim().split(",")));
        }
        CFLintFilter filter = CFLintFilter.createFilter(verbose);
        if (filterFile != null) {
            final File ffile = filterFile;
            if (ffile.exists()) {
                fis = new FileInputStream(ffile);
                final byte[] b = new byte[fis.available()];
                if (fis.read(b) > 0) {
                    fis.close();
                    filter = CFLintFilter.createFilter(new String(b), verbose);
                }
            }
        }
        cflint.getBugs().setFilter(filter);
        if (xmlFile == null && htmlFile == null && textFile == null) {
            xmlFile = new File("cflint-result.xml");
        }
        if (xmlFile != null) {
            if (verbose) {
                System.out.println("Style:" + xmlStyle);
            }
            if ("findbugs".equalsIgnoreCase(xmlStyle)) {
                new XMLOutput().outputFindBugs(cflint.getBugs(), createWriter(xmlFile, StandardCharsets.UTF_8), cflint.getStats());
            } else {
                new DefaultCFlintResultMarshaller().output(cflint.getBugs(), createWriter(xmlFile, StandardCharsets.UTF_8), cflint.getStats());
            }
        }
        if (textFile != null) {
            final Writer textwriter = textFile != null ? new FileWriter(textFile) : new OutputStreamWriter(System.out);
            new TextOutput().output(cflint.getBugs(), textwriter, cflint.getStats());
        }
        if (htmlFile != null) {
            try {
                new HTMLOutput(htmlStyle).output(cflint.getBugs(), new FileWriter(htmlFile), cflint.getStats());
            } catch (final TransformerException e) {
                throw new IOException(e);
            }
        }
        for (final FileSet fileset : filesets) {
            int progress = 1;
            // 3
            final DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
            final ProgressMonitor progressMonitor = showProgress && !filesets.isEmpty() ? new ProgressMonitor(null, "CFLint", "", 1, ds.getIncludedFilesCount()) : null;
            final String[] includedFiles = ds.getIncludedFiles();
            for (final String includedFile : includedFiles) {
                if (progressMonitor != null) {
                    if (progressMonitor.isCanceled()) {
                        throw new RuntimeException("CFLint scan cancelled");
                    }
                    final String filename = ds.getBasedir() + File.separator + includedFile;
                    progressMonitor.setNote("scanning " + includedFile);
                    cflint.scan(filename);
                    progressMonitor.setProgress(progress++);
                }
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (final IOException e) {
        }
    }
}
Also used : TextOutput(com.cflint.TextOutput) CFLintConfiguration(com.cflint.config.CFLintConfiguration) FileWriter(java.io.FileWriter) XMLOutput(com.cflint.XMLOutput) CFLint(com.cflint.CFLint) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) PluginMessage(com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage) DefaultCFlintResultMarshaller(com.cflint.xml.stax.DefaultCFlintResultMarshaller) TransformerException(javax.xml.transform.TransformerException) FileSet(org.apache.tools.ant.types.FileSet) HTMLOutput(com.cflint.HTMLOutput) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CFLint(com.cflint.CFLint) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) ProgressMonitor(javax.swing.ProgressMonitor) CFLintConfig(com.cflint.config.CFLintConfig) CFLintFilter(com.cflint.tools.CFLintFilter) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Aggregations

CFLintConfig (com.cflint.config.CFLintConfig)9 CFLintPluginInfo (com.cflint.config.CFLintPluginInfo)6 PluginInfoRule (com.cflint.config.CFLintPluginInfo.PluginInfoRule)5 PluginMessage (com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage)5 CFLintAPI (com.cflint.api.CFLintAPI)3 ConfigBuilder (com.cflint.config.ConfigBuilder)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 Before (org.junit.Before)3 CFLintChainedConfig (com.cflint.config.CFLintChainedConfig)2 FileNotFoundException (java.io.FileNotFoundException)2 JAXBException (javax.xml.bind.JAXBException)2 TransformerException (javax.xml.transform.TransformerException)2 CFLint (com.cflint.CFLint)1 HTMLOutput (com.cflint.HTMLOutput)1 TextOutput (com.cflint.TextOutput)1 XMLOutput (com.cflint.XMLOutput)1 CFLintConfiguration (com.cflint.config.CFLintConfiguration)1 CFLintFilter (com.cflint.tools.CFLintFilter)1 MarshallerException (com.cflint.xml.MarshallerException)1