package archmapper.main.conformance;
import java.text.Collator;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewResource;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewResult;
import org.eclipse.tptp.platform.analysis.codereview.java.ast.ASTHelper;
import org.eclipse.tptp.platform.analysis.core.AnalysisConstants;
import org.eclipse.tptp.platform.analysis.core.rule.AbstractAnalysisRule;
public class StyleCheckResult extends CodeReviewResult {
private String label;
private boolean hasSourcecodeInformation = true;
public StyleCheckResult(String resName, int p_lineNumber,
int p_startPosSelection, int p_lengthSelection, ASTNode node,
IResource resource, AbstractAnalysisRule rule, String historyId,
String label, boolean hasSourcecodeInformation) {
super(resName, p_lineNumber, p_startPosSelection, p_lengthSelection,
node, resource, rule, historyId, true);
this.label = label;
this.hasSourcecodeInformation = hasSourcecodeInformation;
}
/**
* Overrides the parent class to provide a custom label for style checker
* results.
*
* @return The label string for this result
*/
public String getLabel() {
StringBuffer sb = new StringBuffer();
if (hasSourcecodeInformation) {
sb.append(getMarker().getResource().getName()).append(":").append(
getLineNumber()).append(' ');
}
if (label == null) {
sb.append(getLabelWithVariables());
} else {
sb.append(label);
}
return sb.toString();
}
/**
* Generate a code review result in the result view
*
* @param rule
* The rule that produced the result
* @param historyId
* The unique identifier of the history where results are stored
* @param node
* The node which will be highlighted in the editor when this
* result is viewed
*/
public static void generateResultsForASTNode(AbstractAnalysisRule rule,
String historyId, ASTNode node, CodeReviewResource resource,
String label) {
if (Collator.getInstance().equals(AnalysisConstants.ANALYSIS_REALTIME,
historyId)) {
resource.generateResultsForASTNode(rule, historyId, node);
return;
}
ASTNode parent = node;
while (parent != null && !ASTHelper.isStatement(parent)
&& parent.getNodeType() != ASTNode.METHOD_DECLARATION
&& parent.getNodeType() != ASTNode.TYPE_DECLARATION
&& parent.getNodeType() != ASTNode.FIELD_DECLARATION
&& parent.getNodeType() != ASTNode.IMPORT_DECLARATION) {
parent = parent.getParent();
}
boolean found = false;
List comments = resource.getResourceCompUnit().getCommentList();
if (parent != null && comments.size() > 0) {
int nodeLine = resource.getResourceCompUnit().getLineNumber(
parent.getStartPosition());
int line = 0;
for (Iterator it = comments.iterator(); it.hasNext() && !found
&& nodeLine > line;) {
Comment c = (Comment) it.next();
// $ANALYSIS-IGNORE,codereview.java.rules.loop.RuleLoopAssignLoopVariable
line = resource.getResourceCompUnit().getLineNumber(
c.getStartPosition());
if (c.getNodeType() == ASTNode.LINE_COMMENT) {
LineComment lc = (LineComment) c;
try {
String comment = resource.getICompilationUnit()
.getBuffer().getText(lc.getStartPosition(),
lc.getLength());
int index = comment
.indexOf(AnalysisConstants.IGNORE_TAG
+ AnalysisConstants.LIST_DELIMITER);
if (index == -1) {
index = comment
.indexOf(AnalysisConstants.IGNORE_TAG
+ AnalysisConstants.SPACE);
}
if (index != -1) {
comment = comment
.substring(index
+ AnalysisConstants.IGNORE_TAG
.length() + 1);
if (line == nodeLine - 1) {
StringTokenizer tokens = new StringTokenizer(
comment,
AnalysisConstants.LIST_DELIMITER);
while (tokens.hasMoreTokens() && !found) {
if (Collator.getInstance().equals(
tokens.nextToken(), rule.getId())) {
// $ANALYSIS-IGNORE,codereview.java.rules.loop.RuleLoopAssignLoopVariable
found = true;
}
}
}
}
} catch (JavaModelException e) {
// Do nothing - just ignore this file
}
}
}
}
if (!found) {
String oldLabel = rule.getLabel();
rule.setLabel(oldLabel+ ": "+ label);
StyleCheckResult result = new StyleCheckResult(resource
.getIResource().getFullPath().toString(), resource
.getResourceCompUnit().getLineNumber(
node.getStartPosition()), node.getStartPosition(),
node.getLength(), node, resource.getIResource(), rule,
historyId, label, true);
result.setOwner(rule);
rule.setLabel(oldLabel);
// Save the result in the history
rule.addHistoryResultSet(historyId, result);
}
}
public static void generateResultsWithoutSource(AbstractAnalysisRule rule, String historyId,
String label, IResource resource) {
String resourceName = "";
if (resource != null) {
resourceName = resource.getFullPath().toString();
}
ASTNode node = AST.newAST(AST.JLS3).newLineComment();
String oldLabel = rule.getLabel();
rule.setLabel(oldLabel+ ": "+ label);
StyleCheckResult result = new StyleCheckResult(resourceName, 0, 0, 0, node, resource,
rule, historyId, label, false);
rule.addHistoryResultSet(historyId, result);
rule.setLabel(oldLabel);
}
}