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;
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations