KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, Garvin LeClaire <garvin.leclaire@insightbb.com>
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
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28
29 import org.dom4j.Document;
30 import org.dom4j.DocumentHelper;
31 import org.dom4j.Element;
32 import org.dom4j.io.OutputFormat;
33 import org.dom4j.io.XMLWriter;
34
35 import edu.umd.cs.findbugs.classfile.ClassDescriptor;
36
37 /**
38  * BugReporter to output warnings in xdocs format for Maven.
39  *
40  * @author Garvin LeClaire
41  */

42 public class XDocsBugReporter extends TextUIBugReporter {
43     private SortedBugCollection bugCollection = new SortedBugCollection();
44     private Project project;
45     private Document document;
46     private Element root;
47
48     private static final String JavaDoc ROOT_ELEMENT_NAME = "BugCollection";
49     private static final String JavaDoc PROJECT_ELEMENT_NAME = "Project";
50     private static final String JavaDoc ERRORS_ELEMENT_NAME = "Errors";
51     private static final String JavaDoc ANALYSIS_ERROR_ELEMENT_NAME = "AnalysisError";
52     private static final String JavaDoc MISSING_CLASS_ELEMENT_NAME = "MissingClass";
53     private static final String JavaDoc SUMMARY_HTML_ELEMENT_NAME = "SummaryHTML";
54
55     private static final String JavaDoc ELEMENT_NAME = "BugInstance";
56     private static final String JavaDoc FILE_ELEMENT_NAME = "file";
57
58
59     public XDocsBugReporter(Project project) {
60         this.project = project;
61
62         this.document = DocumentHelper.createDocument();
63         this.root = document.addElement(ROOT_ELEMENT_NAME);
64
65
66     }
67
68     public void observeClass(ClassDescriptor classDescriptor) {
69     }
70
71     @Override JavaDoc
72     public void logError(String JavaDoc message) {
73         bugCollection.addError(message);
74         super.logError(message);
75     }
76
77     @Override JavaDoc
78     public void reportMissingClass(ClassNotFoundException JavaDoc ex) {
79         bugCollection.addMissingClass(getMissingClassName(ex));
80         super.reportMissingClass(ex);
81     }
82
83     @Override JavaDoc
84     public void doReportBug(BugInstance bugInstance) {
85         if (bugCollection.add(bugInstance)) {
86             printBug(bugInstance);
87             notifyObservers(bugInstance);
88         }
89     }
90
91     @Override JavaDoc
92     protected void printBug(BugInstance bugInstance) {
93         try {
94             toElement(bugInstance);
95         } catch (Exception JavaDoc e) {
96             logError("Couldn't add Element", e);
97         }
98     }
99
100     public void finish() {
101
102         try {
103             writeXML(outputStream, project);
104         } catch (Exception JavaDoc e) {
105             logError("Couldn't write XML output", e);
106         }
107         outputStream.close();
108     }
109
110     private void writeXML(OutputStream JavaDoc out, Project project) throws IOException JavaDoc {
111         Document document = endDocument(project);
112
113         XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
114         writer.write(document);
115     }
116
117     private Document endDocument(Project project) {
118
119         // Save the error information
120
Element errorsElement = root.addElement(ERRORS_ELEMENT_NAME);
121         for (Iterator JavaDoc<AnalysisError> i = bugCollection.errorIterator(); i.hasNext();) {
122             AnalysisError analysisError = i.next();
123             errorsElement.addElement(ANALYSIS_ERROR_ELEMENT_NAME).setText(analysisError.getMessage());
124         }
125         for (Iterator JavaDoc<String JavaDoc> i = bugCollection.missingClassIterator(); i.hasNext();) {
126             errorsElement.addElement(MISSING_CLASS_ELEMENT_NAME).setText(i.next());
127         }
128
129         return document;
130     }
131
132     private static String JavaDoc xmlEscape(String JavaDoc theString)
133     {
134         //Replaces characters '>', '<', '"', '&', ''' with XML equivalents
135
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
136         int len = theString.length();
137         char theChar;
138         for (int i=0; i<len; i++)
139         {
140             theChar = theString.charAt(i);
141             switch(theChar){
142             case '>': buf.append("&gt;");
143             break;
144             case '<': buf.append("&lt;");
145             break;
146             case '"': buf.append("&quot;");
147             break;
148             case '&': buf.append("&amp;");
149             break;
150             case '\'': buf.append("&apos;");
151             break;
152             default: buf.append(theChar);
153             }
154         }
155         return buf.toString();
156     }
157     
158     public void toElement(BugInstance bugInstance) {
159
160         String JavaDoc className = bugInstance.getPrimaryClass().getClassName();
161         Element element = (Element) root.selectSingleNode(FILE_ELEMENT_NAME + "[@classname='" + className + "']");
162
163         if (element == null) {
164             element = root.addElement(FILE_ELEMENT_NAME);
165             element.addAttribute("classname", className);
166         }
167
168         element = element.addElement(ELEMENT_NAME);
169
170
171         element.addAttribute("type", bugInstance.getType());
172
173         switch (bugInstance.getPriority()) {
174         case Detector.EXP_PRIORITY:
175             element.addAttribute("priority", "Experimental");
176             break;
177         case Detector.LOW_PRIORITY:
178             element.addAttribute("priority", "Low");
179             break;
180         case Detector.NORMAL_PRIORITY:
181             element.addAttribute("priority", "Normal");
182             break;
183         case Detector.HIGH_PRIORITY:
184             element.addAttribute("priority", "High");
185             break;
186         }
187
188         element.addAttribute("message", xmlEscape(bugInstance.getMessage()));
189
190         SourceLineAnnotation line =
191                 bugInstance.getPrimarySourceLineAnnotation();
192         if (line == null) {
193             element.addAttribute("line", "0");
194         } else {
195             element.addAttribute("line", Integer.toString(line.getStartLine()));
196         }
197
198
199     }
200 /*
201     public static void main(String args[])
202     {
203         String x = "Less than: < Greater than: > Ampersand: & Quotation mark: \" Apostrophe: '";
204         String y = xmlEscape(x);
205         System.out.println(x);
206         System.out.println(y);
207     }
208 */

209     
210 }
211
212 // vim:ts=3
213
Popular Tags