cmXMLParser.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmXMLParser.h"
  4. #include "cm_expat.h"
  5. #include "cmsys/FStream.hxx"
  6. #include <ctype.h>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <string.h>
  10. cmXMLParser::cmXMLParser()
  11. {
  12. this->Parser = nullptr;
  13. this->ParseError = 0;
  14. this->ReportCallback = nullptr;
  15. this->ReportCallbackData = nullptr;
  16. }
  17. cmXMLParser::~cmXMLParser()
  18. {
  19. if (this->Parser) {
  20. this->CleanupParser();
  21. }
  22. }
  23. int cmXMLParser::Parse(const char* string)
  24. {
  25. return this->InitializeParser() &&
  26. this->ParseChunk(string, strlen(string)) && this->CleanupParser();
  27. }
  28. int cmXMLParser::ParseFile(const char* file)
  29. {
  30. if (!file) {
  31. return 0;
  32. }
  33. cmsys::ifstream ifs(file);
  34. if (!ifs) {
  35. return 0;
  36. }
  37. std::ostringstream str;
  38. str << ifs.rdbuf();
  39. return this->Parse(str.str().c_str());
  40. }
  41. int cmXMLParser::InitializeParser()
  42. {
  43. if (this->Parser) {
  44. std::cerr << "Parser already initialized" << std::endl;
  45. this->ParseError = 1;
  46. return 0;
  47. }
  48. // Create the expat XML parser.
  49. this->Parser = XML_ParserCreate(nullptr);
  50. XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
  51. &cmXMLParserStartElement, &cmXMLParserEndElement);
  52. XML_SetCharacterDataHandler(static_cast<XML_Parser>(this->Parser),
  53. &cmXMLParserCharacterDataHandler);
  54. XML_SetUserData(static_cast<XML_Parser>(this->Parser), this);
  55. this->ParseError = 0;
  56. return 1;
  57. }
  58. int cmXMLParser::ParseChunk(const char* inputString,
  59. std::string::size_type length)
  60. {
  61. if (!this->Parser) {
  62. std::cerr << "Parser not initialized" << std::endl;
  63. this->ParseError = 1;
  64. return 0;
  65. }
  66. int res;
  67. res = this->ParseBuffer(inputString, length);
  68. if (res == 0) {
  69. this->ParseError = 1;
  70. }
  71. return res;
  72. }
  73. int cmXMLParser::CleanupParser()
  74. {
  75. if (!this->Parser) {
  76. std::cerr << "Parser not initialized" << std::endl;
  77. this->ParseError = 1;
  78. return 0;
  79. }
  80. int result = !this->ParseError;
  81. if (result) {
  82. // Tell the expat XML parser about the end-of-input.
  83. if (!XML_Parse(static_cast<XML_Parser>(this->Parser), "", 0, 1)) {
  84. this->ReportXmlParseError();
  85. result = 0;
  86. }
  87. }
  88. // Clean up the parser.
  89. XML_ParserFree(static_cast<XML_Parser>(this->Parser));
  90. this->Parser = nullptr;
  91. return result;
  92. }
  93. int cmXMLParser::ParseBuffer(const char* buffer, std::string::size_type count)
  94. {
  95. // Pass the buffer to the expat XML parser.
  96. if (!XML_Parse(static_cast<XML_Parser>(this->Parser), buffer,
  97. static_cast<int>(count), 0)) {
  98. this->ReportXmlParseError();
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. int cmXMLParser::ParseBuffer(const char* buffer)
  104. {
  105. return this->ParseBuffer(buffer, static_cast<int>(strlen(buffer)));
  106. }
  107. int cmXMLParser::ParsingComplete()
  108. {
  109. // Default behavior is to parse to end of stream.
  110. return 0;
  111. }
  112. void cmXMLParser::StartElement(const std::string& name, const char** /*atts*/)
  113. {
  114. std::cout << "Start element: " << name << std::endl;
  115. }
  116. void cmXMLParser::EndElement(const std::string& name)
  117. {
  118. std::cout << "End element: " << name << std::endl;
  119. }
  120. void cmXMLParser::CharacterDataHandler(const char* /*inData*/,
  121. int /*inLength*/)
  122. {
  123. }
  124. int cmXMLParser::IsSpace(char c)
  125. {
  126. return isspace(c);
  127. }
  128. const char* cmXMLParser::FindAttribute(const char** atts,
  129. const char* attribute)
  130. {
  131. if (atts && attribute) {
  132. for (const char** a = atts; *a && *(a + 1); a += 2) {
  133. if (strcmp(*a, attribute) == 0) {
  134. return *(a + 1);
  135. }
  136. }
  137. }
  138. return nullptr;
  139. }
  140. void cmXMLParserStartElement(void* parser, const char* name, const char** atts)
  141. {
  142. // Begin element handler that is registered with the XML_Parser.
  143. // This just casts the user data to a cmXMLParser and calls
  144. // StartElement.
  145. static_cast<cmXMLParser*>(parser)->StartElement(name, atts);
  146. }
  147. void cmXMLParserEndElement(void* parser, const char* name)
  148. {
  149. // End element handler that is registered with the XML_Parser. This
  150. // just casts the user data to a cmXMLParser and calls EndElement.
  151. static_cast<cmXMLParser*>(parser)->EndElement(name);
  152. }
  153. void cmXMLParserCharacterDataHandler(void* parser, const char* data,
  154. int length)
  155. {
  156. // Character data handler that is registered with the XML_Parser.
  157. // This just casts the user data to a cmXMLParser and calls
  158. // CharacterDataHandler.
  159. static_cast<cmXMLParser*>(parser)->CharacterDataHandler(data, length);
  160. }
  161. void cmXMLParser::ReportXmlParseError()
  162. {
  163. XML_Parser parser = static_cast<XML_Parser>(this->Parser);
  164. this->ReportError(XML_GetCurrentLineNumber(parser),
  165. XML_GetCurrentColumnNumber(parser),
  166. XML_ErrorString(XML_GetErrorCode(parser)));
  167. }
  168. void cmXMLParser::ReportError(int line, int /*unused*/, const char* msg)
  169. {
  170. if (this->ReportCallback) {
  171. this->ReportCallback(line, msg, this->ReportCallbackData);
  172. } else {
  173. std::cerr << "Error parsing XML in stream at line " << line << ": " << msg
  174. << std::endl;
  175. }
  176. }