package edu.umd.cs.findbugs;
import java.io.IOException;
import java.util.logging.Logger;
import org.dom4j.DocumentException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
public class AddAnnotation {
private static final Logger LOGGER = Logger.getLogger(AddAnnotation.class.getName());
private final BugCollection bugCollection;
private final String annotation;
public AddAnnotation(BugCollection bugCollection, String annotation) {
this.bugCollection = bugCollection;
this.annotation = annotation;
}
public AddAnnotation(String resultsFile, String annotation) throws IOException, DocumentException {
this(new SortedBugCollection(), annotation);
bugCollection.readXML(resultsFile);
}
public BugCollection getBugCollection() {
return bugCollection;
}
public Project getProject() {
return bugCollection.getProject();
}
public void execute() {
for (BugInstance bugInstance : bugCollection) {
if (bugInstance.annotationTextContainsWord(this.annotation)) {
continue;
}
String annotation = bugInstance.getAnnotationText();
StringBuilder buf = new StringBuilder();
if (!"".equals(annotation)) {
buf.append(annotation);
buf.append('\n');
}
buf.append(this.annotation);
bugInstance.setAnnotationText(buf.toString(), bugCollection);
}
}
@SuppressFBWarnings("DM_EXIT")
public static void main(String[] argv) throws Exception {
if (argv.length != 2) {
System.err.println("Usage: " + AddAnnotation.class.getName() + " <results file> <annotation>");
System.exit(1);
}
String filename = argv[0];
String annotation = argv[1];
AddAnnotation addAnnotation = new AddAnnotation(filename, annotation);
addAnnotation.execute();
addAnnotation.getBugCollection().writeXML(filename);
}
}