Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Remove tinyxml2 from CCSaxParser implement. (#20141)" #17

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions cocos/platform/CCSAXParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,66 @@
#include <vector> // because its based on windows 8 build :P

#include "platform/CCFileUtils.h"
#include "tinyxml2.h"
#include "rapidxml/rapidxml_sax3.hpp"

NS_CC_BEGIN

class XmlSaxHander : public tinyxml2::XMLVisitor
{
public:
XmlSaxHander():_ccsaxParserImp(0){};

virtual bool VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute );
virtual bool VisitExit( const tinyxml2::XMLElement& element );
virtual bool Visit( const tinyxml2::XMLText& text );
virtual bool Visit( const tinyxml2::XMLUnknown&){ return true; }

void setSAXParserImp(SAXParser* parser)
{
_ccsaxParserImp = parser;
}

private:
SAXParser *_ccsaxParserImp;
};


bool XmlSaxHander::VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute )
{
//log(" VisitEnter %s",element.Value());

std::vector<const char*> attsVector;
for( const tinyxml2::XMLAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
{
//log("%s", attrib->Name());
attsVector.push_back(attrib->Name());
//log("%s",attrib->Value());
attsVector.push_back(attrib->Value());
}

// nullptr is used in c++11
//attsVector.push_back(nullptr);
attsVector.push_back(nullptr);

SAXParser::startElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value(), (const CC_XML_CHAR **)(&attsVector[0]));
return true;
}
bool XmlSaxHander::VisitExit( const tinyxml2::XMLElement& element )
{
//log("VisitExit %s",element.Value());

SAXParser::endElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value());
return true;
}

bool XmlSaxHander::Visit( const tinyxml2::XMLText& text )
{
//log("Visit %s",text.Value());
SAXParser::textHandler(_ccsaxParserImp, (const CC_XML_CHAR *)text.Value(), strlen(text.Value()));
return true;
}

/// rapidxml SAX handler
class RapidXmlSaxHander : public rapidxml::xml_sax2_handler
{
Expand Down Expand Up @@ -80,11 +136,12 @@ bool SAXParser::init(const char* /*encoding*/)

bool SAXParser::parse(const char* xmlData, size_t dataLength)
{
if(xmlData != nullptr && dataLength > 0) {
std::string mutableData(xmlData, dataLength);
return this->parseIntrusive(&mutableData.front(), dataLength);
}
return false;
tinyxml2::XMLDocument tinyDoc;
tinyDoc.Parse(xmlData, dataLength);
XmlSaxHander printer;
printer.setSAXParserImp(this);

return tinyDoc.Accept( &printer );
}

bool SAXParser::parse(const std::string& filename)
Expand All @@ -93,7 +150,7 @@ bool SAXParser::parse(const std::string& filename)
Data data = FileUtils::getInstance()->getDataFromFile(filename);
if (!data.isNull())
{
ret = parseIntrusive((char*)data.getBytes(), data.getSize());
ret = parse((const char*)data.getBytes(), data.getSize());
}

return ret;
Expand Down