Search in sources :

Example 1 with XMLLogger

use of com.puppycrawl.tools.checkstyle.XMLLogger 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 2 with XMLLogger

use of com.puppycrawl.tools.checkstyle.XMLLogger 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 3 with XMLLogger

use of com.puppycrawl.tools.checkstyle.XMLLogger in project checkstyle by checkstyle.

the class TranslationCheckTest method testLogOutput.

@Test
public void testLogOutput() throws Exception {
    final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
    checkConfig.addProperty("requiredTranslations", "ja,de");
    checkConfig.addProperty("baseName", "^InputTranslation.*$");
    final Checker checker = createChecker(checkConfig);
    checker.setBasedir(getPath(""));
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final XMLLogger logger = new XMLLogger(out, AutomaticBean.OutputStreamOptions.NONE);
    checker.addListener(logger);
    final String defaultProps = getPath("InputTranslationCheckFireErrors.properties");
    final String translationProps = getPath("InputTranslationCheckFireErrors_de.properties");
    final File[] propertyFiles = { new File(defaultProps), new File(translationProps) };
    final String line = "1: ";
    final String firstErrorMessage = getCheckMessage(MSG_KEY_MISSING_TRANSLATION_FILE, "InputTranslationCheckFireErrors_ja.properties");
    final String secondErrorMessage = getCheckMessage(MSG_KEY, "anotherKey");
    verify(checker, propertyFiles, ImmutableMap.of(":1", Collections.singletonList(" " + firstErrorMessage), "InputTranslationCheckFireErrors_de.properties", Collections.singletonList(line + secondErrorMessage)));
    verifyXml(getPath("ExpectedTranslationLog.xml"), out, TranslationCheckTest::isFilenamesEqual, firstErrorMessage, secondErrorMessage);
}
Also used : XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)2 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 IOException (java.io.IOException)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1 Test (org.junit.jupiter.api.Test)1