Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmXMLParser.cxx
blob31c9e7eefa131a42e66ca2fea63776a5d1e2a671
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmXMLParser.cxx,v $
5 Language: C++
6 Date: $Date: 2007/07/31 01:38:50 $
7 Version: $Revision: 1.8 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmXMLParser.h"
19 #include <cm_expat.h>
20 #include <ctype.h>
22 //----------------------------------------------------------------------------
23 cmXMLParser::cmXMLParser()
25 this->Parser = 0;
26 this->ParseError = 0;
29 //----------------------------------------------------------------------------
30 cmXMLParser::~cmXMLParser()
32 if ( this->Parser )
34 this->CleanupParser();
38 //----------------------------------------------------------------------------
39 int cmXMLParser::Parse(const char* string)
41 return (int)this->InitializeParser() &&
42 this->ParseChunk(string, strlen(string)) &&
43 this->CleanupParser();
46 int cmXMLParser::ParseFile(const char* file)
48 if ( !file )
50 return 0;
53 std::ifstream ifs(file);
54 if ( !ifs )
56 return 0;
59 cmOStringStream str;
60 str << ifs.rdbuf();
61 return this->Parse(str.str().c_str());
64 //----------------------------------------------------------------------------
65 int cmXMLParser::InitializeParser()
67 if ( this->Parser )
69 std::cerr << "Parser already initialized" << std::endl;
70 this->ParseError = 1;
71 return 0;
74 // Create the expat XML parser.
75 this->Parser = XML_ParserCreate(0);
76 XML_SetElementHandler(static_cast<XML_Parser>(this->Parser),
77 &cmXMLParserStartElement,
78 &cmXMLParserEndElement);
79 XML_SetCharacterDataHandler(static_cast<XML_Parser>(this->Parser),
80 &cmXMLParserCharacterDataHandler);
81 XML_SetUserData(static_cast<XML_Parser>(this->Parser), this);
82 this->ParseError = 0;
83 return 1;
86 //----------------------------------------------------------------------------
87 int cmXMLParser::ParseChunk(const char* inputString,
88 std::string::size_type length)
90 if ( !this->Parser )
92 std::cerr << "Parser not initialized" << std::endl;
93 this->ParseError = 1;
94 return 0;
96 int res;
97 res = this->ParseBuffer(inputString, length);
98 if ( res == 0 )
100 this->ParseError = 1;
102 return res;
105 //----------------------------------------------------------------------------
106 int cmXMLParser::CleanupParser()
108 if ( !this->Parser )
110 std::cerr << "Parser not initialized" << std::endl;
111 this->ParseError = 1;
112 return 0;
114 int result = !this->ParseError;
115 if(result)
117 // Tell the expat XML parser about the end-of-input.
118 if(!XML_Parse(static_cast<XML_Parser>(this->Parser), "", 0, 1))
120 this->ReportXmlParseError();
121 result = 0;
125 // Clean up the parser.
126 XML_ParserFree(static_cast<XML_Parser>(this->Parser));
127 this->Parser = 0;
129 return result;
132 //----------------------------------------------------------------------------
133 int cmXMLParser::ParseBuffer(const char* buffer, std::string::size_type count)
135 // Pass the buffer to the expat XML parser.
136 if(!XML_Parse(static_cast<XML_Parser>(this->Parser), buffer,
137 static_cast<int>(count), 0))
139 this->ReportXmlParseError();
140 return 0;
142 return 1;
145 //----------------------------------------------------------------------------
146 int cmXMLParser::ParseBuffer(const char* buffer)
148 return this->ParseBuffer(buffer, static_cast<int>(strlen(buffer)));
151 //----------------------------------------------------------------------------
152 int cmXMLParser::ParsingComplete()
154 // Default behavior is to parse to end of stream.
155 return 0;
158 //----------------------------------------------------------------------------
159 void cmXMLParser::StartElement(const char * name,
160 const char ** /*atts*/)
162 std::cout << "Start element: " << name << std::endl;
165 //----------------------------------------------------------------------------
166 void cmXMLParser::EndElement(const char * name)
168 std::cout << "End element: " << name << std::endl;
171 //----------------------------------------------------------------------------
172 void cmXMLParser::CharacterDataHandler(const char* /*inData*/,
173 int /*inLength*/)
177 //----------------------------------------------------------------------------
178 int cmXMLParser::IsSpace(char c)
180 return isspace(c);
183 //----------------------------------------------------------------------------
184 void cmXMLParserStartElement(void* parser, const char *name,
185 const char **atts)
187 // Begin element handler that is registered with the XML_Parser.
188 // This just casts the user data to a cmXMLParser and calls
189 // StartElement.
190 static_cast<cmXMLParser*>(parser)->StartElement(name, atts);
193 //----------------------------------------------------------------------------
194 void cmXMLParserEndElement(void* parser, const char *name)
196 // End element handler that is registered with the XML_Parser. This
197 // just casts the user data to a cmXMLParser and calls EndElement.
198 static_cast<cmXMLParser*>(parser)->EndElement(name);
201 //----------------------------------------------------------------------------
202 void cmXMLParserCharacterDataHandler(void* parser, const char* data,
203 int length)
205 // Character data handler that is registered with the XML_Parser.
206 // This just casts the user data to a cmXMLParser and calls
207 // CharacterDataHandler.
208 static_cast<cmXMLParser*>(parser)->CharacterDataHandler(data, length);
211 //----------------------------------------------------------------------------
212 void cmXMLParser::ReportXmlParseError()
214 std::cerr << "Error parsing XML in stream at line "
215 << XML_GetCurrentLineNumber(static_cast<XML_Parser>(this->Parser))
216 << ": "
217 << XML_ErrorString(XML_GetErrorCode(
218 static_cast<XML_Parser>(this->Parser))) << std::endl;