cmXMLParser.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <ctype.h>
  13. //----------------------------------------------------------------------------
  14. cmXMLParser::cmXMLParser()
  15. {
  16. this->Parser = 0;
  17. this->ParseError = 0;
  18. }
  19. //----------------------------------------------------------------------------
  20. cmXMLParser::~cmXMLParser()
  21. {
  22. if ( this->Parser )
  23. {
  24. this->CleanupParser();
  25. }
  26. }
  27. //----------------------------------------------------------------------------
  28. int cmXMLParser::Parse(const char* string)
  29. {
  30. return (int)this->InitializeParser() &&
  31. this->ParseChunk(string, strlen(string)) &&
  32. this->CleanupParser();
  33. }
  34. int cmXMLParser::ParseFile(const char* file)
  35. {
  36. if ( !file )
  37. {
  38. return 0;
  39. }
  40. std::ifstream ifs(file);
  41. if ( !ifs )
  42. {
  43. return 0;
  44. }
  45. cmOStringStream str;
  46. str << ifs.rdbuf();
  47. return this->Parse(str.str().c_str());
  48. }
  49. //----------------------------------------------------------------------------
  50. int cmXMLParser::InitializeParser()
  51. {
  52. if ( this->Parser )
  53. {
  54. std::cerr << "Parser already initialized" << std::endl;
  55. this->ParseError = 1;
  56. return 0;
  57. }
  58. // Create the expat XML parser.
  59. this->Parser = XML_ParserCreate(0);
  60. XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
  61. &cmXMLParserStartElement,
  62. &cmXMLParserEndElement);
  63. XML_SetCharacterDataHandler(static_cast<XML_Parser>(this->Parser),
  64. &cmXMLParserCharacterDataHandler);
  65. XML_SetUserData(static_cast<XML_Parser>(this->Parser), this);
  66. this->ParseError = 0;
  67. return 1;
  68. }
  69. //----------------------------------------------------------------------------
  70. int cmXMLParser::ParseChunk(const char* inputString,
  71. std::string::size_type length)
  72. {
  73. if ( !this->Parser )
  74. {
  75. std::cerr << "Parser not initialized" << std::endl;
  76. this->ParseError = 1;
  77. return 0;
  78. }
  79. int res;
  80. res = this->ParseBuffer(inputString, length);
  81. if ( res == 0 )
  82. {
  83. this->ParseError = 1;
  84. }
  85. return res;
  86. }
  87. //----------------------------------------------------------------------------
  88. int cmXMLParser::CleanupParser()
  89. {
  90. if ( !this->Parser )
  91. {
  92. std::cerr << "Parser not initialized" << std::endl;
  93. this->ParseError = 1;
  94. return 0;
  95. }
  96. int result = !this->ParseError;
  97. if(result)
  98. {
  99. // Tell the expat XML parser about the end-of-input.
  100. if(!XML_Parse(static_cast<XML_Parser>(this->Parser), "", 0, 1))
  101. {
  102. this->ReportXmlParseError();
  103. result = 0;
  104. }
  105. }
  106. // Clean up the parser.
  107. XML_ParserFree(static_cast<XML_Parser>(this->Parser));
  108. this->Parser = 0;
  109. return result;
  110. }
  111. //----------------------------------------------------------------------------
  112. int cmXMLParser::ParseBuffer(const char* buffer, std::string::size_type count)
  113. {
  114. // Pass the buffer to the expat XML parser.
  115. if(!XML_Parse(static_cast<XML_Parser>(this->Parser), buffer,
  116. static_cast<int>(count), 0))
  117. {
  118. this->ReportXmlParseError();
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. //----------------------------------------------------------------------------
  124. int cmXMLParser::ParseBuffer(const char* buffer)
  125. {
  126. return this->ParseBuffer(buffer, static_cast<int>(strlen(buffer)));
  127. }
  128. //----------------------------------------------------------------------------
  129. int cmXMLParser::ParsingComplete()
  130. {
  131. // Default behavior is to parse to end of stream.
  132. return 0;
  133. }
  134. //----------------------------------------------------------------------------
  135. void cmXMLParser::StartElement(const char * name,
  136. const char ** /*atts*/)
  137. {
  138. std::cout << "Start element: " << name << std::endl;
  139. }
  140. //----------------------------------------------------------------------------
  141. void cmXMLParser::EndElement(const char * name)
  142. {
  143. std::cout << "End element: " << name << std::endl;
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmXMLParser::CharacterDataHandler(const char* /*inData*/,
  147. int /*inLength*/)
  148. {
  149. }
  150. //----------------------------------------------------------------------------
  151. int cmXMLParser::IsSpace(char c)
  152. {
  153. return isspace(c);
  154. }
  155. //----------------------------------------------------------------------------
  156. const char* cmXMLParser::FindAttribute(const char** atts,
  157. const char* attribute)
  158. {
  159. if(atts && attribute)
  160. {
  161. for(const char** a = atts; *a && *(a+1); a += 2)
  162. {
  163. if(strcmp(*a, attribute) == 0)
  164. {
  165. return *(a+1);
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. //----------------------------------------------------------------------------
  172. void cmXMLParserStartElement(void* parser, const char *name,
  173. const char **atts)
  174. {
  175. // Begin element handler that is registered with the XML_Parser.
  176. // This just casts the user data to a cmXMLParser and calls
  177. // StartElement.
  178. static_cast<cmXMLParser*>(parser)->StartElement(name, atts);
  179. }
  180. //----------------------------------------------------------------------------
  181. void cmXMLParserEndElement(void* parser, const char *name)
  182. {
  183. // End element handler that is registered with the XML_Parser. This
  184. // just casts the user data to a cmXMLParser and calls EndElement.
  185. static_cast<cmXMLParser*>(parser)->EndElement(name);
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmXMLParserCharacterDataHandler(void* parser, const char* data,
  189. int length)
  190. {
  191. // Character data handler that is registered with the XML_Parser.
  192. // This just casts the user data to a cmXMLParser and calls
  193. // CharacterDataHandler.
  194. static_cast<cmXMLParser*>(parser)->CharacterDataHandler(data, length);
  195. }
  196. //----------------------------------------------------------------------------
  197. void cmXMLParser::ReportXmlParseError()
  198. {
  199. XML_Parser parser = static_cast<XML_Parser>(this->Parser);
  200. this->ReportError(XML_GetCurrentLineNumber(parser),
  201. XML_GetCurrentColumnNumber(parser),
  202. XML_ErrorString(XML_GetErrorCode(parser)));
  203. }
  204. //----------------------------------------------------------------------------
  205. void cmXMLParser::ReportError(int line, int, const char* msg)
  206. {
  207. std::cerr << "Error parsing XML in stream at line "
  208. << line << ": " << msg << std::endl;
  209. }