Search in sources :

Example 6 with AuditListener

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);
    }
}
Also used : AuditEvent(com.puppycrawl.tools.checkstyle.api.AuditEvent) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener)

Example 7 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)

Example 8 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 9 with AuditListener

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;
}
Also used : LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream)

Example 10 with AuditListener

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;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener)

Aggregations

AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)11 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)4 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)3 AuditEvent (com.puppycrawl.tools.checkstyle.api.AuditEvent)3 IOException (java.io.IOException)3 XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)2 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 AutomaticBean (com.puppycrawl.tools.checkstyle.api.AutomaticBean)1 BeforeExecutionFileFilter (com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 FileSetCheck (com.puppycrawl.tools.checkstyle.api.FileSetCheck)1 Filter (com.puppycrawl.tools.checkstyle.api.Filter)1 FilterSet (com.puppycrawl.tools.checkstyle.api.FilterSet)1 LocalizedMessage (com.puppycrawl.tools.checkstyle.api.LocalizedMessage)1