KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004,2005 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.io.BufferedOutputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.dom4j.Document;
29 import org.dom4j.Element;
30 import org.dom4j.io.OutputFormat;
31 import org.dom4j.io.XMLWriter;
32
33 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
34
35 /**
36  * Add human-readable messages to a dom4j tree containing
37  * FindBugs XML output. This transformation makes it easier
38  * to generate reports (such as HTML) from the XML.
39  *
40  * @see BugCollection
41  * @author David Hovemeyer
42  */

43 public class AddMessages {
44     private BugCollection bugCollection;
45     private Document document;
46
47     /**
48      * Constructor.
49      *
50      * @param bugCollection the BugCollection the dom4j was generated from
51      * @param document the dom4j tree
52      */

53     public AddMessages(BugCollection bugCollection, Document document) {
54         this.bugCollection = bugCollection;
55         this.document = document;
56     }
57
58     /**
59      * Add messages to the dom4j tree.
60      */

61     public void execute() {
62         Iterator JavaDoc<Element> elementIter = document.selectNodes("/BugCollection/BugInstance").iterator();
63         Iterator JavaDoc<BugInstance> bugInstanceIter = bugCollection.iterator();
64
65         Set JavaDoc<String JavaDoc> bugTypeSet = new HashSet JavaDoc<String JavaDoc>();
66         Set JavaDoc<String JavaDoc> bugCategorySet = new HashSet JavaDoc<String JavaDoc>();
67         Set JavaDoc<String JavaDoc> bugCodeSet = new HashSet JavaDoc<String JavaDoc>();
68
69         // Add short and long descriptions to BugInstance elements.
70
// We rely on the Document and the BugCollection storing
71
// the bug instances in the same order.
72
while (elementIter.hasNext() && bugInstanceIter.hasNext()) {
73             Element element = elementIter.next();
74             BugInstance bugInstance = bugInstanceIter.next();
75
76             String JavaDoc bugType = bugInstance.getType();
77             bugTypeSet.add(bugType);
78
79             BugPattern bugPattern = bugInstance.getBugPattern();
80             
81             bugCategorySet.add(bugPattern.getCategory());
82             bugCodeSet.add(bugPattern.getAbbrev());
83
84             element.addElement("ShortMessage").addText(
85                 bugPattern.getShortDescription());
86             element.addElement("LongMessage").addText(bugInstance.getMessage());
87
88             // Add pre-formatted display strings in "Message"
89
// elements for all bug annotations.
90
Iterator JavaDoc<Element> annElementIter = element.elements().iterator();
91             Iterator JavaDoc<BugAnnotation> annIter = bugInstance.annotationIterator();
92             while (annElementIter.hasNext() && annIter.hasNext()) {
93                 Element annElement = annElementIter.next();
94                 BugAnnotation ann = annIter.next();
95                 annElement.addElement("Message").addText(ann.toString());
96             }
97         }
98
99         // Add BugPattern elements for each referenced bug types.
100
addBugCategories(bugCategorySet);
101         addBugPatterns(bugTypeSet);
102         addBugCodes(bugCodeSet);
103     }
104
105     /**
106      * Add BugCategory elements.
107      *
108      * @param bugCategorySet all bug categories referenced in the BugCollection
109      */

110     private void addBugCategories(Set JavaDoc<String JavaDoc> bugCategorySet) {
111         Element root = document.getRootElement();
112         for (String JavaDoc category : bugCategorySet) {
113             Element element = root.addElement("BugCategory");
114             element.addAttribute("category", category);
115             Element description = element.addElement("Description");
116             description.setText(I18N.instance().getBugCategoryDescription(category));
117
118             BugCategory bc = I18N.instance().getBugCategory(category);
119             if (bc != null) { // shouldn't be null
120
String JavaDoc s = bc.getAbbrev();
121                 if (s != null) {
122                     Element abbrev = element.addElement("Abbreviation");
123                     abbrev.setText(s);
124                 }
125                 s = bc.getDetailText();
126                 if (s != null) {
127                     Element details = element.addElement("Details");
128                     details.setText(s);
129                 }
130             }
131         }
132     }
133
134     /**
135      * Add BugCode elements.
136      *
137      * @param bugCodeSet all bug codes (abbrevs) referenced in the BugCollection
138      */

139     private void addBugCodes(Set JavaDoc<String JavaDoc> bugCodeSet) {
140         Element root = document.getRootElement();
141         for (String JavaDoc bugCode : bugCodeSet) {
142             Element element = root.addElement("BugCode");
143             element.addAttribute("abbrev", bugCode);
144             Element description = element.addElement("Description");
145             description.setText(I18N.instance().getBugTypeDescription(bugCode));
146         }
147     }
148
149     private void addBugPatterns(Set JavaDoc<String JavaDoc> bugTypeSet) {
150         Element root = document.getRootElement();
151         for (String JavaDoc bugType : bugTypeSet) {
152             BugPattern bugPattern = I18N.instance().lookupBugPattern(bugType);
153             if (bugPattern == null)
154                 continue;
155             Element details = root.addElement("BugPattern");
156             details
157                     .addAttribute("type", bugType)
158                     .addAttribute("abbrev", bugPattern.getAbbrev())
159                     .addAttribute("category", bugPattern.getCategory());
160             details
161                     .addElement("ShortDescription")
162                     .addText(bugPattern.getShortDescription());
163             details
164                     .addElement("Details")
165                     .addCDATA(bugPattern.getDetailText());
166         }
167     }
168     
169     @SuppressWarnings JavaDoc("DM_EXIT")
170     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
171         if (args.length != 2) {
172             System.err.println("Usage: " + AddMessages.class.getName() + " <input collection> <output collection>");
173             System.exit(1);
174         }
175         
176         // Load plugins, in order to get message files
177
DetectorFactoryCollection.instance();
178         
179         String JavaDoc inputFile = args[0];
180         String JavaDoc outputFile = args[1];
181         Project project = new Project();
182         
183         SortedBugCollection inputCollection = new SortedBugCollection();
184         inputCollection.readXML(inputFile, project);
185         
186         Document document = inputCollection.toDocument(project);
187         
188         AddMessages addMessages = new AddMessages(inputCollection, document);
189         addMessages.execute();
190         
191         XMLWriter writer = new XMLWriter(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outputFile)),
192                 OutputFormat.createPrettyPrint());
193         writer.write(document);
194         writer.close();
195     }
196 }
197
198 // vim:ts=4
199
Popular Tags