2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2005-01-18 21:41:23 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2005-01-18 21:41:23 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2005-01-18 21:41:23 +03:00
|
|
|
#include "cmXMLParser.h"
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2005-01-18 21:41:23 +03:00
|
|
|
|
2006-10-19 23:00:10 +04:00
|
|
|
#include <cm_expat.h>
|
2005-01-18 23:54:20 +03:00
|
|
|
#include <ctype.h>
|
2005-01-18 21:41:23 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmXMLParser::cmXMLParser()
|
|
|
|
{
|
|
|
|
this->Parser = 0;
|
|
|
|
this->ParseError = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmXMLParser::~cmXMLParser()
|
|
|
|
{
|
|
|
|
if ( this->Parser )
|
|
|
|
{
|
|
|
|
this->CleanupParser();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmXMLParser::Parse(const char* string)
|
|
|
|
{
|
2006-03-30 22:49:56 +04:00
|
|
|
return (int)this->InitializeParser() &&
|
2012-08-13 21:42:58 +04:00
|
|
|
this->ParseChunk(string, strlen(string)) &&
|
2005-01-18 21:41:23 +03:00
|
|
|
this->CleanupParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmXMLParser::ParseFile(const char* file)
|
|
|
|
{
|
|
|
|
if ( !file )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream ifs(file);
|
2005-01-18 21:41:23 +03:00
|
|
|
if ( !ifs )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-18 22:02:39 +03:00
|
|
|
cmOStringStream str;
|
2005-01-18 21:41:23 +03:00
|
|
|
str << ifs.rdbuf();
|
|
|
|
return this->Parse(str.str().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmXMLParser::InitializeParser()
|
|
|
|
{
|
|
|
|
if ( this->Parser )
|
|
|
|
{
|
|
|
|
std::cerr << "Parser already initialized" << std::endl;
|
|
|
|
this->ParseError = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the expat XML parser.
|
|
|
|
this->Parser = XML_ParserCreate(0);
|
|
|
|
XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
|
|
|
|
&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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2012-08-13 21:42:58 +04:00
|
|
|
int cmXMLParser::ParseChunk(const char* inputString,
|
2007-07-25 17:22:19 +04:00
|
|
|
std::string::size_type length)
|
2005-01-18 21:41:23 +03:00
|
|
|
{
|
|
|
|
if ( !this->Parser )
|
|
|
|
{
|
|
|
|
std::cerr << "Parser not initialized" << std::endl;
|
|
|
|
this->ParseError = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int res;
|
|
|
|
res = this->ParseBuffer(inputString, length);
|
|
|
|
if ( res == 0 )
|
|
|
|
{
|
|
|
|
this->ParseError = 1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmXMLParser::CleanupParser()
|
|
|
|
{
|
|
|
|
if ( !this->Parser )
|
|
|
|
{
|
|
|
|
std::cerr << "Parser not initialized" << std::endl;
|
|
|
|
this->ParseError = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int result = !this->ParseError;
|
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
// Tell the expat XML parser about the end-of-input.
|
|
|
|
if(!XML_Parse(static_cast<XML_Parser>(this->Parser), "", 0, 1))
|
|
|
|
{
|
|
|
|
this->ReportXmlParseError();
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2005-01-18 21:41:23 +03:00
|
|
|
// Clean up the parser.
|
|
|
|
XML_ParserFree(static_cast<XML_Parser>(this->Parser));
|
|
|
|
this->Parser = 0;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2005-01-18 21:41:23 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2007-07-26 22:36:06 +04:00
|
|
|
int cmXMLParser::ParseBuffer(const char* buffer, std::string::size_type count)
|
2005-01-18 21:41:23 +03:00
|
|
|
{
|
|
|
|
// Pass the buffer to the expat XML parser.
|
2012-08-13 21:42:58 +04:00
|
|
|
if(!XML_Parse(static_cast<XML_Parser>(this->Parser), buffer,
|
2007-07-31 05:38:50 +04:00
|
|
|
static_cast<int>(count), 0))
|
2005-01-18 21:41:23 +03:00
|
|
|
{
|
|
|
|
this->ReportXmlParseError();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmXMLParser::StartElement(const char * name,
|
|
|
|
const char ** /*atts*/)
|
|
|
|
{
|
|
|
|
std::cout << "Start element: " << name << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmXMLParser::EndElement(const char * name)
|
|
|
|
{
|
|
|
|
std::cout << "End element: " << name << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmXMLParser::CharacterDataHandler(const char* /*inData*/,
|
|
|
|
int /*inLength*/)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
int cmXMLParser::IsSpace(char c)
|
|
|
|
{
|
|
|
|
return isspace(c);
|
|
|
|
}
|
|
|
|
|
2009-02-24 23:43:49 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* cmXMLParser::FindAttribute(const char** atts,
|
|
|
|
const char* attribute)
|
|
|
|
{
|
|
|
|
if(atts && attribute)
|
|
|
|
{
|
|
|
|
for(const char** a = atts; *a && *(a+1); a += 2)
|
|
|
|
{
|
|
|
|
if(strcmp(*a, attribute) == 0)
|
|
|
|
{
|
|
|
|
return *(a+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-18 21:41:23 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
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,
|
|
|
|
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-06-11 17:03:56 +04:00
|
|
|
XML_Parser parser = static_cast<XML_Parser>(this->Parser);
|
2009-02-24 23:43:37 +03:00
|
|
|
this->ReportError(XML_GetCurrentLineNumber(parser),
|
|
|
|
XML_GetCurrentColumnNumber(parser),
|
|
|
|
XML_ErrorString(XML_GetErrorCode(parser)));
|
2005-01-18 21:41:23 +03:00
|
|
|
}
|
|
|
|
|
2009-02-24 23:43:37 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmXMLParser::ReportError(int line, int, const char* msg)
|
|
|
|
{
|
|
|
|
std::cerr << "Error parsing XML in stream at line "
|
|
|
|
<< line << ": " << msg << std::endl;
|
|
|
|
}
|