Search in sources :

Example 1 with JSONOutput

use of com.cflint.JSONOutput in project CFLint by cflint.

the class TestFiles method test.

@Test
public void test() throws IOException, URISyntaxException, JAXBException, TransformerException {
    final String inputString = FileUtil.loadFile(sourceFile);
    final File expectedFile = new File(sourceFile.getPath().replaceAll("\\.cf.", ".expected.txt"));
    final String expectedFileText = expectedFile.exists() ? FileUtil.loadFile(expectedFile) : null;
    String expectedText = expectedFileText;
    final CFLintConfiguration config = loadPluginInfo(sourceFile.getParentFile());
    CFLint cflint = new CFLint(config);
    cflint.setVerbose(true);
    cflint.setLogError(true);
    cflint.addExceptionListener(new CFLintExceptionListener() {

        @Override
        public void exceptionOccurred(Throwable exception, String messageCode, String filename, Integer line, Integer column, String functionName, String expression) {
            exception.printStackTrace();
            fail("Error scanning " + filename);
        }
    });
    cflint.process(inputString, sourceFile.getPath());
    //List<BugInfo> result = cflint.getBugs().getFlatBugList();
    StringWriter writer = new StringWriter();
    new JSONOutput().output(cflint.getBugs(), writer, false);
    String actualTree = writer.toString();
    if (expectedText == null || expectedText.trim().length() == 0) {
        writeExpectFile(expectedFile, actualTree);
        System.out.println("Tree written to " + expectedFile);
    } else {
        if (autoReplaceFailed && !actualTree.equals(expectedText)) {
            System.out.println("Replaced content of " + expectedFile);
            expectedText = actualTree;
            writeExpectFile(expectedFile, actualTree);
        }
        assertEquals(expectedText.replaceAll("\\\\", "/").replaceAll("/+", "/").replaceAll("\r\n", "\n"), actualTree.replaceAll("\\\\", "/").replaceAll("/+", "/").replaceAll("\r\n", "\n"));
    }
}
Also used : CFLintExceptionListener(com.cflint.plugins.exceptions.CFLintExceptionListener) StringWriter(java.io.StringWriter) CFLint(com.cflint.CFLint) CFLintConfiguration(com.cflint.config.CFLintConfiguration) JSONOutput(com.cflint.JSONOutput) File(java.io.File) Test(org.junit.Test)

Example 2 with JSONOutput

use of com.cflint.JSONOutput 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 JSONOutput (com.cflint.JSONOutput)2 HTMLOutput (com.cflint.HTMLOutput)1 TextOutput (com.cflint.TextOutput)1 XMLOutput (com.cflint.XMLOutput)1 CFLintConfiguration (com.cflint.config.CFLintConfiguration)1 CFLintExceptionListener (com.cflint.plugins.exceptions.CFLintExceptionListener)1 CFLintFilter (com.cflint.tools.CFLintFilter)1 MarshallerException (com.cflint.xml.MarshallerException)1 DefaultCFlintResultMarshaller (com.cflint.xml.stax.DefaultCFlintResultMarshaller)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Scanner (java.util.Scanner)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1