cmParseCoberturaCoverage.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "cmStandardIncludes.h"
  2. #include "cmSystemTools.h"
  3. #include "cmXMLParser.h"
  4. #include "cmParseCoberturaCoverage.h"
  5. #include <cmsys/Directory.hxx>
  6. #include <cmsys/FStream.hxx>
  7. //----------------------------------------------------------------------------
  8. class cmParseCoberturaCoverage::XMLParser: public cmXMLParser
  9. {
  10. public:
  11. XMLParser(cmCTest* ctest, cmCTestCoverageHandlerContainer& cont)
  12. : CTest(ctest), Coverage(cont)
  13. {
  14. this->InSources = false;
  15. this->InSource = false;
  16. this->FilePaths.push_back(this->Coverage.SourceDir);
  17. this->CurFileName = "";
  18. }
  19. virtual ~XMLParser()
  20. {
  21. }
  22. protected:
  23. virtual void EndElement(const std::string& name)
  24. {
  25. if(name == "source")
  26. {
  27. this->InSource=false;
  28. }
  29. else if (name == "sources")
  30. {
  31. this->InSources=false;
  32. }
  33. }
  34. virtual void CharacterDataHandler(const char* data, int length)
  35. {
  36. std::string tmp;
  37. tmp.insert(0,data,length);
  38. if (this->InSources && this->InSource)
  39. {
  40. this->FilePaths.push_back(tmp);
  41. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Adding Source: "
  42. << tmp << std::endl);
  43. }
  44. }
  45. virtual void StartElement(const std::string& name, const char** atts)
  46. {
  47. std::string FoundSource;
  48. std::string finalpath = "";
  49. if(name == "source")
  50. {
  51. this->InSource = true;
  52. }
  53. else if(name == "sources")
  54. {
  55. this->InSources = true;
  56. }
  57. else if(name == "class")
  58. {
  59. int tagCount = 0;
  60. while(true)
  61. {
  62. if(strcmp(atts[tagCount], "filename") == 0)
  63. {
  64. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Reading file: "
  65. << atts[tagCount+1]<< std::endl);
  66. std::string filename = atts[tagCount+1];
  67. for(size_t i=0;i < FilePaths.size();i++)
  68. {
  69. finalpath = FilePaths[i] + "/" + filename;
  70. if(cmSystemTools::FileExists(finalpath.c_str()))
  71. {
  72. this->CurFileName = finalpath;
  73. break;
  74. }
  75. }
  76. cmsys::ifstream fin(this->CurFileName.c_str());
  77. if(this->CurFileName == "" || !fin )
  78. {
  79. this->CurFileName = this->Coverage.BinaryDir + "/" +
  80. atts[tagCount+1];
  81. fin.open(this->CurFileName.c_str());
  82. if (!fin)
  83. {
  84. cmCTestLog(this->CTest, ERROR_MESSAGE,
  85. "Python Coverage: Error opening " << this->CurFileName
  86. << std::endl);
  87. this->Coverage.Error++;
  88. break;
  89. }
  90. }
  91. std::string line;
  92. FileLinesType& curFileLines =
  93. this->Coverage.TotalCoverage[this->CurFileName];
  94. curFileLines.push_back(-1);
  95. while(cmSystemTools::GetLineFromStream(fin, line))
  96. {
  97. curFileLines.push_back(-1);
  98. }
  99. break;
  100. }
  101. ++tagCount;
  102. }
  103. }
  104. else if(name == "line")
  105. {
  106. int tagCount = 0;
  107. int curNumber = -1;
  108. int curHits = -1;
  109. while(true)
  110. {
  111. if(strcmp(atts[tagCount], "hits") == 0)
  112. {
  113. curHits = atoi(atts[tagCount+1]);
  114. }
  115. else if(strcmp(atts[tagCount], "number") == 0)
  116. {
  117. curNumber = atoi(atts[tagCount+1]);
  118. }
  119. if(curHits > -1 && curNumber > 0)
  120. {
  121. FileLinesType& curFileLines =
  122. this->Coverage.TotalCoverage[this->CurFileName];
  123. {
  124. curFileLines[curNumber-1] = curHits;
  125. }
  126. break;
  127. }
  128. ++tagCount;
  129. }
  130. }
  131. }
  132. private:
  133. bool InSources;
  134. bool InSource;
  135. std::vector<std::string> FilePaths;
  136. typedef cmCTestCoverageHandlerContainer::SingleFileCoverageVector
  137. FileLinesType;
  138. cmCTest* CTest;
  139. cmCTestCoverageHandlerContainer& Coverage;
  140. std::string CurFileName;
  141. };
  142. cmParseCoberturaCoverage::cmParseCoberturaCoverage(
  143. cmCTestCoverageHandlerContainer& cont,
  144. cmCTest* ctest)
  145. :Coverage(cont), CTest(ctest)
  146. {
  147. }
  148. bool cmParseCoberturaCoverage::ReadCoverageXML(const char* xmlFile)
  149. {
  150. cmParseCoberturaCoverage::XMLParser parser(this->CTest, this->Coverage);
  151. parser.ParseFile(xmlFile);
  152. return true;
  153. }