Skip to content

Commit

Permalink
add debug information for #14
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault_falque committed Aug 8, 2017
1 parent b6ddd7c commit 936b2d7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 89 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<!-- it's recommended to follow the pattern "sonar-{key}-plugin", for example
"sonar-php-plugin" -->
<artifactId>sonar-golang-plugin</artifactId>
<version>1.2.9-gamma</version>
<version>1.2.10-beta</version>


<name>Golang</name>
Expand Down
176 changes: 88 additions & 88 deletions src/main/java/fr/univartois/sonargo/coverage/CoverageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,105 +48,105 @@

public class CoverageParser implements Parser {

private final Map<String, List<LineCoverage>> coverageByFile = new HashMap<>();
private static final String FILE_NAME_ATTR = "filename";
private static final String LINE_NUMBER_ATTR = "number";
private static final String HITS_ATTR = "hits";
private static final String METHOD_TAG = "method";
private static final String CLASS_TAG = "class";
private static final String LINE_TAG = "line";
private static final Logger LOGGER = Loggers.get(CoverageParser.class);

private final boolean checkDtd;

public CoverageParser(SensorContext context) {
checkDtd = context.settings().getBoolean(GoProperties.DTD_VERIFICATION_KEY);
}

private DocumentBuilder constructDocumentBuilder() throws ParserConfigurationException {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

final DocumentBuilder db = dbf.newDocumentBuilder();

if (!checkDtd) {
db.setEntityResolver((publicId, systemId) -> {
if (systemId.contains("http://cobertura.sourceforge.net/xml/coverage-03.dtd")) {
return new InputSource(new StringReader(""));
} else {
return null;
}
});
private final Map<String, List<LineCoverage>> coverageByFile = new HashMap<>();
private static final String FILE_NAME_ATTR = "filename";
private static final String LINE_NUMBER_ATTR = "number";
private static final String HITS_ATTR = "hits";
private static final String METHOD_TAG = "method";
private static final String CLASS_TAG = "class";
private static final String LINE_TAG = "line";
private static final Logger LOGGER = Loggers.get(CoverageParser.class);

private final boolean checkDtd;

public CoverageParser(SensorContext context) {
checkDtd = context.settings().getBoolean(GoProperties.DTD_VERIFICATION_KEY);
}

return db;
}

/**
* {@link http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work}
*
* @param reportPath
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
@Override
public void parse(String reportPath) throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilder db = constructDocumentBuilder();
final Document doc = db.parse(new File(reportPath));
private DocumentBuilder constructDocumentBuilder() throws ParserConfigurationException {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

doc.getDocumentElement().normalize();
final DocumentBuilder db = dbf.newDocumentBuilder();

final NodeList classList = doc.getElementsByTagName(CLASS_TAG);
if (!checkDtd) {
db.setEntityResolver((publicId, systemId) -> {
if (systemId.contains("http://cobertura.sourceforge.net/xml/coverage-03.dtd")) {
return new InputSource(new StringReader(""));
} else {
return null;
}
});
}

for (int i = 0; i < classList.getLength(); i++) {
final Node nNode = classList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
return db;
}

final Element eElement = (Element) nNode;
final String filepath = eElement.getAttribute(FILE_NAME_ATTR);
/**
* {@link http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work}
*
* @param reportPath
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
@Override
public void parse(String reportPath) throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilder db = constructDocumentBuilder();
final Document doc = db.parse(new File(reportPath));

doc.getDocumentElement().normalize();

final NodeList classList = doc.getElementsByTagName(CLASS_TAG);

for (int i = 0; i < classList.getLength(); i++) {
final Node nNode = classList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {

final Element eElement = (Element) nNode;
final String filepath = eElement.getAttribute(FILE_NAME_ATTR);
LOGGER.debug("filepath from coverage file " + filepath);
parseMethodTag(eElement.getElementsByTagName(METHOD_TAG), getListForFile(filepath));
}
}

parseMethodTag(eElement.getElementsByTagName(METHOD_TAG), getListForFile(filepath));
}
}

}

public Map<String, List<LineCoverage>> getCoveragePerFile() {
for (Map.Entry<String, List<LineCoverage>> entry : coverageByFile.entrySet()) {
String fileName = entry.getKey();
List<LineCoverage> list = entry.getValue();
LOGGER.debug(list.size() + "line coverage for file " + fileName);
public Map<String, List<LineCoverage>> getCoveragePerFile() {
for (Map.Entry<String, List<LineCoverage>> entry : coverageByFile.entrySet()) {
String fileName = entry.getKey();
List<LineCoverage> list = entry.getValue();
LOGGER.debug(list.size() + "line coverage for file " + fileName);
}
return coverageByFile;
}
return coverageByFile;
}

private List<LineCoverage> getListForFile(String filepath) {
List<LineCoverage> list = coverageByFile.get(filepath);
if (list == null) {
list = new ArrayList<>();
coverageByFile.put(filepath, list);

private List<LineCoverage> getListForFile(String filepath) {
List<LineCoverage> list = coverageByFile.get(filepath);
if (list == null) {
list = new ArrayList<>();
coverageByFile.put(filepath, list);
}
return list;
}
return list;
}

private void parseMethodTag(NodeList methodsList, List<LineCoverage> listOfCoverage) {
for (int j = 0; j < methodsList.getLength(); j++) {
final Node nNode = methodsList.item(j);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;
parseLineTag(eElement.getElementsByTagName(LINE_TAG), listOfCoverage);
}

private void parseMethodTag(NodeList methodsList, List<LineCoverage> listOfCoverage) {
for (int j = 0; j < methodsList.getLength(); j++) {
final Node nNode = methodsList.item(j);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;
parseLineTag(eElement.getElementsByTagName(LINE_TAG), listOfCoverage);
}
}
}
}

private void parseLineTag(NodeList lineList, List<LineCoverage> listOfCoverage) {
for (int j = 0; j < lineList.getLength(); j++) {
final Node nNode = lineList.item(j);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;
listOfCoverage.add(new LineCoverage(Integer.parseInt(eElement.getAttribute(LINE_NUMBER_ATTR)),
Integer.parseInt(eElement.getAttribute(HITS_ATTR))));
}

private void parseLineTag(NodeList lineList, List<LineCoverage> listOfCoverage) {
for (int j = 0; j < lineList.getLength(); j++) {
final Node nNode = lineList.item(j);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
final Element eElement = (Element) nNode;
listOfCoverage.add(new LineCoverage(Integer.parseInt(eElement.getAttribute(LINE_NUMBER_ATTR)),
Integer.parseInt(eElement.getAttribute(HITS_ATTR))));
}
}
}
}
}

0 comments on commit 936b2d7

Please sign in to comment.