package parsers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import database.MySQLConnection;
/**
*
* The parser for parsing the Findbugs reports
*
*/
public class FindbugsParser extends AbstractParser {
private ArrayList<InsertList> insertList = new ArrayList<InsertList>();
private class InsertList {
public String category = "";
public String abbrev = "";
public String type = "";
public String file = "";
public String line = "";
public InsertList(String c, String a, String t, String f, String l) {
category = c;
abbrev = a;
type = t;
file = f;
line = l;
}
public String toString() {
return abbrev + " " + category + " " + type + " " + file + " "
+ line;
}
}
private int getChild(String s, Node subNode) {
NodeList l = subNode.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
if (l.item(i).getNodeName().equalsIgnoreCase(s))
return i;
}
return -1;
}
public void readResults(String path) throws ParserConfigurationException,
SAXException, IOException {
insertList.clear();
parseResults(path);
NodeList bugList = root.getElementsByTagName("BugInstance");
for (int i = 0; i < bugList.getLength(); i++) {
Element courant = (Element) bugList.item(i);
String category = courant.getAttribute("category");
String abbrev = courant.getAttribute("abbrev");
String type = courant.getAttribute("type");
String file;
String line;
if ((abbrev.equalsIgnoreCase("Nm")
|| abbrev.equalsIgnoreCase("SBSC")
|| abbrev.equalsIgnoreCase("DE")
|| abbrev.equalsIgnoreCase("NP")
|| abbrev.equalsIgnoreCase("BC")
|| abbrev.equalsIgnoreCase("Dm")
|| abbrev.equalsIgnoreCase("WMI")
|| abbrev.equalsIgnoreCase("Bx")
|| abbrev.equalsIgnoreCase("HSC")
|| abbrev.equalsIgnoreCase("ITA")
|| abbrev.equalsIgnoreCase("SIC")
|| abbrev.equalsIgnoreCase("CN")
|| abbrev.equalsIgnoreCase("DMI")
|| abbrev.equalsIgnoreCase("ES")
|| abbrev.equalsIgnoreCase("Eq")
|| abbrev.equalsIgnoreCase("HE")
|| abbrev.equalsIgnoreCase("IC")
|| abbrev.equalsIgnoreCase("ISC")
|| abbrev.equalsIgnoreCase("It")
|| abbrev.equalsIgnoreCase("J2EE")
|| abbrev.equalsIgnoreCase("ODR")
|| abbrev.equalsIgnoreCase("RR")
|| abbrev.equalsIgnoreCase("RV")
|| abbrev.equalsIgnoreCase("SI") || abbrev
.equalsIgnoreCase("Se"))
&& (category.equalsIgnoreCase("BAD_PRACTICE") || category
.equalsIgnoreCase("PERFORMANCE"))) {
NodeList subList = courant.getChildNodes();
Node subNode = subList.item(subList.getLength() - 2);
if (subNode.getNodeName().equalsIgnoreCase("Field")) {
subNode = subList.item(1).getChildNodes().item(1);
}
else if (subNode.getNodeName().equalsIgnoreCase("Property")) {
subNode = (Element) subNode.getChildNodes().item(
getChild("SourceLine", subNode));
} else if (getChild("SourceLine", subNode) != -1) {
subNode = (Element) subNode.getChildNodes().item(
getChild("SourceLine", subNode));
}
file = ((Element) subNode).getAttribute("sourcefile");
line = ((Element) subNode).getAttribute("start");
insertList.add(new InsertList(category, abbrev, type, file,
line));
}
}
}
public void printResults(int idVersion) {
Iterator<InsertList> i = insertList.iterator();
while (i.hasNext()) {
InsertList e = i.next();
System.out.println(e.category + "','" + e.abbrev + "','" + e.type
+ "','" + e.file + "'," + e.line);
}
}
public void saveResults(int idVersion) throws Exception {
MySQLConnection conn = new MySQLConnection();
conn.connect();
Iterator<InsertList> i = insertList.iterator();
while (i.hasNext()) {
InsertList e = i.next();
// Mandatory info on the error has been successfully retrieved
if (!e.abbrev.isEmpty() && !e.category.isEmpty()
&& !e.type.isEmpty()) {
conn.SQLUpdate("insert into findbugs_stats (version , category , abbrev , type , file , line) values ("
+ idVersion
+ ",'"
+ e.category
+ "',"
+ "'"
+ e.abbrev
+ "',"
+ "'"
+ e.type
+ "',"
+ (e.file.equals("") ? "NULL," : "'" + e.file + "',")
+ (e.line.equals("") ? "NULL" : e.line) + ")");
}
// Couldn't retrieve enough info on the error
else {
System.err
.println("Findbugs error skipped because of lack of info");
}
}
conn.disconnect();
}
}