Search in sources :

Example 1 with CFLintConfigurationException

use of com.cflint.exception.CFLintConfigurationException in project CFLint by cflint.

the class CFLintAPI method createFilter.

protected CFLintFilter createFilter() throws CFLintConfigurationException {
    try {
        if (filterFile != null) {
            final File ffile = new File(filterFile);
            if (ffile.exists()) {
                final FileInputStream fis = new FileInputStream(ffile);
                final byte[] b = new byte[fis.available()];
                IOUtils.read(fis, b);
                fis.close();
                return CFLintFilter.createFilter(new String(b), verbose);
            }
        }
        return CFLintFilter.createFilter(verbose);
    } catch (final Exception e) {
        throw new CFLintConfigurationException(e);
    }
}
Also used : CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException) File(java.io.File) FileInputStream(java.io.FileInputStream) CFLintScanException(com.cflint.exception.CFLintScanException) CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException)

Example 2 with CFLintConfigurationException

use of com.cflint.exception.CFLintConfigurationException in project CFLint by cflint.

the class CFLintAPI method createCFLintInstance.

private CFLint createCFLintInstance() throws CFLintConfigurationException {
    try {
        final CFLint cflint = new CFLint(configuration);
        cflint.setVerbose(verbose);
        cflint.setLogError(logError);
        cflint.setQuiet(quiet);
        cflint.setStrictIncludes(strictInclude);
        cflint.setAllowedExtensions(extensions);
        cflint.getBugs().setFilter(createFilter());
        return cflint;
    } catch (final Exception e) {
        throw new CFLintConfigurationException(e);
    }
}
Also used : CFLint(com.cflint.CFLint) CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException) CFLintScanException(com.cflint.exception.CFLintScanException) CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException)

Example 3 with CFLintConfigurationException

use of com.cflint.exception.CFLintConfigurationException in project CFLint by cflint.

the class CFLintCLI method execute.

private void execute(final CFLintConfiguration cfLintConfig) throws IOException, TransformerException, MarshallerException, JAXBException, CFLintScanException, CFLintConfigurationException {
    final CFLintAPI api = new CFLintAPI(cfLintConfig);
    api.setVerbose(verbose);
    api.setLogError(logerror);
    api.setQuiet(quiet);
    api.setStrictInclude(strictInclude);
    if (extensions != null && extensions.trim().length() > 0) {
        try {
            api.setExtensions(Arrays.asList(extensions.trim().split(",")));
        } catch (final Exception e) {
            System.err.println("Unable to use extensions (" + extensions + ") using default instead. " + e.getMessage());
        }
    }
    api.setFilterFile(filterFile);
    CFLintResult lintResult = null;
    if (stdIn) {
        final StringWriter source = new StringWriter();
        IOUtils.copy(new InputStreamReader(System.in), source);
        lintResult = api.scan(source.toString(), stdInFile);
    } else {
        lintResult = api.scan(folder);
    }
    if (xmlOutput) {
        try (final Writer xmlwriter = stdOut ? new OutputStreamWriter(System.out) : createXMLWriter(xmlOutFile, StandardCharsets.UTF_8)) {
            if (FINDBUGS.equalsIgnoreCase(xmlstyle)) {
                if (verbose) {
                    display("Writing XML (style: findbugs)" + (stdOut ? "." : " to " + xmlOutFile));
                }
                lintResult.writeFindBugsXml(xmlwriter);
            } else {
                if (verbose) {
                    display("Writing XML" + (stdOut ? "." : " to " + xmlOutFile));
                }
                lintResult.writeXml(xmlwriter);
            }
        }
    }
    if (textOutput) {
        try (final Writer textwriter = stdOut || textOutFile == null ? new OutputStreamWriter(System.out) : new FileWriter(textOutFile)) {
            if (textOutFile != null && verbose) {
                display("Writing text" + (stdOut ? "." : " to " + textOutFile));
            }
            lintResult.writeText(textwriter);
        }
    }
    if (htmlOutput) {
        try (final Writer htmlwriter = stdOut ? new OutputStreamWriter(System.out) : new FileWriter(htmlOutFile)) {
            if (verbose) {
                display("Writing HTML (style: " + htmlStyle + ")" + (stdOut ? "." : " to " + htmlOutFile));
            }
            lintResult.writeHTML(htmlStyle, htmlwriter);
        }
    }
    if (jsonOutput) {
        try (final Writer jsonwriter = stdOut ? new OutputStreamWriter(System.out) : new FileWriter(jsonOutFile)) {
            if (verbose) {
                display("Writing JSON" + (stdOut ? "." : " to " + jsonOutFile));
            }
            lintResult.writeJSON(jsonwriter);
        }
    }
    if (verbose) {
        display("Total files scanned: " + lintResult.getStats().getFileCount());
        display("Total LOC scanned: " + lintResult.getStats().getTotalLines());
    }
}
Also used : CFLintAPI(com.cflint.api.CFLintAPI) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) FileWriter(java.io.FileWriter) OutputStreamWriter(java.io.OutputStreamWriter) TransformerException(javax.xml.transform.TransformerException) CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) CFLintScanException(com.cflint.exception.CFLintScanException) MarshallerException(com.cflint.xml.MarshallerException) CFLintResult(com.cflint.api.CFLintResult) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 4 with CFLintConfigurationException

use of com.cflint.exception.CFLintConfigurationException in project CFLint by cflint.

the class ConfigFileLoader method loadConfig.

/**
 * Load the configuration from a file, ignore a null configuration filename.
 * bubble up any exceptions.
 *
 * @param configfile        the config file
 * @return                  the configuration
 * @throws CFLintConfigurationException exception thrown when there is an issue with the configuration
 */
public static CFLintConfig loadConfig(final String configfile) throws CFLintConfigurationException {
    if (configfile != null) {
        try {
            CFLintPluginInfo pluginInfo = null;
            if (configfile.toLowerCase().endsWith(".xml")) {
                @SuppressWarnings("deprecation") final Object configObj = ConfigUtils.unmarshal(new File(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);
            }
            final CFLintConfig returnVal = new CFLintConfig();
            returnVal.setRules(pluginInfo.getRules());
            return returnVal;
        } catch (final Exception e) {
            throw new CFLintConfigurationException(e);
        }
    }
    return null;
}
Also used : CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException) File(java.io.File) FileInputStream(java.io.FileInputStream) CFLintConfigurationException(com.cflint.exception.CFLintConfigurationException)

Aggregations

CFLintConfigurationException (com.cflint.exception.CFLintConfigurationException)4 CFLintScanException (com.cflint.exception.CFLintScanException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 CFLint (com.cflint.CFLint)1 CFLintAPI (com.cflint.api.CFLintAPI)1 CFLintResult (com.cflint.api.CFLintResult)1 MarshallerException (com.cflint.xml.MarshallerException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1