2012-04-19 14:35:52 +08:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
Copyright (c) 2010 Максим Аксенов
|
2014-01-07 11:25:07 +08:00
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
2013-01-25 18:39:54 +08:00
|
|
|
|
Copyright (c) 2013 Martell Malone
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
|
#include "platform/CCSAXParser.h"
|
2013-01-25 18:39:54 +08:00
|
|
|
|
|
|
|
|
|
#include <vector> // because its based on windows 8 build :P
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
#include "tinyxml2.h"
|
2016-08-25 07:37:03 +08:00
|
|
|
|
#include "rapidxml/rapidxml_sax3.hpp"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
2013-01-25 18:39:54 +08:00
|
|
|
|
class XmlSaxHander : public tinyxml2::XMLVisitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-08-14 21:17:51 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2013-01-25 18:39:54 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2015-08-14 21:17:51 +08:00
|
|
|
|
SAXParser *_ccsaxParserImp;
|
2013-01-25 18:39:54 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool XmlSaxHander::VisitEnter( const tinyxml2::XMLElement& element, const tinyxml2::XMLAttribute* firstAttribute )
|
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
|
//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());
|
|
|
|
|
}
|
2013-02-06 10:18:31 +08:00
|
|
|
|
|
|
|
|
|
// nullptr is used in c++11
|
2015-08-14 21:17:51 +08:00
|
|
|
|
//attsVector.push_back(nullptr);
|
2013-12-18 17:47:20 +08:00
|
|
|
|
attsVector.push_back(nullptr);
|
2013-01-25 18:39:54 +08:00
|
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
|
SAXParser::startElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value(), (const CC_XML_CHAR **)(&attsVector[0]));
|
|
|
|
|
return true;
|
2013-01-25 18:39:54 +08:00
|
|
|
|
}
|
|
|
|
|
bool XmlSaxHander::VisitExit( const tinyxml2::XMLElement& element )
|
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
|
//log("VisitExit %s",element.Value());
|
2013-01-25 18:39:54 +08:00
|
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
|
SAXParser::endElement(_ccsaxParserImp, (const CC_XML_CHAR *)element.Value());
|
|
|
|
|
return true;
|
2013-01-25 18:39:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool XmlSaxHander::Visit( const tinyxml2::XMLText& text )
|
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
|
//log("Visit %s",text.Value());
|
2016-09-26 14:49:47 +08:00
|
|
|
|
SAXParser::textHandler(_ccsaxParserImp, (const CC_XML_CHAR *)text.Value(), strlen(text.Value()));
|
2015-08-14 21:17:51 +08:00
|
|
|
|
return true;
|
2013-01-25 18:39:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 07:37:03 +08:00
|
|
|
|
/// rapidxml SAX handler
|
|
|
|
|
class RapidXmlSaxHander : public rapidxml::xml_sax2_handler
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
RapidXmlSaxHander() :_ccsaxParserImp(0) {};
|
|
|
|
|
|
|
|
|
|
void setSAXParserImp(SAXParser* parser)
|
|
|
|
|
{
|
|
|
|
|
_ccsaxParserImp = parser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmlSAX2StartElement(const char *name, size_t /*len*/, const char **atts, size_t /*attslen*/) override
|
|
|
|
|
{
|
|
|
|
|
SAXParser::startElement(_ccsaxParserImp, (const CC_XML_CHAR *)name, (const CC_XML_CHAR **)atts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmlSAX2EndElement(const char *name, size_t /*len*/) override
|
|
|
|
|
{
|
|
|
|
|
SAXParser::endElement(_ccsaxParserImp, (const CC_XML_CHAR *)name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void xmlSAX2Text(const char *s, size_t len) override
|
|
|
|
|
{
|
|
|
|
|
SAXParser::textHandler(_ccsaxParserImp, (const CC_XML_CHAR *)s, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SAXParser *_ccsaxParserImp;
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
SAXParser::SAXParser()
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
_delegator = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
SAXParser::~SAXParser(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
bool SAXParser::init(const char *encoding)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
CC_UNUSED_PARAM(encoding);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// nothing to do
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
bool SAXParser::parse(const char* xmlData, size_t dataLength)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
|
tinyxml2::XMLDocument tinyDoc;
|
|
|
|
|
tinyDoc.Parse(xmlData, dataLength);
|
|
|
|
|
XmlSaxHander printer;
|
|
|
|
|
printer.setSAXParserImp(this);
|
|
|
|
|
|
|
|
|
|
return tinyDoc.Accept( &printer );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 21:16:46 +08:00
|
|
|
|
bool SAXParser::parse(const std::string& filename)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-11-06 09:36:44 +08:00
|
|
|
|
bool ret = false;
|
2013-12-18 14:58:17 +08:00
|
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(filename);
|
|
|
|
|
if (!data.isNull())
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 14:58:17 +08:00
|
|
|
|
ret = parse((const char*)data.getBytes(), data.getSize());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
|
|
2013-11-06 09:36:44 +08:00
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 07:37:03 +08:00
|
|
|
|
bool SAXParser::parseIntrusive(char* xmlData, size_t dataLength)
|
|
|
|
|
{
|
|
|
|
|
RapidXmlSaxHander printer;
|
|
|
|
|
printer.setSAXParserImp(this);
|
|
|
|
|
|
|
|
|
|
rapidxml::xml_sax3_parser<> parser(&printer);
|
|
|
|
|
try {
|
2016-09-26 14:49:47 +08:00
|
|
|
|
parser.parse<>(xmlData, static_cast<int>(dataLength));
|
2016-08-25 07:37:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (rapidxml::parse_error& e)
|
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: SAXParser: Error parsing xml: %s at %s", e.what(), e.where<char>());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void SAXParser::startElement(void *ctx, const CC_XML_CHAR *name, const CC_XML_CHAR **atts)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
((SAXParser*)(ctx))->_delegator->startElement(ctx, (char*)name, (const char**)atts);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void SAXParser::endElement(void *ctx, const CC_XML_CHAR *name)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
((SAXParser*)(ctx))->_delegator->endElement(ctx, (char*)name);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2016-09-26 14:49:47 +08:00
|
|
|
|
void SAXParser::textHandler(void *ctx, const CC_XML_CHAR *name, size_t len)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
((SAXParser*)(ctx))->_delegator->textHandler(ctx, (char*)name, len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
|
void SAXParser::setDelegator(SAXDelegator* delegator)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
_delegator = delegator;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
|
|
|