001: package tide.exttools.findbugs;
002:
003: import java.io.*;
004: import java.util.*;
005: import org.xml.sax.*;
006:
007: /** Contains the tag objects mapping corresponding to the finbugs XML tags
008: */
009: public abstract class FindBugsTag {
010: private final static boolean debug = false;
011:
012: abstract void startElement(String namespaceURI, String lName, // local name
013: String qName, // qualified name
014: Attributes attrs) throws SAXException;
015:
016: // inner classes 77
017:
018: public static class Int extends FindBugsTag {
019:
020: String role = "";
021: String intvalue = "";
022:
023: public Int(Attributes attrs) {
024: readAttributes("Class", attrs);
025: }
026:
027: @Override
028: public void startElement(String namespaceURI, String lName, // local name
029: String qName, // qualified name
030: Attributes attrs) throws SAXException {
031: readAttributes(qName, attrs);
032: }
033:
034: private void readAttributes(String parentQname, Attributes attrs) {
035: for (int i = 0; i < attrs.getLength(); i++) {
036: String qname = attrs.getQName(i);
037: String value = attrs.getValue(qname);
038:
039: if (qname.equals("role"))
040: role = value;
041: else if (qname.equals("value"))
042: intvalue = value;
043: else {
044: System.out.println("Class ? " + parentQname + ": "
045: + qname + "= " + value);
046: }
047: }
048: }
049:
050: @Override
051: public String toString() {
052: StringBuilder sb = new StringBuilder();
053: if (role.length() > 0)
054: sb.append(role + " = ");
055: else {
056: sb.append("Int= ");
057: }
058: sb.append(intvalue);
059:
060: return sb.toString();
061: }
062:
063: } // Int
064:
065: public static class PackageStats extends FindBugsTag {
066: final Vector<ClassStats> classStats = new Vector<ClassStats>();
067: public String packageName;
068: public String total_bugs;
069:
070: public PackageStats(Attributes attrs) {
071: readAttributes(attrs);
072: }
073:
074: private void readAttributes(Attributes attrs) {
075: for (int i = 0; i < attrs.getLength(); i++) {
076: String qname = attrs.getQName(i);
077: String value = attrs.getValue(qname);
078:
079: if (qname.equals("package")) {
080: packageName = value;
081: }
082: if (qname.equals("total_bugs")) {
083: total_bugs = value;
084: }
085:
086: /*else
087: {
088: //System.out.println("PackageStats ? "+qname +" = "+value);
089: }*/
090: }
091: }
092:
093: @Override
094: public void startElement(String namespaceURI, String lName, // local name
095: String qName, // qualified name
096: Attributes attrs) throws SAXException {
097: // create
098: if (qName.equals("ClassStats")) {
099: classStats.addElement(new ClassStats(attrs));
100: }
101: }
102:
103: //no? @Override
104: public void endElement(String namespaceURI, String sName, // simple name
105: String qName // qualified name
106: ) throws SAXException {
107: /* if(qName.equals("PackageStats"))
108: {
109: packageStats.addElement(actual);
110: actual = null;
111: }
112: else
113: {
114: // delegate
115: if(actual!=null)
116: {
117: actual.endElement(namespaceURI, sName, qName);
118: }
119: System.out.println("FindBugsSummary unknown end "+sName);
120: }*/
121: }
122:
123: } // PackageStats
124:
125: public static class MissingClass {
126: public String text;
127:
128: public MissingClass(String text) {
129: this .text = text;
130: }
131:
132: @Override
133: public String toString() {
134: return "Missing class; " + text;
135: }
136:
137: } // MissingClass
138:
139: public static class AnalysisError {
140: public String text;
141:
142: public AnalysisError(String text) {
143: this .text = text;
144: }
145:
146: @Override
147: public String toString() {
148: return "Analysis Error: " + text;
149: }
150:
151: } // AnalysisError
152:
153: public static class Class extends FindBugsTag {
154: public String className = "";
155: String role = "";
156:
157: public Class(Attributes attrs) {
158: readAttributes("Class", attrs);
159: }
160:
161: @Override
162: public void startElement(String namespaceURI, String lName, // local name
163: String qName, // qualified name
164: Attributes attrs) throws SAXException {
165: readAttributes(qName, attrs);
166: }
167:
168: private void readAttributes(String parentQname, Attributes attrs) {
169: for (int i = 0; i < attrs.getLength(); i++) {
170: String qname = attrs.getQName(i);
171: String value = attrs.getValue(qname);
172:
173: if (qname.equals("classname"))
174: className = value;
175: else if (qname.equals("role"))
176: role = value;
177: else {
178: System.out.println("Class ? " + parentQname + ": "
179: + qname + "= " + value);
180: }
181: }
182: }
183:
184: @Override
185: public String toString() {
186: StringBuilder sb = new StringBuilder();
187: if (role.length() > 0)
188: sb.append(role + ": Class " + className);
189: else {
190: sb.append("In Class " + className);
191: }
192:
193: // sb.append("\r\n\tat "+className+"("+Field.extractJavaFileName(className)+":-1)");
194:
195: return sb.toString();
196: }
197:
198: } // Class
199:
200: public static class Method extends FindBugsTag {
201: SourceLine sourceLine = null;
202:
203: String name = "";
204: String classname = "";
205: String signature = "";
206: String role = "";
207:
208: public Method(Attributes attrs) {
209: readAttributes("Method", attrs);
210: }
211:
212: @Override
213: public void startElement(String namespaceURI, String lName, // local name
214: String qName, // qualified name
215: Attributes attrs) throws SAXException {
216: if (qName.equals("SourceLine") || qName.equals("SourcLine")) // a Bug in Bugfinder !
217: {
218: sourceLine = new SourceLine(attrs);
219: } else {
220: readAttributes(qName, attrs);
221: }
222: }
223:
224: private void readAttributes(String parentQname, Attributes attrs) {
225: for (int i = 0; i < attrs.getLength(); i++) {
226: String qname = attrs.getQName(i);
227: String value = attrs.getValue(qname);
228:
229: if (qname.equals("name")) {
230: name = value;
231: } else if (qname.equals("classname")) {
232: classname = value;
233: } else if (qname.equals("signature")) {
234: signature = value;
235: } else if (qname.equals("role")) {
236: role = value;
237: } else if (qname.equals("isStatic")) {
238: } // NOPMD
239:
240: else {
241: System.out.println("Method ? " + parentQname
242: + ": " + qname + "= " + value);
243: }
244: }
245: }
246:
247: @Override
248: public String toString() {
249: StringBuilder sb = new StringBuilder();
250: if (role.length() > 0) {
251: sb.append(role + ": Method ");
252: } else {
253: sb.append("In Method ");
254: }
255: sb.append(classname + "." + name + "( " + signature
256: + " )");
257:
258: if (sourceLine != null && sourceLine.startLine >= 0) {
259: sb.append(sourceLine.toString());
260: }
261: /* else
262: {
263: sb.append("\r\n\tat "+classname+"("+Field.extractJavaFileName(classname)+":-1)");
264: }*/
265:
266: return sb.toString();
267: }
268:
269: } // Method
270:
271: /** for example in Unused field
272: * also may contain a SourceLine, however contains -1 as line !
273: */
274: public static class Field extends FindBugsTag {
275: String name = "";
276: String classname = "";
277: String signature = "";
278: String isStatic = "";
279: String role = "";
280:
281: // Fields often have SourceLine, but always "-1". :-(
282:
283: public Field(Attributes attrs) {
284: readAttributes("Class", attrs);
285: }
286:
287: @Override
288: public void startElement(String namespaceURI, String lName, // local name
289: String qName, // qualified name
290: Attributes attrs) throws SAXException {
291: readAttributes(qName, attrs);
292: }
293:
294: private void readAttributes(String parentQname, Attributes attrs) {
295: for (int i = 0; i < attrs.getLength(); i++) {
296: String qname = attrs.getQName(i);
297: String value = attrs.getValue(qname);
298:
299: if (qname.equals("name"))
300: name = value;
301: else if (qname.equals("classname"))
302: classname = value;
303: else if (qname.equals("signature"))
304: signature = value;
305: else if (qname.equals("isStatic"))
306: isStatic = value;
307: else if (qname.equals("role"))
308: role = value;
309: else {
310: System.out.println("Field ? " + parentQname + ": "
311: + qname + "= " + value);
312: }
313: }
314: }
315:
316: @Override
317: public String toString() {
318: StringBuilder sb = new StringBuilder();
319: if (role.length() > 0)
320: sb.append(role + ": ");
321: if (isStatic.equals("true"))
322: sb.append("static ");
323: sb.append("Field " + name + " ( " + signature + " )"); //, static="+isStatic+" in class "+classname);
324:
325: // sb.append("\r\n\tat "+classname+"("+extractJavaFileName(classname)+":-1)");
326: return sb.toString();
327: }
328:
329: /** keep up to the last point and let $ (inner class fall)
330: */
331: public static String extractJavaFileName(String className) {
332: int pos = className.lastIndexOf('.');
333: if (pos >= 0)
334: className = className.substring(pos + 1);
335:
336: pos = className.indexOf('$');
337: if (pos >= 0)
338: className = className.substring(0, pos);
339:
340: return className + ".java";
341: }
342:
343: }
344:
345: /** Example
346: <SourceLine classname="SnowMailClient.FileEncryptor.SimpleFileEncryptor"
347: start="186" end="252"
348: startBytecode="0" endBytecode="364" sourcefile="SimpleFileEncryptor.java"/>
349:
350: present in Class, Method or BugInstance
351: */
352: public static class SourceLine extends FindBugsTag {
353: String className = "";
354: public String start = "1";
355: String end = "";
356: String startBytecode = "";
357: String endBytecode = "";
358: String sourcefile = "";
359: String role = "";
360: String sourcepath = "";
361: public int startLine = -1;
362:
363: // stored where it was collected
364: public FindBugsTag enclosingTag = null;
365:
366: public SourceLine(Attributes attrs) {
367: readAttributes("SourceLine", attrs);
368: }
369:
370: @Override
371: public void startElement(String namespaceURI, String lName, // local name
372: String qName, // qualified name
373: Attributes attrs) throws SAXException {
374: readAttributes(qName, attrs);
375: }
376:
377: private void readAttributes(String parentQname, Attributes attrs) {
378: for (int i = 0; i < attrs.getLength(); i++) {
379: String qname = attrs.getQName(i);
380: String value = attrs.getValue(qname);
381:
382: if (qname.equals("classname")) {
383: className = value;
384: } else if (qname.equals("sourcefile")) {
385: sourcefile = value;
386: } else if (qname.equals("start")) {
387: this .start = value;
388: try {
389: startLine = Integer.parseInt(value);
390: } catch (NumberFormatException nfe) {
391: nfe.printStackTrace();
392: }
393: } else if (qname.equals("end")) {
394: end = value;
395: } else if (qname.equals("startBytecode")) {
396: startBytecode = value;
397: } else if (qname.equals("endBytecode")) {
398: endBytecode = value;
399: } else if (qname.equals("role")) {
400: role = value;
401: } else if (qname.equals("sourcepath")) {
402: sourcepath = value;
403: }
404:
405: else if (qname.equals("opcodes")) {
406: } // NOPMD
407: else {
408: if (debug)
409: System.out.println("SourceLine ? "
410: + parentQname + ": " + qname + "= "
411: + value);
412: }
413: }
414: }
415:
416: @Override
417: public String toString() {
418: StringBuilder sb = new StringBuilder("");
419: if (role.length() > 0)
420: sb.append("\r\n" + role + ": ");
421:
422: sb.append("\r\n\tat " + className + "(" + sourcefile + ":"
423: + start + ")");
424: if (sourcefile.length() == 0 && sourcepath.length() > 0) {
425: sb.append("\r\nin " + sourcepath);
426: }
427:
428: return sb.toString();
429: }
430:
431: } // SourceLine
432:
433: }
|