version 0.3.2
[fms.git] / src / freenet / trustlistxml.cpp
index 1b36aa3..95d3c5e 100644 (file)
@@ -1,6 +1,8 @@
 #include "../../include/freenet/trustlistxml.h"\r
 #include "../../include/stringfunctions.h"\r
 \r
+#include <algorithm>\r
+\r
 #ifdef XMEM\r
        #include <xmem.h>\r
 #endif\r
@@ -10,9 +12,9 @@ TrustListXML::TrustListXML()
        Initialize();\r
 }\r
 \r
-void TrustListXML::AddTrust(const std::string &identity, const long messagetrust, const long trustlisttrust)\r
+void TrustListXML::AddTrust(const std::string &identity, const long messagetrust, const long trustlisttrust, const std::string &messagetrustcomment, const std::string &trustlisttrustcomment)\r
 {\r
-       m_trust.push_back(trust(identity,messagetrust,trustlisttrust));\r
+       m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));\r
 }\r
 \r
 std::string TrustListXML::GetIdentity(const long index)\r
@@ -39,6 +41,18 @@ long TrustListXML::GetMessageTrust(const long index)
        }       \r
 }\r
 \r
+std::string TrustListXML::GetMessageTrustComment(const long index)\r
+{\r
+       if(index>=0 && index<m_trust.size())\r
+       {\r
+               return m_trust[index].m_messagetrustcomment;\r
+       }\r
+       else\r
+       {\r
+               return "";\r
+       }       \r
+}\r
+\r
 long TrustListXML::GetTrustListTrust(const long index)\r
 {\r
        if(index>=0 && index<m_trust.size())\r
@@ -51,16 +65,24 @@ long TrustListXML::GetTrustListTrust(const long index)
        }\r
 }\r
 \r
+std::string TrustListXML::GetTrustListTrustComment(const long index)\r
+{\r
+       if(index>=0 && index<m_trust.size())\r
+       {\r
+               return m_trust[index].m_trustlisttrustcomment;\r
+       }\r
+       else\r
+       {\r
+               return "";\r
+       }\r
+}\r
+\r
 std::string TrustListXML::GetXML()\r
 {\r
-       TiXmlDocument td;\r
-       TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
-       TiXmlElement *tid;\r
-       TiXmlPrinter tp;\r
+       Poco::AutoPtr<Poco::XML::Document> doc=new Poco::XML::Document;\r
+       Poco::AutoPtr<Poco::XML::Element> root=doc->createElement("TrustList");\r
 \r
-       td.LinkEndChild(tdec);\r
-       tid=new TiXmlElement("TrustList");\r
-       td.LinkEndChild(tid);\r
+       doc->appendChild(root);\r
 \r
        for(std::vector<trust>::iterator i=m_trust.begin(); i!=m_trust.end(); i++)\r
        {\r
@@ -68,15 +90,28 @@ std::string TrustListXML::GetXML()
                std::string trustlisttrust;\r
                StringFunctions::Convert((*i).m_messagetrust,messagetrust);\r
                StringFunctions::Convert((*i).m_trustlisttrust,trustlisttrust);\r
-               TiXmlElement *tr=new TiXmlElement("Trust");\r
-               tid->LinkEndChild(tr);\r
-               tr->LinkEndChild(XMLCreateCDATAElement("Identity",(*i).m_identity));\r
-               tr->LinkEndChild(XMLCreateTextElement("MessageTrustLevel",messagetrust));\r
-               tr->LinkEndChild(XMLCreateTextElement("TrustListTrustLevel",trustlisttrust));\r
+               Poco::AutoPtr<Poco::XML::Element> tr=doc->createElement("Trust");\r
+               root->appendChild(tr);\r
+               tr->appendChild(XMLCreateCDATAElement(doc,"Identity",(*i).m_identity));\r
+               if((*i).m_messagetrust>=0)\r
+               {\r
+                       tr->appendChild(XMLCreateTextElement(doc,"MessageTrustLevel",messagetrust));\r
+               }\r
+               if((*i).m_trustlisttrust>=0)\r
+               {\r
+                       tr->appendChild(XMLCreateTextElement(doc,"TrustListTrustLevel",trustlisttrust));\r
+               }\r
+               if((*i).m_messagetrustcomment!="")\r
+               {\r
+                       tr->appendChild(XMLCreateTextElement(doc,"MessageTrustComment",(*i).m_messagetrustcomment));\r
+               }\r
+               if((*i).m_trustlisttrustcomment!="")\r
+               {\r
+                       tr->appendChild(XMLCreateTextElement(doc,"TrustListTrustComment",(*i).m_trustlisttrustcomment));\r
+               }\r
        }\r
 \r
-       td.Accept(&tp);\r
-       return std::string(tp.CStr());\r
+       return GenerateXML(doc);\r
 }\r
 \r
 void TrustListXML::Initialize()\r
