KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Search for bug instances whose text annotations contain
28  * one of a set of keywords.
29  */

30 public abstract class QueryBugAnnotations {
31     // Bug's text annotation must contain one of the key
32
// words in order to match
33
private HashSet JavaDoc<String JavaDoc> keywordSet = new HashSet JavaDoc<String JavaDoc>();
34
35     /**
36      * Add a keyword to the query.
37      * A BugInstance's text annotation must contain at least
38      * one keyword in order to match the query.
39      *
40      * @param keyword the keyword
41      */

42     public void addKeyword(String JavaDoc keyword) {
43         keywordSet.add(keyword);
44     }
45
46     /**
47      * Scan bug instances contained in given file,
48      * reporting those whose text annotations contain at least
49      * one of the keywords in the query.
50      *
51      * @param filename an XML file containing bug instances
52      */

53     public void scan(String JavaDoc filename) throws Exception JavaDoc {
54         BugCollection bugCollection = new SortedBugCollection();
55         bugCollection.readXML(filename, new Project());
56         scan(bugCollection, filename);
57     }
58
59     /**
60      * Scan bug instances contained in given bug collection,
61      * reporting those whose text annotations contain at least
62      * one of the keywords in the query.
63      *
64      * @param bugCollection the bug collection
65      * @param filename the XML file from which the bug collection was read
66      */

67     public void scan(BugCollection bugCollection, String JavaDoc filename) throws Exception JavaDoc {
68         Iterator JavaDoc<BugInstance> i = bugCollection.iterator();
69         while (i.hasNext()) {
70             BugInstance bugInstance = i.next();
71
72             Set JavaDoc<String JavaDoc> contents = bugInstance.getTextAnnotationWords();
73             for (String JavaDoc aKeywordSet : keywordSet) {
74                 if (contents.contains(aKeywordSet)) {
75                     match(bugInstance, filename);
76                     break;
77                 }
78             }
79         }
80     }
81
82     /**
83      * Called when a bug instance contains a query keyword.
84      *
85      * @param bugInstance the bug instance containing the keyword
86      * @param filename name of the file containing the bug instance
87      */

88     protected abstract void match(BugInstance bugInstance, String JavaDoc filename) throws Exception JavaDoc;
89 }
90
91 // vim:ts=4
92
Popular Tags