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