KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > AddAnnotation


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.dom4j.DocumentException;
27
28 /**
29  * Add an annotation string to every BugInstance in a BugCollection.
30  */

31 public class AddAnnotation {
32     private BugCollection bugCollection;
33     private Project project;
34     private String JavaDoc annotation;
35
36     public AddAnnotation(BugCollection bugCollection, Project project, String JavaDoc annotation) {
37         this.bugCollection = bugCollection;
38         this.project = project;
39         this.annotation = annotation;
40     }
41
42     public AddAnnotation(String JavaDoc resultsFile, String JavaDoc annotation)
43             throws IOException JavaDoc, DocumentException {
44         this(new SortedBugCollection(), new Project(), annotation);
45         bugCollection.readXML(resultsFile, project);
46     }
47
48     public BugCollection getBugCollection() {
49         return bugCollection;
50     }
51
52     public Project getProject() {
53         return project;
54     }
55
56     public void execute() {
57         for (Iterator JavaDoc<BugInstance> i = bugCollection.iterator(); i.hasNext();) {
58             BugInstance bugInstance = i.next();
59
60             // Don't add the annotation if it is already present
61
if (bugInstance.annotationTextContainsWord(this.annotation))
62                 continue;
63
64             String JavaDoc annotation = bugInstance.getAnnotationText();
65             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
66             if (!annotation.equals("")) {
67                 buf.append(annotation);
68                 buf.append('\n');
69             }
70             buf.append(this.annotation);
71             bugInstance.setAnnotationText(buf.toString());
72         }
73     }
74
75     @SuppressWarnings JavaDoc("DM_EXIT")
76     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
77         if (argv.length != 2) {
78             System.err.println("Usage: " + AddAnnotation.class.getName() + " <results file> <annotation>");
79             System.exit(1);
80         }
81
82         String JavaDoc filename = argv[0];
83         String JavaDoc annotation = argv[1];
84
85         AddAnnotation addAnnotation = new AddAnnotation(filename, annotation);
86         addAnnotation.execute();
87
88         addAnnotation.getBugCollection().writeXML(filename, addAnnotation.getProject());
89     }
90 }
91
92 // vim:ts=3
93
Popular Tags