X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=include%2Fifmsxmldocument.h;h=6358e8f6a4b30ce1ac09d1e23a3cc7c14c7e8fe8;hb=dec33c63afafabf83c3039e916725cac6faef9b3;hp=ae555a1fad52ec06663cab8972462a21876028d0;hpb=9b22dd53fe62e312c1647310b7ec43aa127090af;p=fms.git diff --git a/include/ifmsxmldocument.h b/include/ifmsxmldocument.h index ae555a1..6358e8f 100644 --- a/include/ifmsxmldocument.h +++ b/include/ifmsxmldocument.h @@ -1,10 +1,18 @@ #ifndef _ifmsxmldocument_ #define _ifmsxmldocument_ +#include +#include +#include +#include +#include +#include +#include +#include + #include "stringfunctions.h" #include -#include #ifdef XMEM #include @@ -33,53 +41,116 @@ public: protected: /** + Poco doesn't like CDATA with whitespace outside the tags + This will remove the whitespace from around CDATA tags + */ + virtual const std::string FixCDATA(const std::string &xmlstr) + { + std::string rstring=xmlstr; + std::string::size_type beg1=std::string::npos; + std::string::size_type end1=rstring.find("",end1); + if(beg1!=end1-1) + { + rstring.erase(beg1+1,end1-(beg1+1)); + } + + beg2=rstring.find("]]>",end1); + if(beg2!=std::string::npos) + { + end2=rstring.find("<",beg2); + if(end2!=std::string::npos) + { + rstring.erase(beg2+3,end2-(beg2+3)); + } + } + + end1=rstring.find("LinkEndChild(txt); - return el; + if(doc) + { + Poco::XML::Text *txt=doc->createTextNode(value ? "true" : "false"); + Poco::XML::Element *el=doc->createElement(name); + el->appendChild(txt); + return el; + } + else + { + return NULL; + } } /** \brief Creates and returns an element with a CDATA value */ - virtual TiXmlElement *XMLCreateCDATAElement(const std::string &name, const std::string &data) + virtual Poco::XML::Element *XMLCreateCDATAElement(Poco::XML::Document *doc, const std::string &name, const std::string &data) { - TiXmlText *txt=new TiXmlText(data); - txt->SetCDATA(true); - TiXmlElement *el=new TiXmlElement(name); - el->LinkEndChild(txt); - return el; + if(doc) + { + // Poco XML won't break up CDATA sections correctly when assigned a string with the + // end tag is present. However, it will parse it correctly, so we will manually break the + // CDATA into separate parts + Poco::XML::CDATASection *sec=doc->createCDATASection(StringFunctions::Replace(data,"]]>","]]]>")); + Poco::XML::Element *el=doc->createElement(name); + el->appendChild(sec); + return el; + } + else + { + return NULL; + } } /** \brief Creates and returns a text element */ - virtual TiXmlElement *XMLCreateTextElement(const std::string &name, const std::string &data) + virtual Poco::XML::Element *XMLCreateTextElement(Poco::XML::Document *doc, const std::string &name, const std::string &data) { - TiXmlText *txt=new TiXmlText(data); - TiXmlElement *el=new TiXmlElement(name); - el->LinkEndChild(txt); - return el; + if(doc) + { + Poco::XML::Text *txt=doc->createTextNode(data); + Poco::XML::Element *el=doc->createElement(name); + el->appendChild(txt); + return el; + } + else + { + return NULL; + } } - virtual TiXmlElement *XMLCreateTextElement(const std::string &name, const long data) + virtual Poco::XML::Element *XMLCreateTextElement(Poco::XML::Document *doc, const std::string &name, const long data) { - std::string datastr; - StringFunctions::Convert(data,datastr); - return XMLCreateTextElement(name,datastr); + if(doc) + { + std::string datastr; + StringFunctions::Convert(data,datastr); + return XMLCreateTextElement(doc,name,datastr); + } + else + { + return NULL; + } } - virtual const bool XMLGetBooleanElement(TiXmlElement *parent, const std::string &name) + virtual const bool XMLGetBooleanElement(Poco::XML::Element *parent, const std::string &name) { - TiXmlHandle hnd(parent); - TiXmlText *txt=hnd.FirstChild(name).FirstChild().ToText(); - if(txt) + Poco::XML::Element *el=XMLGetFirstChild(parent,name); + if(el && el->firstChild()) { - if(txt->ValueStr()=="true") + if(el->firstChild()->getNodeValue()=="true") { return true; } @@ -91,6 +162,40 @@ protected: return false; } + virtual Poco::XML::Element *XMLGetFirstChild(Poco::XML::Node *parent, const std::string &name) + { + if(parent) + { + Poco::XML::Node *child=parent->firstChild(); + while(child && child->nodeName()!=name) + { + child=child->nextSibling(); + } + return static_cast(child); + } + else + { + return NULL; + } + } + + virtual Poco::XML::Element *XMLGetNextSibling(Poco::XML::Node *node, const std::string &name) + { + if(node) + { + Poco::XML::Node *next=node->nextSibling(); + while(next && next->nodeName()!=name) + { + next=next->nextSibling(); + } + return static_cast(next); + } + else + { + return NULL; + } + } + const std::string SanitizeSingleString(const std::string &text) { std::string returntext=text; @@ -101,6 +206,19 @@ protected: } return returntext; } + + const std::string GenerateXML(Poco::XML::Document *doc) + { + std::ostringstream str; + if(doc) + { + Poco::XML::DOMWriter dr; + dr.setOptions(Poco::XML::XMLWriter::WRITE_XML_DECLARATION | Poco::XML::XMLWriter::PRETTY_PRINT); + dr.setNewLine(Poco::XML::XMLWriter::NEWLINE_CRLF); + dr.writeNode(str,doc); + } + return str.str(); + } };