001: package tide.exttools.findbugs;
002:
003: import java.util.*;
004: import java.util.jar.JarFile;
005: import java.util.zip.*;
006: import java.io.*;
007: import snow.utils.storage.FileUtils;
008:
009: import org.xml.sax.*;
010: import org.xml.sax.helpers.*;
011: import org.xml.sax.SAXException;
012: import javax.xml.parsers.SAXParser;
013: import javax.xml.parsers.SAXParserFactory;
014:
015: /** reads the messages from C:/Java/analysis_tools/findbugs/findbugs-1.2.0/plugin/coreplugin.jar
016: */
017: class FindBugsMessagesReader {
018: final Map<String, BugDescription> bugTypes = new HashMap<String, BugDescription>();
019: final Set<String> categories = new HashSet<String>();
020:
021: public FindBugsMessagesReader() throws Exception {
022: File f = new File(
023: "C:/Java/analysis_tools/findbugs/findbugs-1.2.0/plugin/coreplugin.jar");
024: JarFile jarFile = new JarFile(f);
025:
026: // information is distributed across several entries !
027: ZipEntry ze = jarFile.getEntry("messages.xml");
028: parse(jarFile, ze);
029: ze = jarFile.getEntry("findbugs.xml");
030: parse(jarFile, ze);
031:
032: System.out.println(categories);
033: System.out.println(bugTypes.values());
034: }
035:
036: public static void main(String[] arguments) throws Exception {
037: new FindBugsMessagesReader();
038: }
039:
040: void parse(JarFile jarFile, ZipEntry ze) throws Exception {
041: InputStream is = jarFile.getInputStream(ze);
042: String cont = FileUtils.getStringContent(is);
043: //System.out.println(""+cont.substring(0, 50000));
044: // Use the default (non-validating) parser
045: SAXParserFactory factory = SAXParserFactory.newInstance();
046: SAXParser saxParser = factory.newSAXParser();
047: //BufferedReader is = new BufferedReader(new StringReader(cont));
048: InputSource iso = new InputSource(new StringReader(cont));
049: BugTypesParser rp = new BugTypesParser();
050: saxParser.parse(iso, rp);
051: is.close();
052: }
053:
054: public class BugDescription {
055: String category;
056: String abbrev;
057: final String type;
058: String description;
059: boolean experimental;
060:
061: public BugDescription(String t) {
062: type = t;
063: }
064:
065: @Override
066: public final String toString() {
067: StringBuilder sb = new StringBuilder();
068: sb.append("\n" + type);
069: sb.append(": ");
070: sb.append(abbrev);
071: sb.append(": ");
072: sb.append(category);
073: sb.append(": ");
074: if (experimental)
075: sb.append("(EXPERIMENTAL)");
076: sb.append(description);
077:
078: return sb.toString();
079: }
080: }
081:
082: /** called once for messages.xml and once for findbugs.xml
083: */
084: class BugTypesParser extends DefaultHandler {
085:
086: boolean isInPattern = false;
087: StringBuilder lastChars = new StringBuilder();
088: BugDescription actualBugDescription = null;
089:
090: @Override
091: public void startDocument() {
092: }
093:
094: @Override
095: public void endDocument() {
096: }
097:
098: @Override
099: public void startElement(String namespaceURI, String sName,
100: String qName, Attributes attributes)
101: throws SAXException {
102: // reset
103: lastChars.setLength(0);
104: if (qName.equalsIgnoreCase("BugCategory")) {
105: categories.add(attributes.getValue("category"));
106: } else if (qName.equalsIgnoreCase("BugPattern")) {
107: String type = attributes.getValue("type");
108:
109: if (bugTypes.containsKey(type)) {
110: actualBugDescription = bugTypes.get(type);
111: } else {
112: actualBugDescription = new BugDescription(type);
113: bugTypes.put(actualBugDescription.type,
114: actualBugDescription);
115: }
116:
117: // found in findbugs.xml
118: if (attributes.getValue("abbrev") != null)
119: actualBugDescription.abbrev = attributes
120: .getValue("abbrev");
121: if (attributes.getValue("category") != null)
122: actualBugDescription.category = attributes
123: .getValue("category");
124: if (attributes.getValue("experimental") != null)
125: actualBugDescription.experimental = attributes
126: .getValue("experimental").equalsIgnoreCase(
127: "true");
128:
129: isInPattern = true;
130: lastChars.setLength(0);
131: }
132: }
133:
134: @Override
135: public void endElement(String namespaceURI, String sName, // simple name
136: String qName // qualified name
137: ) throws SAXException {
138: if (isInPattern) {
139: if (qName.equalsIgnoreCase("ShortDescription")) {
140: actualBugDescription.description = lastChars
141: .toString().trim();
142: lastChars.setLength(0);
143: }
144: }
145:
146: }
147:
148: @Override
149: public void characters(char[] buf, int offset, int len)
150: throws SAXException {
151: if (lastChars.length() > 0)
152: lastChars.append(" ");
153: lastChars.append(new String(buf, offset, len).trim());
154: }
155:
156: }
157: }
|