use of com.puppycrawl.tools.checkstyle.api.AuditListener in project checkstyle by checkstyle.
the class Checker method fireFileFinished.
/**
* Notify all listeners about the end of a file audit.
*
* @param fileName
* the audited file
*/
@Override
public void fireFileFinished(String fileName) {
final String stripped = CommonUtils.relativizeAndNormalizePath(basedir, fileName);
final AuditEvent event = new AuditEvent(this, stripped);
for (final AuditListener listener : listeners) {
listener.fileFinished(event);
}
}
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;
}
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 checkstyle by checkstyle.
the class CheckstyleAntTask method getListeners.
/**
* Return the list of listeners set in this task.
* @return the list of listeners.
*/
private AuditListener[] getListeners() {
final int formatterCount = Math.max(1, formatters.size());
final AuditListener[] listeners = new AuditListener[formatterCount];
// formatters
try {
if (formatters.isEmpty()) {
final OutputStream debug = new LogOutputStream(this, Project.MSG_DEBUG);
final OutputStream err = new LogOutputStream(this, Project.MSG_ERR);
listeners[0] = new DefaultLogger(debug, true, err, true);
} else {
for (int i = 0; i < formatterCount; i++) {
final Formatter formatter = formatters.get(i);
listeners[i] = formatter.createListener(this);
}
}
} catch (IOException ex) {
throw new BuildException(String.format(Locale.ROOT, "Unable to create listeners: " + "formatters {%s}.", formatters), ex);
}
return listeners;
}
use of com.puppycrawl.tools.checkstyle.api.AuditListener in project checkstyle by checkstyle.
the class Main method createListener.
/**
* Creates the audit listener.
*
* @param format format of the audit listener
* @param outputLocation the location of output
* @return a fresh new {@code AuditListener}
* @exception FileNotFoundException when provided output location is not found
*/
private static AuditListener createListener(String format, String outputLocation) throws FileNotFoundException {
// setup the output stream
final OutputStream out;
final boolean closeOutputStream;
if (outputLocation == null) {
out = System.out;
closeOutputStream = false;
} else {
out = new FileOutputStream(outputLocation);
closeOutputStream = true;
}
// setup a listener
final AuditListener listener;
if (XML_FORMAT_NAME.equals(format)) {
listener = new XMLLogger(out, closeOutputStream);
} else if (PLAIN_FORMAT_NAME.equals(format)) {
listener = new DefaultLogger(out, closeOutputStream, out, false);
} else {
if (closeOutputStream) {
CommonUtils.close(out);
}
throw new IllegalStateException(String.format("Invalid output format. Found '%s' but expected '%s' or '%s'.", format, PLAIN_FORMAT_NAME, XML_FORMAT_NAME));
}
return listener;
}
Aggregations