input, and that the result passes some very basic sanity checks.
// Copyright (C) 2015 Christian Jaekl
-import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
+import net.jaekl.cfb.xml.BugCollection;
+import net.jaekl.qd.xml.ParseErrorHandler;
+import net.jaekl.qd.xml.ParseHandler;
+
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
-import net.jaekl.cfb.xml.BugCollection;
-import net.jaekl.qd.util.InputStreamWrapper;
-import net.jaekl.qd.xml.ParseErrorHandler;
-import net.jaekl.qd.xml.ParseHandler;
-
public class Analysis {
BugCollection m_bugCollection;
public BugCollection getBugCollection() { return m_bugCollection; }
- public void parse(File xml) throws FileNotFoundException, IOException, SAXException
+ public void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException
{
m_bugCollection = new BugCollection();
- try (InputStreamWrapper isw = new InputStreamWrapper(new FileInputStream(xml)))
- {
- XMLReader reader = XMLReaderFactory.createXMLReader();
- ParseHandler ph = new ParseHandler(m_bugCollection);
- ParseErrorHandler peh = new ParseErrorHandler();
- reader.setContentHandler(ph);
- reader.setErrorHandler(peh);
- reader.parse(new InputSource(isw));
- }
+ XMLReader reader = XMLReaderFactory.createXMLReader();
+ ParseHandler ph = new ParseHandler(m_bugCollection);
+ ParseErrorHandler peh = new ParseErrorHandler();
+ reader.setContentHandler(ph);
+ reader.setErrorHandler(peh);
+ reader.parse(xml);
}
public void dump(PrintWriter pw)
import java.util.Locale;
import java.util.Locale.Category;
+import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import net.jaekl.cfb.CfbBundle;
return null;
}
- result = parseFbOutput(fbOutput);
+ result = parseFbOutput(new InputSource(fbOutput.getAbsolutePath()));
result.dump(pw);
return result;
}
// Parse the output.xml that resulted from a FindBugs run,
// and store its findings into an Analysis object.
- Analysis parseFbOutput(File fbOutput) throws XmlParseException
+ Analysis parseFbOutput(InputSource fbOutput) throws XmlParseException
{
Analysis result = new Analysis();
try {
m_bugs = new ArrayList<BugInstance>();
}
+ public int size() { return m_bugs.size(); }
+ public BugInstance get(int idx) { return m_bugs.get(idx); }
+
@Override
public void endContents(String uri, String localName, String qName, String chars)
throws XmlParseException
m_lines = new ArrayList<SourceLine>();
}
+ public String getType() { return m_type; }
+
@Override
public void endContents(String uri, String localName, String qName, String chars)
throws XmlParseException
m_className = getRequiredAttr(TAG, attr, ATTR_CLASS_NAME);
- scratch = getRequiredAttr(TAG, attr, ATTR_START);
+ scratch = getOptionalAttr(attr, ATTR_START, "-1");
m_start = Integer.parseInt(scratch);
- scratch = getRequiredAttr(TAG, attr, ATTR_END);
+ scratch = getOptionalAttr(attr, ATTR_END, "-1");
m_end = Integer.parseInt(scratch);
}
import static org.junit.Assert.*;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.HashMap;
+
+import net.jaekl.cfb.xml.BugCollection;
+import net.jaekl.cfb.xml.BugInstance;
+import net.jaekl.qd.xml.XmlParseException;
import org.junit.Test;
+import org.xml.sax.InputSource;
public class AnalyzerTest {
+ private static final String UTF8 = "utf-8";
+
+ private static final String SAMPLE1_XML = ""
+ + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ + "<BugCollection version=\"3.0.1\" sequence=\"0\" timestamp=\"1440757060000\" analysisTimestamp=\"1440761315847\" release=\"\">\n"
+ + "<Project projectName=\"CFB\">\n"
+ + "<Jar>/home/chris/prog/././cfb/bin</Jar>\n"
+ + "<SrcDir>/home/chris/prog/././cfb/src</SrcDir>\n"
+ + "</Project>\n"
+ + "<BugInstance type=\"DM_DEFAULT_ENCODING\" priority=\"1\" rank=\"19\" abbrev=\"Dm\" category=\"I18N\">\n"
+ + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+ + "</Class>\n"
+ + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"main\" signature=\"([Ljava/lang/String;)V\" isStatic=\"true\">\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"112\" end=\"119\" startBytecode=\"0\" endBytecode=\"266\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+ + "</Method>\n"
+ + "<Method classname=\"java.io.PrintWriter\" name=\"<init>\" signature=\"(Ljava/io/OutputStream;)V\" isStatic=\"false\" role=\"METHOD_CALLED\">\n"
+ + "<SourceLine classname=\"java.io.PrintWriter\" start=\"131\" end=\"132\" startBytecode=\"0\" endBytecode=\"62\" sourcefile=\"PrintWriter.java\" sourcepath=\"java/io/PrintWriter.java\"/>\n"
+ + "</Method>\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"114\" end=\"114\" startBytecode=\"22\" endBytecode=\"22\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+ + "</BugInstance>\n"
+ + "<BugInstance type=\"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE\" priority=\"2\" rank=\"18\" abbrev=\"RCN\" category=\"STYLE\">\n"
+ + "<Class classname=\"net.jaekl.cfb.CFB\">\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"32\" end=\"119\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+ + "</Class>\n"
+ + "<Method classname=\"net.jaekl.cfb.CFB\" name=\"doMain\" signature=\"(Ljava/io/PrintWriter;[Ljava/lang/String;)V\" isStatic=\"false\">\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"96\" end=\"109\" startBytecode=\"0\" endBytecode=\"407\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\"/>\n"
+ + "</Method>\n"
+ + "<LocalVariable name=\"con\" register=\"5\" pc=\"44\" role=\"LOCAL_VARIABLE_VALUE_OF\"/>\n"
+ + "<Method classname=\"net.jaekl.cfb.db.driver.DbDriver\" name=\"connect\" signature=\"(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection;\" isStatic=\"false\" role=\"METHOD_RETURN_VALUE_OF\">\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.db.driver.DbDriver\" sourcefile=\"DbDriver.java\" sourcepath=\"net/jaekl/cfb/db/driver/DbDriver.java\"/>\n"
+ + "</Method>\n"
+ + "<SourceLine classname=\"net.jaekl.cfb.CFB\" start=\"101\" end=\"101\" startBytecode=\"46\" endBytecode=\"46\" sourcefile=\"CFB.java\" sourcepath=\"net/jaekl/cfb/CFB.java\" role=\"SOURCE_REDUNDANT_NULL_CHECK\"/>\n"
+ + "</BugInstance>\n"
+ + "</BugCollection>\n";
@Test
public void testOutputWorkFile() {
}
}
+ @Test
+ public void testParseSample1() throws IOException, XmlParseException {
+ Charset utf8 = Charset.forName(UTF8);
+ BugInstance inst = null;
+
+ try ( ByteArrayInputStream bais = new ByteArrayInputStream(SAMPLE1_XML.getBytes(utf8)))
+ {
+ InputSource inputSource = new InputSource(bais);
+ Analyzer analyzer = new Analyzer(new File("."));
+ Analysis analysis = analyzer.parseFbOutput(inputSource);
+
+ assertNotNull(analysis);
+
+ BugCollection bugColl = analysis.getBugCollection();
+
+ assertNotNull(bugColl);
+ assertEquals(2, bugColl.size());
+
+ HashMap<String, BugInstance> typeMap = new HashMap<String, BugInstance>();
+ for (int idx = 0; idx < bugColl.size(); ++idx) {
+ inst = bugColl.get(idx);
+ typeMap.put(inst.getType(), inst);
+ }
+
+ inst = typeMap.get("DM_DEFAULT_ENCODING");
+ assertNotNull(inst);
+
+ inst = typeMap.get("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE");
+ assertNotNull(inst);
+ }
+ }
}