@@ -86,64 +121,95 @@ void TrustListXML::Initialize()
 \r
 const bool TrustListXML::ParseXML(const std::string &xml)\r
 {\r
-       TiXmlDocument td;\r
-       td.Parse(xml.c_str());\r
 \r
-       if(!td.Error())\r
+       bool parsed=false;\r
+       Poco::XML::DOMParser dp;\r
+\r
+       Initialize();\r
+\r
+       try\r
        {\r
-               std::string identity;\r
-               std::string messagetruststr;\r
-               std::string trustlisttruststr;\r
-               long messagetrust;\r
-               long trustlisttrust;\r
-               TiXmlText *txt;\r
-               TiXmlHandle hnd(&td);\r
-               TiXmlNode *node;\r
-\r
-               Initialize();\r
-\r
-               node=hnd.FirstChild("TrustList").FirstChild("Trust").ToElement();\r
-               while(node)\r
+               Poco::AutoPtr<Poco::XML::Document> doc=dp.parseString(FixCDATA(xml));\r
+               Poco::XML::Element *root=XMLGetFirstChild(doc,"TrustList");\r
+               Poco::XML::Element *trustel=NULL;\r
+               Poco::XML::Element *txt=NULL;\r
+\r
+               std::vector<std::string> foundkeys;\r
+\r
+               trustel=XMLGetFirstChild(root,"Trust");\r
+               while(trustel)\r
                {\r
-                       identity="";\r
-                       messagetrust=-1;\r
-                       trustlisttrust=-1;\r
+                       std::string identity="";\r
+                       int messagetrust=-1;\r
+                       int trustlisttrust=-1;\r
+                       std::string messagetrustcomment="";\r
+                       std::string trustlisttrustcomment="";\r
 \r
-                       TiXmlHandle hnd2(node);\r
-                       txt=hnd2.FirstChild("Identity").FirstChild().ToText();\r
+                       txt=XMLGetFirstChild(trustel,"Identity");\r
+                       if(txt)\r
+                       {\r
+                               if(txt->firstChild())\r
+                               {\r
+                                       identity=SanitizeSingleString(txt->firstChild()->getNodeValue());\r
+                               }\r
+                       }\r
+                       txt=XMLGetFirstChild(trustel,"MessageTrustLevel");\r
+                       if(txt)\r
+                       {\r
+                               if(txt->firstChild())\r
+                               {\r
+                                       std::string mtl=txt->firstChild()->getNodeValue();\r
+                                       StringFunctions::Convert(mtl,messagetrust);\r
+                               }\r
+                       }\r
+                       txt=XMLGetFirstChild(trustel,"TrustListTrustLevel");\r
                        if(txt)\r
                        {\r
-                               identity=txt->ValueStr();\r
+                               if(txt->firstChild())\r
+                               {\r
+                                       std::string tltl=txt->firstChild()->getNodeValue();\r
+                                       StringFunctions::Convert(tltl,trustlisttrust);\r
+                               }\r
                        }\r
-                       txt=hnd2.FirstChild("MessageTrustLevel").FirstChild().ToText();\r
+                       txt=XMLGetFirstChild(trustel,"MessageTrustComment");\r
                        if(txt)\r
                        {\r
-                               messagetruststr=txt->ValueStr();\r
-                               StringFunctions::Convert(messagetruststr,messagetrust);\r
+                               if(txt->firstChild())\r
+                               {\r
+                                       messagetrustcomment=SanitizeSingleString(txt->firstChild()->getNodeValue());\r
+                               }\r
                        }\r
-                       txt=hnd2.FirstChild("TrustListTrustLevel").FirstChild().ToText();\r
+                       txt=XMLGetFirstChild(trustel,"TrustListTrustComment");\r
                        if(txt)\r
                        {\r
-                               trustlisttruststr=txt->ValueStr();\r
-                               StringFunctions::Convert(trustlisttruststr,trustlisttrust);\r
+                               if(txt->firstChild())\r
+                               {\r
+                                       trustlisttrustcomment=SanitizeSingleString(txt->firstChild()->getNodeValue());\r
+                               }\r
                        }\r
 \r
-                       if(identity!="" && messagetrust>=0 && messagetrust<=100 && trustlisttrust>=0 && trustlisttrust<=100)\r
+                       if(identity!="" && messagetrust>=-1 && messagetrust<=100 && trustlisttrust>=-1 && trustlisttrust<=100)\r
                        {\r
-                               m_trust.push_back(trust(identity,messagetrust,trustlisttrust));\r
+                               // check so we don't add the same identity multiple times from a trust list\r
+                               if(std::find(foundkeys.begin(),foundkeys.end(),identity)==foundkeys.end())\r
+                               {\r
+                                       foundkeys.push_back(identity);\r
+                                       m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));\r
+                               }\r
                        }\r
                        else\r
                        {\r
-                               m_log->WriteLog(LogFile::LOGLEVEL_ERROR,__FUNCTION__," malformed Trust in TrustList.xml");\r
+                               m_log->error("TrustListXML::ParseXML malformed Trust in TrustList.xml");\r
                        }\r
-                       \r
-                       node=node->NextSibling("Trust");\r
+\r
+                       trustel=XMLGetNextSibling(trustel,"Trust");\r
                }\r
-               return true;\r
 \r
+               parsed=true;\r
        }\r
-       else\r
+       catch(...)\r
        {\r
-               return false;\r
        }\r
+\r
+       return parsed;\r
 }\r