Search in sources :

Example 1 with AuditListener

use of com.puppycrawl.tools.checkstyle.api.AuditListener in project checkstyle by checkstyle.

the class Main method runCheckstyle.

/**
     * Executes required Checkstyle actions based on passed parameters.
     * @param cliOptions
     *        pojo object that contains all options
     * @return number of violations of ERROR level
     * @throws FileNotFoundException
     *         when output file could not be found
     * @throws CheckstyleException
     *         when properties file could not be loaded
     */
private static int runCheckstyle(CliOptions cliOptions) throws CheckstyleException, FileNotFoundException {
    // setup the properties
    final Properties props;
    if (cliOptions.propertiesLocation == null) {
        props = System.getProperties();
    } else {
        props = loadProperties(new File(cliOptions.propertiesLocation));
    }
    // create a configuration
    final Configuration config = ConfigurationLoader.loadConfiguration(cliOptions.configLocation, new PropertiesExpander(props));
    // create a listener for output
    final AuditListener listener = createListener(cliOptions.format, cliOptions.outputLocation);
    // create RootModule object and run it
    int errorCounter = 0;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
    try {
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(config);
        rootModule.addListener(listener);
        // run RootModule
        errorCounter = rootModule.process(cliOptions.files);
    } finally {
        rootModule.destroy();
    }
    return errorCounter;
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) Properties(java.util.Properties) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File)

Example 2 with AuditListener

use of com.puppycrawl.tools.checkstyle.api.AuditListener in project checkstyle by checkstyle.

the class Checker method fireErrors.

/**
     * Notify all listeners about the errors in a file.
     *
     * @param fileName the audited file
     * @param errors the audit errors from the file
     */
@Override
public void fireErrors(String fileName, SortedSet<LocalizedMessage> errors) {
    final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName);
    boolean hasNonFilteredViolations = false;
    for (final LocalizedMessage element : errors) {
        final AuditEvent event = new AuditEvent(this, stripped, element);
        if (filters.accept(event)) {
            hasNonFilteredViolations = true;
            for (final AuditListener listener : listeners) {
                listener.addError(event);
            }
        }
    }
    if (hasNonFilteredViolations && cache != null) {
        cache.remove(fileName);
    }
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) LocalizedMessage(com.puppycrawl.tools.checkstyle.api.LocalizedMessage)

Example 3 with AuditListener

use of com.puppycrawl.tools.checkstyle.api.AuditListener in project methods-distance by sevntu-checkstyle.

the class ReportCli method main.

public static void main(String... args) throws CheckstyleException {
    final DependencyInformationConsumer consumer = new ViolationReporterDependencyInformationConsumer();
    final ModuleFactory moduleFactory = new DependencyInformationConsumerInjector(consumer);
    final DefaultConfiguration moduleConfig = new DefaultConfiguration(MethodCallDependencyCheckstyleModule.class.getCanonicalName());
    moduleConfig.addAttribute("screenLinesCount", "50");
    final TreeWalker tw = new TreeWalker();
    tw.setModuleFactory(moduleFactory);
    tw.finishLocalSetup();
    tw.setupChild(moduleConfig);
    final AuditListener listener = new DefaultLogger(System.out, false);
    final Checker checker = new Checker();
    checker.setModuleFactory(moduleFactory);
    checker.addFileSetCheck(tw);
    checker.addListener(listener);
    final List<File> files = Collections.singletonList(new File(args[0]));
    checker.process(files);
}
Also used : ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) Checker(com.puppycrawl.tools.checkstyle.Checker) ViolationReporterDependencyInformationConsumer(com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer) DependencyInformationConsumerInjector(com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector) TreeWalker(com.puppycrawl.tools.checkstyle.TreeWalker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) MethodCallDependencyCheckstyleModule(com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) ViolationReporterDependencyInformationConsumer(com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) File(java.io.File)

Example 4 with AuditListener

use of com.puppycrawl.tools.checkstyle.api.AuditListener in project maven-plugins by apache.

the class AbstractCheckstyleReport method getListener.

/**
 * Creates and returns the report generation listener.
 *
 * @return The audit listener.
 * @throws MavenReportException If something goes wrong.
 */
protected AuditListener getListener() throws MavenReportException {
    AuditListener listener = null;
    if (StringUtils.isNotEmpty(outputFileFormat)) {
        File resultFile = outputFile;
        OutputStream out = getOutputStream(resultFile);
        if ("xml".equals(outputFileFormat)) {
            listener = new XMLLogger(out, true);
        } else if ("plain".equals(outputFileFormat)) {
            listener = new DefaultLogger(out, true);
        } else {
            // TODO: failure if not a report
            throw new MavenReportException("Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");
        }
    }
    return listener;
}
Also used : XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 5 with AuditListener

use of com.puppycrawl.tools.checkstyle.api.AuditListener in project maven-plugins by apache.

the class CheckstyleViolationCheckMojo method getListener.

private AuditListener getListener() throws MojoFailureException, MojoExecutionException {
    AuditListener listener = null;
    if (StringUtils.isNotEmpty(outputFileFormat)) {
        File resultFile = outputFile;
        OutputStream out = getOutputStream(resultFile);
        if ("xml".equals(outputFileFormat)) {
            listener = new XMLLogger(out, true);
        } else if ("plain".equals(outputFileFormat)) {
            try {
                // Write a plain output file to the standard output file,
                // and write an XML output file to the temp directory that can be used to count violations
                outputXmlFile = File.createTempFile("checkstyle-result", ".xml");
                outputXmlFile.deleteOnExit();
                OutputStream xmlOut = getOutputStream(outputXmlFile);
                CompositeAuditListener compoundListener = new CompositeAuditListener();
                compoundListener.addListener(new XMLLogger(xmlOut, true));
                compoundListener.addListener(new DefaultLogger(out, true));
                listener = compoundListener;
            } catch (IOException e) {
                throw new MojoExecutionException("Unable to create temporary file", e);
            }
        } else {
            throw new MojoFailureException("Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");
        }
    }
    return listener;
}
Also used : XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger)

Aggregations

AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 File (java.io.File)5 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)4 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)4 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)4 OutputStream (java.io.OutputStream)4 Test (org.junit.jupiter.api.Test)4 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)2 XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)2 Properties (java.util.Properties)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)1 DependencyInformationConsumer (com.github.sevntu.checkstyle.module.DependencyInformationConsumer)1 MethodCallDependencyCheckstyleModule (com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule)1 ViolationReporterDependencyInformationConsumer (com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1