From d1dd79047e2612f6811ef879f19c3b8449bfc979 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 15 Mar 2012 05:59:23 +0000 Subject: [PATCH] - no need for xerces anymore? All XML is now done by rapidxml by default (loading and saving) --- source/glest_game/game/game.cpp | 3 +- source/shared_lib/include/xml/xml_parser.h | 3 +- source/shared_lib/sources/xml/xml_parser.cpp | 120 ++++++++++++------- 3 files changed, 82 insertions(+), 44 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index e82382d2..73fcbd47 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -3436,7 +3436,8 @@ void Game::toggleTeamColorMarker() { } void Game::saveGame(string name) { - XmlTree xmlTree(XML_XERCES_ENGINE); + //XmlTree xmlTree(XML_XERCES_ENGINE); + XmlTree xmlTree; xmlTree.init("megaglest-saved-game"); XmlNode *rootNode = xmlTree.getRootNode(); diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index bb1f6424..8eb51254 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -70,7 +70,7 @@ public: class XmlIoRapid { private: static bool initialized; - rapidxml::xml_document<> *doc; + xml_document<> *doc; private: XmlIoRapid(); @@ -153,6 +153,7 @@ public: XmlAttribute *addAttribute(const string &name, const string &value, std::map mapTagReplacementValues); XERCES_CPP_NAMESPACE::DOMElement *buildElement(XERCES_CPP_NAMESPACE::DOMDocument *document) const; + xml_node<>* buildElement(xml_document<> *document) const; private: string getTreeString() const; diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 2d8bca82..80c738a3 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -27,6 +27,7 @@ #include "platform_util.h" #include "cache_manager.h" +#include "rapidxml_print.hpp" #include "leak_dumper.h" XERCES_CPP_NAMESPACE_USE @@ -236,7 +237,7 @@ XmlIoRapid::XmlIoRapid() { } try { - doc = new rapidxml::xml_document<>(); + doc = new xml_document<>(); } catch(const DOMException &ex) { SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Exception while creating XML parser, msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.getMessage()); @@ -318,48 +319,66 @@ XmlNode *XmlIoRapid::load(const string &path, std::map mapTagRepl } void XmlIoRapid::save(const string &path, const XmlNode *node){ -// try{ -// XMLCh str[strSize]; -// XMLString::transcode(node->getName().c_str(), str, strSize-1); + try { + xml_document<> doc; + + // xml declaration + xml_node<>* decl = doc.allocate_node(node_declaration); + decl->append_attribute(doc.allocate_attribute(doc.allocate_string("version"), doc.allocate_string("1.0"))); + decl->append_attribute(doc.allocate_attribute(doc.allocate_string("encoding"), doc.allocate_string("utf-8"))); + decl->append_attribute(doc.allocate_attribute(doc.allocate_string("standalone"), doc.allocate_string("no"))); + doc.append_node(decl); + + // root node + xml_node<>* root = doc.allocate_node(node_element, doc.allocate_string(node->getName().c_str())); + for(unsigned int i = 0; i < node->getAttributeCount() ; ++i){ + XmlAttribute *attr = node->getAttribute(i); + root->append_attribute(doc.allocate_attribute( + doc.allocate_string(attr->getName().c_str()), + doc.allocate_string(attr->getValue("",false).c_str()))); + } + doc.append_node(root); + + // child nodes + for(unsigned int i = 0; i < node->getChildCount(); ++i) { + root->append_node(node->getChild(i)->buildElement(&doc)); + } + +// std::string xml_as_string; +// // watch for name collisions here, print() is a very common function name! +// print(std::back_inserter(xml_as_string), doc); +// // xml_as_string now contains the XML in string form, indented +// // (in all its angle bracket glory) // -// XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *document= implementation->createDocument(0, str, 0); -// DOMElement *documentElement= document->getDocumentElement(); -// for(unsigned int i = 0; i < node->getAttributeCount() ; ++i){ -// XmlAttribute *attr = node->getAttribute(i); -// -// XMLCh strName[strSize]; -// XMLString::transcode(attr->getName().c_str(), strName, strSize-1); -// XMLCh strValue[strSize]; -// XMLString::transcode(attr->getValue("",false).c_str(), strValue, strSize-1); -// -// documentElement->setAttribute(strName,strValue); -// } -// -// for(unsigned int i=0; igetChildCount(); ++i){ -// documentElement->appendChild(node->getChild(i)->buildElement(document)); -// } -// -// LocalFileFormatTarget file(path.c_str()); -//#if XERCES_VERSION_MAJOR < 3 -// DOMWriter* writer = implementation->createDOMWriter(); -// writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); -// writer->writeNode(&file, *document); -//#else -// DOMLSSerializer *serializer = implementation->createLSSerializer(); -// DOMLSOutput* output=implementation->createLSOutput(); -// DOMConfiguration* config=serializer->getDomConfig(); -// config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,true); -// output->setByteStream(&file); -// serializer->write(document,output); -// output->release(); -// serializer->release(); -//#endif -// document->release(); -// } -// catch(const DOMException &e){ -// SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Exception while saving: [%s], %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),XMLString::transcode(e.msg)); -// throw runtime_error("Exception while saving: " + path + ": " + XMLString::transcode(e.msg)); -// } +// std::string xml_no_indent; +// // print_no_indenting is the only flag that print() knows about +// print(std::back_inserter(xml_no_indent), doc, print_no_indenting); +// // xml_no_indent now contains non-indented XML + +#if defined(WIN32) && !defined(__MINGW32__) + FILE *fp = _wfopen(utf8_decode(path).c_str(), L"wt"); + ofstream xmlFile(fp); +#else + ofstream xmlFile(path.c_str()); +#endif + if(xmlFile.is_open() == false) { + throw runtime_error("Can not open file: [" + path + "]"); + } + + //xmlFile << xml_no_indent; +// xmlFile << xml_as_string << '\0'; + xmlFile << doc << '\0'; + +#if defined(WIN32) && !defined(__MINGW32__) + if(fp) { + fclose(fp); + } +#endif + } + catch(const exception &e){ + SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Exception while saving: [%s], %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),e.what()); + throw runtime_error("Exception while saving [" + path + "] msg: " + e.what()); + } } // ===================================================== @@ -670,6 +689,23 @@ DOMElement *XmlNode::buildElement(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *do return node; } +xml_node<>* XmlNode::buildElement(xml_document<> *document) const { + xml_node<>* node = document->allocate_node(node_element, document->allocate_string(name.c_str())); + + for(unsigned int i = 0; i < attributes.size(); ++i) { + node->append_attribute( + document->allocate_attribute( + document->allocate_string(attributes[i]->getName().c_str()), + document->allocate_string(attributes[i]->getValue().c_str()))); + } + + for(unsigned int i = 0; i < children.size(); ++i) { + node->append_node(children[i]->buildElement(document)); + } + + return node; +} + string XmlNode::getTreeString() const { string str;