cmXMLParser.cxx 5.5 KB

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