/*
Copyright 2012 Christian Prause and Fraunhofer FIT
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package net.sf.collabreview.agents;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import net.sf.collabreview.core.Artifact;
import net.sf.collabreview.core.CollabReview;
import net.sf.collabreview.core.toolbox.LineCounter;
/**
* @author Christian Prause (prause)
* @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);
}
@Override
public String getProposedName() {
return "CheckstyleVPSLOC";
}
}