Search in sources :

Example 1 with DefaultCFlintResultMarshaller

use of com.cflint.xml.stax.DefaultCFlintResultMarshaller 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)

Example 2 with DefaultCFlintResultMarshaller

use of com.cflint.xml.stax.DefaultCFlintResultMarshaller in project CFLint by cflint.

the class CFLintMain method execute.

private void execute() throws IOException, TransformerException, JAXBException, MarshallerException {
    final CFLint cflint = new CFLint(buildConfigChain());
    cflint.setVerbose(verbose);
    cflint.setLogError(logerror);
    cflint.setQuiet(quiet);
    cflint.setShowProgress(showprogress);
    cflint.setProgressUsesThread(progressUsesThread);
    if (extensions != null && extensions.trim().length() > 0) {
        try {
            cflint.setAllowedExtensions(Arrays.asList(extensions.trim().split(",")));
        } catch (final Exception e) {
            System.err.println("Unable to use extensions (" + extensions + ") using default instead. " + e.getMessage());
        }
    }
    final CFLintFilter filter = createBaseFilter();
    cflint.getBugs().setFilter(filter);
    for (final String scanfolder : folder) {
        cflint.scan(scanfolder);
    }
    if (stdIn) {
        final StringBuilder source = new StringBuilder();
        final Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            final String nextLine = scanner.nextLine();
            source.append(nextLine);
            source.append(System.lineSeparator());
        }
        scanner.close();
        cflint.process(source.toString(), stdInFile);
    }
    if (xmlOutput) {
        final Writer xmlwriter = stdOut ? new OutputStreamWriter(System.out) : createWriter(xmlOutFile, StandardCharsets.UTF_8);
        if ("findbugs".equalsIgnoreCase(xmlstyle)) {
            if (verbose) {
                display("Writing XML findbugs style" + (stdOut ? "." : " to " + xmlOutFile));
            }
            new XMLOutput().outputFindBugs(cflint.getBugs(), xmlwriter, showStats);
        } else {
            if (verbose) {
                display("Writing XML" + (stdOut ? "." : " to " + xmlOutFile));
            }
            new DefaultCFlintResultMarshaller().output(cflint.getBugs(), xmlwriter, showStats);
        }
    }
    if (textOutput) {
        if (textOutFile != null && verbose) {
            display("Writing text" + (stdOut ? "." : " to " + textOutFile));
        }
        final Writer textwriter = stdOut || textOutFile == null ? new OutputStreamWriter(System.out) : new FileWriter(textOutFile);
        new TextOutput().output(cflint.getBugs(), textwriter, showStats);
    }
    if (htmlOutput) {
        try {
            if (verbose) {
                display("Writing HTML" + (stdOut ? "." : " to " + htmlOutFile));
            }
            final Writer htmlwriter = stdOut ? new OutputStreamWriter(System.out) : new FileWriter(htmlOutFile);
            new HTMLOutput(htmlStyle).output(cflint.getBugs(), htmlwriter, showStats);
        } catch (final TransformerException e) {
            throw new IOException(e);
        }
    }
    if (jsonOutput) {
        if (verbose) {
            display("Writing JSON" + (stdOut ? "." : " to " + jsonOutFile));
        }
        final Writer jsonwriter = stdOut ? new OutputStreamWriter(System.out) : new FileWriter(jsonOutFile);
        new JSONOutput().output(cflint.getBugs(), jsonwriter, showStats);
    }
}
Also used : TextOutput(com.cflint.TextOutput) Scanner(java.util.Scanner) FileWriter(java.io.FileWriter) XMLOutput(com.cflint.XMLOutput) HTMLOutput(com.cflint.HTMLOutput) JSONOutput(com.cflint.JSONOutput) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) MarshallerException(com.cflint.xml.MarshallerException) CFLint(com.cflint.CFLint) CFLintFilter(com.cflint.tools.CFLintFilter) DefaultCFlintResultMarshaller(com.cflint.xml.stax.DefaultCFlintResultMarshaller) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

CFLint (com.cflint.CFLint)2 HTMLOutput (com.cflint.HTMLOutput)2 TextOutput (com.cflint.TextOutput)2 XMLOutput (com.cflint.XMLOutput)2 CFLintFilter (com.cflint.tools.CFLintFilter)2 DefaultCFlintResultMarshaller (com.cflint.xml.stax.DefaultCFlintResultMarshaller)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 TransformerException (javax.xml.transform.TransformerException)2 JSONOutput (com.cflint.JSONOutput)1 CFLintConfig (com.cflint.config.CFLintConfig)1 CFLintConfiguration (com.cflint.config.CFLintConfiguration)1 PluginMessage (com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginMessage)1 MarshallerException (com.cflint.xml.MarshallerException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Scanner (java.util.Scanner)1