XMLParser.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <XML/XMLParser.h>
00022 #include <common/Defines.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 
00026 XMLParser::XMLParser(bool useContentNodes) : 
00027         root_(0), current_(0), 
00028         useContentNodes_(useContentNodes),
00029         source_("Not Specified")
00030 {
00031         // Init the XML parser
00032         p_ = XML_ParserCreate(0);
00033         XML_SetStartElementHandler(p_, startElementStaticHandler);
00034         XML_SetEndElementHandler(p_, endElementStaticHandler);
00035         XML_SetCharacterDataHandler(p_, characterDataStaticHandler);
00036         XML_SetUserData(p_, this);
00037 }
00038 
00039 XMLParser::~XMLParser()
00040 {
00041         // Cleanup
00042         XML_ParserFree(p_);
00043         delete root_;
00044 }
00045 
00046 bool XMLParser::parse(const char *data, int len, int final)
00047 {
00048         // Parser the XML
00049         XML_Status status;
00050         status = XML_Parse(p_, data, len, final);
00051         return (status == XML_STATUS_OK);
00052 }
00053 
00054 const char *XMLParser::getParseError()
00055 {
00056         XML_Error errorCode = XML_GetErrorCode(p_);
00057 
00058         static char message[1024];
00059         snprintf(message, 1024,
00060                 "Parse Error, File %s: Line:%i Col:%i Error:%s",
00061                 source_.c_str(),
00062                 XML_GetCurrentLineNumber(p_),
00063                 XML_GetCurrentColumnNumber(p_),
00064                 XML_ErrorString(errorCode));
00065         return message;
00066 }
00067 
00068 void XMLParser::startElementHandler(const XML_Char *name,
00069                            const XML_Char **atts)
00070 {
00071         if (!root_)
00072         {
00073                 // Create the root node
00074                 root_ = current_ = new XMLNode(name);
00075                 root_->setUseContentNodes(useContentNodes_);
00076                 root_->setSource(source_.c_str());
00077                 root_->setLine(XML_GetCurrentLineNumber(p_),
00078                         XML_GetCurrentColumnNumber(p_));
00079         }
00080         else
00081         {
00082                 DIALOG_ASSERT(current_);
00083 
00084                 // Add a new child to this node
00085                 XMLNode *newNode = new XMLNode(name);
00086                 current_->setUseContentNodes(useContentNodes_);
00087                 current_->addChild(newNode);
00088                 current_ = newNode;
00089                 current_->setLine(XML_GetCurrentLineNumber(p_),
00090                         XML_GetCurrentColumnNumber(p_));
00091         }
00092 
00093         if (atts)
00094         {
00095                 while (*atts)
00096                 {
00097                         const XML_Char *name = *atts;
00098                         atts++;
00099                         const XML_Char *value = *atts;
00100                         atts++;
00101 
00102                         XMLNode *param = new XMLNode(name, "", XMLNode::XMLParameterType);
00103                         param->addContent(value, strlen(value));
00104                         param->setLine(XML_GetCurrentLineNumber(p_),
00105                                 XML_GetCurrentColumnNumber(p_));
00106                         current_->addParameter(param);
00107                 }
00108         }
00109 }
00110 
00111 void XMLParser::endElementHandler(const XML_Char *name)
00112 {
00113         DIALOG_ASSERT(current_);
00114         DIALOG_ASSERT(strcmp(name, current_->getName()) == 0);
00115 
00116         current_ = (XMLNode *) current_->getParent();
00117 }
00118 
00119 void XMLParser::characterDataHandler(const XML_Char *s,
00120                             int len)
00121 {
00122         current_->addContent(s, len);
00123 }
00124 
00125 void XMLParser::startElementStaticHandler(void *userData,
00126                            const XML_Char *name,
00127                            const XML_Char **atts)
00128 {
00129         XMLParser *parser = (XMLParser *) userData;
00130         parser->startElementHandler(name, atts);
00131 }
00132 
00133 void XMLParser::endElementStaticHandler(void *userData,
00134                          const XML_Char *name)
00135 {
00136         XMLParser *parser = (XMLParser *) userData;
00137         parser->endElementHandler(name);
00138 }
00139 
00140 void XMLParser::characterDataStaticHandler(void *userData,
00141                             const XML_Char *s,
00142                             int len)
00143 {
00144         XMLParser *parser = (XMLParser *) userData;
00145         parser->characterDataHandler(s, len);
00146 }

Generated on Mon Feb 16 15:14:52 2009 for Scorched3D by  doxygen 1.5.3