cmake/Source/cmXMLParser.cxx

205 lines
5.1 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmXMLParser.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <cctype>
#include <cstring>
2016-10-30 18:24:19 +01:00
#include <iostream>
#include <sstream>
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
#include <cm3p/expat.h>
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
#include "cmsys/FStream.hxx"
cmXMLParser::cmXMLParser()
{
2018-01-26 17:06:56 +01:00
this->Parser = nullptr;
this->ParseError = 0;
2018-01-26 17:06:56 +01:00
this->ReportCallback = nullptr;
this->ReportCallbackData = nullptr;
}
cmXMLParser::~cmXMLParser()
{
2016-07-09 11:21:54 +02:00
if (this->Parser) {
this->CleanupParser();
2016-07-09 11:21:54 +02:00
}
}
int cmXMLParser::Parse(const char* string)
{
2018-01-26 17:06:56 +01:00
return this->InitializeParser() &&
2016-07-09 11:21:54 +02:00
this->ParseChunk(string, strlen(string)) && this->CleanupParser();
}
int cmXMLParser::ParseFile(const char* file)
{
2016-07-09 11:21:54 +02:00
if (!file) {
return 0;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmsys::ifstream ifs(file);
2016-07-09 11:21:54 +02:00
if (!ifs) {
return 0;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream str;
str << ifs.rdbuf();
return this->Parse(str.str().c_str());
}
int cmXMLParser::InitializeParser()
{
2016-07-09 11:21:54 +02:00
if (this->Parser) {
std::cerr << "Parser already initialized" << std::endl;
this->ParseError = 1;
return 0;
2016-07-09 11:21:54 +02:00
}
// Create the expat XML parser.
2018-01-26 17:06:56 +01:00
this->Parser = XML_ParserCreate(nullptr);
XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
2016-07-09 11:21:54 +02:00
&cmXMLParserStartElement, &cmXMLParserEndElement);
XML_SetCharacterDataHandler(static_cast<XML_Parser>(this->Parser),
&cmXMLParserCharacterDataHandler);
XML_SetUserData(static_cast<XML_Parser>(this->Parser), this);
this->ParseError = 0;
return 1;
}
2013-03-16 19:13:01 +02:00
int cmXMLParser::ParseChunk(const char* inputString,
std::string::size_type length)
{
2016-07-09 11:21:54 +02:00
if (!this->Parser) {
std::cerr << "Parser not initialized" << std::endl;
this->ParseError = 1;
return 0;
2016-07-09 11:21:54 +02:00
}
int res;
res = this->ParseBuffer(inputString, length);
2016-07-09 11:21:54 +02:00
if (res == 0) {
this->ParseError = 1;
2016-07-09 11:21:54 +02:00
}
return res;
}
int cmXMLParser::CleanupParser()
{
2016-07-09 11:21:54 +02:00
if (!this->Parser) {
std::cerr << "Parser not initialized" << std::endl;
this->ParseError = 1;
return 0;
2016-07-09 11:21:54 +02:00
}
int result = !this->ParseError;
2016-07-09 11:21:54 +02:00
if (result) {
// Tell the expat XML parser about the end-of-input.
2016-07-09 11:21:54 +02:00
if (!XML_Parse(static_cast<XML_Parser>(this->Parser), "", 0, 1)) {
this->ReportXmlParseError();
result = 0;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// Clean up the parser.
XML_ParserFree(static_cast<XML_Parser>(this->Parser));
2018-01-26 17:06:56 +01:00
this->Parser = nullptr;
2013-03-16 19:13:01 +02:00
return result;
}
int cmXMLParser::ParseBuffer(const char* buffer, std::string::size_type count)
{
// Pass the buffer to the expat XML parser.
2016-07-09 11:21:54 +02:00
if (!XML_Parse(static_cast<XML_Parser>(this->Parser), buffer,
static_cast<int>(count), 0)) {
this->ReportXmlParseError();
return 0;
2016-07-09 11:21:54 +02:00
}
return 1;
}
int cmXMLParser::ParseBuffer(const char* buffer)
{
return this->ParseBuffer(buffer, static_cast<int>(strlen(buffer)));
}
int cmXMLParser::ParsingComplete()
{
// Default behavior is to parse to end of stream.
return 0;
}
2016-07-09 11:21:54 +02:00
void cmXMLParser::StartElement(const std::string& name, const char** /*atts*/)
{
std::cout << "Start element: " << name << std::endl;
}
2015-04-27 22:25:09 +02:00
void cmXMLParser::EndElement(const std::string& name)
{
std::cout << "End element: " << name << std::endl;
}
void cmXMLParser::CharacterDataHandler(const char* /*inData*/,
2016-07-09 11:21:54 +02:00
int /*inLength*/)
{
}
int cmXMLParser::IsSpace(char c)
{
return isspace(c);
}
2009-10-04 10:30:41 +03:00
const char* cmXMLParser::FindAttribute(const char** atts,
const char* attribute)
{
2016-07-09 11:21:54 +02:00
if (atts && attribute) {
for (const char** a = atts; *a && *(a + 1); a += 2) {
if (strcmp(*a, attribute) == 0) {
return *(a + 1);
2009-10-04 10:30:41 +03:00
}
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
return nullptr;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
void cmXMLParserStartElement(void* parser, const char* name, const char** atts)
{
// Begin element handler that is registered with the XML_Parser.
// This just casts the user data to a cmXMLParser and calls
// StartElement.
static_cast<cmXMLParser*>(parser)->StartElement(name, atts);
}
2016-07-09 11:21:54 +02:00
void cmXMLParserEndElement(void* parser, const char* name)
{
// End element handler that is registered with the XML_Parser. This
// just casts the user data to a cmXMLParser and calls EndElement.
static_cast<cmXMLParser*>(parser)->EndElement(name);
}
void cmXMLParserCharacterDataHandler(void* parser, const char* data,
2016-07-09 11:21:54 +02:00
int length)
{
// Character data handler that is registered with the XML_Parser.
// This just casts the user data to a cmXMLParser and calls
// CharacterDataHandler.
static_cast<cmXMLParser*>(parser)->CharacterDataHandler(data, length);
}
void cmXMLParser::ReportXmlParseError()
{
2009-10-04 10:30:41 +03:00
XML_Parser parser = static_cast<XML_Parser>(this->Parser);
2018-10-28 12:09:07 +01:00
this->ReportError(static_cast<int>(XML_GetCurrentLineNumber(parser)),
static_cast<int>(XML_GetCurrentColumnNumber(parser)),
2009-10-04 10:30:41 +03:00
XML_ErrorString(XML_GetErrorCode(parser)));
}
2016-10-30 18:24:19 +01:00
void cmXMLParser::ReportError(int line, int /*unused*/, const char* msg)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (this->ReportCallback) {
2015-04-27 22:25:09 +02:00
this->ReportCallback(line, msg, this->ReportCallbackData);
2016-07-09 11:21:54 +02:00
} else {
std::cerr << "Error parsing XML in stream at line " << line << ": " << msg
<< std::endl;
}
2009-10-04 10:30:41 +03:00
}