Search in sources :

Example 1 with Reporter

use of org.revapi.Reporter in project revapi by revapi.

the class TextReporter method initialize.

@Override
public void initialize(@Nonnull AnalysisContext analysis) {
    // noinspection ConstantConditions
    if (analysis != null) {
        try {
            flushReports();
        } catch (IOException e) {
            throw new IllegalStateException("Failed to output previous analysis report.");
        }
    }
    this.analysis = analysis;
    String minLevel = analysis.getConfiguration().get("minSeverity").asString();
    String output = analysis.getConfiguration().get("output").asString();
    output = "undefined".equals(output) ? "out" : output;
    String templatePath = analysis.getConfiguration().get("template").asString();
    if ("undefined".equals(templatePath)) {
        templatePath = null;
    }
    boolean append = analysis.getConfiguration().get("append").asBoolean(false);
    this.minLevel = "undefined".equals(minLevel) ? DifferenceSeverity.POTENTIALLY_BREAKING : DifferenceSeverity.valueOf(minLevel);
    OutputStream out;
    switch(output) {
        case "out":
            out = System.out;
            break;
        case "err":
            out = System.err;
            break;
        default:
            File f = new File(output);
            if (f.exists() && !f.canWrite()) {
                LOG.warn("The configured file for text reporter, '" + f.getAbsolutePath() + "' is not a writable file." + " Defaulting the output to standard output.");
                out = System.out;
            } else {
                File parent = f.getParentFile();
                if (!parent.exists()) {
                    if (!parent.mkdirs()) {
                        LOG.warn("Failed to create directory structure to write to the configured output file '" + f.getAbsolutePath() + "'. Defaulting the output to standard output.");
                        out = System.out;
                        break;
                    }
                }
                try {
                    out = new FileOutputStream(output, append);
                } catch (FileNotFoundException e) {
                    LOG.warn("Failed to create the configured output file '" + f.getAbsolutePath() + "'." + " Defaulting the output to standard output.", e);
                    out = System.out;
                }
            }
    }
    shouldClose = out != System.out && out != System.err;
    this.output = new PrintWriter(new OutputStreamWriter(out, Charset.forName("UTF-8")));
    this.reports = new TreeSet<>((r1, r2) -> {
        Element r1El = r1.getOldElement() == null ? r1.getNewElement() : r1.getOldElement();
        Element r2El = r2.getOldElement() == null ? r2.getNewElement() : r2.getOldElement();
        // noinspection ConstantConditions
        return r1El.compareTo(r2El);
    });
    Configuration freeMarker = createFreeMarkerConfiguration();
    template = null;
    try {
        template = templatePath == null ? freeMarker.getTemplate("default-template-with-improbable-name@@#(*&$)(.ftl") : freeMarker.getTemplate(templatePath);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to initialize the freemarker template.", e);
    }
}
Also used : Reporter(org.revapi.Reporter) SortedSet(java.util.SortedSet) Report(org.revapi.Report) TemplateException(freemarker.template.TemplateException) Element(org.revapi.Element) LoggerFactory(org.slf4j.LoggerFactory) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Charset(java.nio.charset.Charset) DifferenceSeverity(org.revapi.DifferenceSeverity) OutputStreamWriter(java.io.OutputStreamWriter) Template(freemarker.template.Template) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Logger(org.slf4j.Logger) AnalysisContext(org.revapi.AnalysisContext) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Difference(org.revapi.Difference) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder) TemplateLoader(freemarker.cache.TemplateLoader) Configuration(freemarker.template.Configuration) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Element(org.revapi.Element) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 Configuration (freemarker.template.Configuration)1 DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1