* @date 2012-04-22 12:16
*/
public class CheckstyleViolationsPerSourceLineOfCodeAgent extends CheckstyleAgent {
@Override
public void writeReview(CheckstyleAgent.AuditEventList auditEventList) {
CollabReview collabReview = getAgentManager().getCollabReview();
Artifact artifact = collabReview.getRepository().getArtifact(auditEventList.getArtifactIdentifier());
int lines = LineCounter.countNonEmptyLines(getAgentManager().getCollabReview().getRepository(), auditEventList.getArtifactIdentifier());
int violations = auditEventList.getInfoCount() + auditEventList.getWarnCount() * 2 + auditEventList.getErrorCount() * 3;
float violationsPerLine = (float) violations / lines;
float quality = violationsPerLine <= 1 ?
(10 - 10 * violationsPerLine)
:
(-10 + 10 / violationsPerLine);
int rating = Math.round(quality);
String review = String.format("Source file statistics: Lines: %d Violations: %d (%d x1, %d x2, %d x3) Violations per Line: %.1f"
+ (auditEventList.hasException() ? " (EXCEPTION)\n" : "\n"),
lines, violations, auditEventList.getInfoCount(), auditEventList.getWarnCount(), auditEventList.getErrorCount(), violationsPerLine);
for (AuditEvent auditEvent : auditEventList.getEvents()) {
String severityString = ": ";
if (auditEvent.getSeverityLevel() == SeverityLevel.WARNING) {
severityString = " (!): ";
} else if (auditEvent.getSeverityLevel() == SeverityLevel.ERROR) {
severityString = " (!!):";
}
review += String.format("Line %3d%s %s\n",
auditEvent.getLine(),
severityString,
auditEvent.getMessage()
//auditEvent.getLine() > 0 ? artifact.getContent().split("\n")[auditEvent.getLine() - 1].trim() : ""
);
}
collabReview.getRepository().setReview(auditEventList.getArtifactIdentifier(), collabReview.getAuthorManager().getAuthor(getProposedName()), rating, review, false);
}