Skip to content

Commit 3e89cf6

Browse files
authored
Merge pull request #17 from hashcube/use_tinyxml2
Revert "Remove tinyxml2 from CCSaxParser implement. (cocos2d#20141)"
2 parents e231305 + e161bdd commit 3e89cf6

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

cocos/platform/CCSAXParser.cpp

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,66 @@
2828
#include <vector> // because its based on windows 8 build :P
2929

3030
#include "platform/CCFileUtils.h"
31+
#include "tinyxml2.h"
3132
#include "rapidxml/rapidxml_sax3.hpp"
3233

3334
NS_CC_BEGIN
3435

36+
class XmlSaxHander : public tinyxml2::XMLVisitor
37+
{
38+
public:
39+
XmlSaxHander():_ccsaxParserImp(0){};
40+
41+
virtual bool VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute );
42+
virtual bool VisitExit( const tinyxml2::XMLElement& element );
43+
virtual bool Visit( const tinyxml2::XMLText& text );
44+
virtual bool Visit( const tinyxml2::XMLUnknown&){ return true; }
45+
46+
void setSAXParserImp(SAXParser* parser)
47+
{
48+
_ccsaxParserImp = parser;
49+
}
50+
51+
private:
52+
SAXParser *_ccsaxParserImp;
53+
};
54+
55+
56+
bool XmlSaxHander::VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute )
57+
{
58+
//log(" VisitEnter %s",element.Value());
59+
60+
std::vector<const char*> attsVector;
61+
for( const tinyxml2::XMLAttribute* attrib = firstAttribute; attrib; attrib = attrib->Next() )
62+
{
63+
//log("%s", attrib->Name());
64+
attsVector.push_back(attrib->Name());
65+
//log("%s",attrib->Value());
66+
attsVector.push_back(attrib->Value());
67+
}
68+
69+
// nullptr is used in c++11
70+
//attsVector.push_back(nullptr);
71+
attsVector.push_back(nullptr);
72+
73+
SAXParser::startElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value(), (const CC_XML_CHAR **)(&attsVector[0]));
74+
return true;
75+
}
76+
bool XmlSaxHander::VisitExit( const tinyxml2::XMLElement& element )
77+
{
78+
//log("VisitExit %s",element.Value());
79+
80+
SAXParser::endElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value());
81+
return true;
82+
}
83+
84+
bool XmlSaxHander::Visit( const tinyxml2::XMLText& text )
85+
{
86+
//log("Visit %s",text.Value());
87+
SAXParser::textHandler(_ccsaxParserImp, (const CC_XML_CHAR *)text.Value(), strlen(text.Value()));
88+
return true;
89+
}
90+
3591
/// rapidxml SAX handler
3692
class RapidXmlSaxHander : public rapidxml::xml_sax2_handler
3793
{
@@ -80,11 +136,12 @@ bool SAXParser::init(const char* /*encoding*/)
80136

81137
bool SAXParser::parse(const char* xmlData, size_t dataLength)
82138
{
83-
if(xmlData != nullptr && dataLength > 0) {
84-
std::string mutableData(xmlData, dataLength);
85-
return this->parseIntrusive(&mutableData.front(), dataLength);
86-
}
87-
return false;
139+
tinyxml2::XMLDocument tinyDoc;
140+
tinyDoc.Parse(xmlData, dataLength);
141+
XmlSaxHander printer;
142+
printer.setSAXParserImp(this);
143+
144+
return tinyDoc.Accept( &printer );
88145
}
89146

90147
bool SAXParser::parse(const std::string& filename)
@@ -93,7 +150,7 @@ bool SAXParser::parse(const std::string& filename)
93150
Data data = FileUtils::getInstance()->getDataFromFile(filename);
94151
if (!data.isNull())
95152
{
96-
ret = parseIntrusive((char*)data.getBytes(), data.getSize());
153+
ret = parse((const char*)data.getBytes(), data.getSize());
97154
}
98155

99156
return ret;

0 commit comments

Comments
 (0)