1 #include "../../include/freenet/trustlistxml.h"
\r
2 #include "../../include/stringfunctions.h"
\r
10 TrustListXML::TrustListXML()
\r
15 void TrustListXML::AddTrust(const std::string &identity, const long messagetrust, const long trustlisttrust, const std::string &messagetrustcomment, const std::string &trustlisttrustcomment)
\r
17 m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));
\r
20 std::string TrustListXML::GetIdentity(const long index)
\r
22 if(index>=0 && index<m_trust.size())
\r
24 return m_trust[index].m_identity;
\r
32 long TrustListXML::GetMessageTrust(const long index)
\r
34 if(index>=0 && index<m_trust.size())
\r
36 return m_trust[index].m_messagetrust;
\r
44 std::string TrustListXML::GetMessageTrustComment(const long index)
\r
46 if(index>=0 && index<m_trust.size())
\r
48 return m_trust[index].m_messagetrustcomment;
\r
56 long TrustListXML::GetTrustListTrust(const long index)
\r
58 if(index>=0 && index<m_trust.size())
\r
60 return m_trust[index].m_trustlisttrust;
\r
68 std::string TrustListXML::GetTrustListTrustComment(const long index)
\r
70 if(index>=0 && index<m_trust.size())
\r
72 return m_trust[index].m_trustlisttrustcomment;
\r
80 std::string TrustListXML::GetXML()
\r
82 Poco::AutoPtr<Poco::XML::Document> doc=new Poco::XML::Document;
\r
84 //TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");
\r
85 Poco::XML::Element *root=doc->createElement("TrustList");
\r
86 //TiXmlElement *tid;
\r
89 doc->appendChild(root);
\r
90 //td.LinkEndChild(tdec);
\r
91 //tid=new TiXmlElement("TrustList");
\r
92 //td.LinkEndChild(tid);
\r
94 for(std::vector<trust>::iterator i=m_trust.begin(); i!=m_trust.end(); i++)
\r
96 std::string messagetrust;
\r
97 std::string trustlisttrust;
\r
98 StringFunctions::Convert((*i).m_messagetrust,messagetrust);
\r
99 StringFunctions::Convert((*i).m_trustlisttrust,trustlisttrust);
\r
100 Poco::XML::Element *tr=doc->createElement("Trust");
\r
101 //TiXmlElement *tr=new TiXmlElement("Trust");
\r
102 root->appendChild(tr);
\r
103 //tid->LinkEndChild(tr);
\r
104 tr->appendChild(XMLCreateCDATAElement(doc,"Identity",(*i).m_identity));
\r
105 //tr->LinkEndChild(XMLCreateCDATAElement("Identity",(*i).m_identity));
\r
106 if((*i).m_messagetrust>=0)
\r
108 tr->appendChild(XMLCreateTextElement(doc,"MessageTrustLevel",messagetrust));
\r
109 //tr->LinkEndChild(XMLCreateTextElement("MessageTrustLevel",messagetrust));
\r
111 if((*i).m_trustlisttrust>=0)
\r
113 tr->appendChild(XMLCreateTextElement(doc,"TrustListTrustLevel",trustlisttrust));
\r
114 //tr->LinkEndChild(XMLCreateTextElement("TrustListTrustLevel",trustlisttrust));
\r
116 if((*i).m_messagetrustcomment!="")
\r
118 tr->appendChild(XMLCreateTextElement(doc,"MessageTrustComment",(*i).m_messagetrustcomment));
\r
119 //tr->LinkEndChild(XMLCreateCDATAElement("MessageTrustComment",(*i).m_messagetrustcomment));
\r
121 if((*i).m_trustlisttrustcomment!="")
\r
123 tr->appendChild(XMLCreateTextElement(doc,"TrustListTrustComment",(*i).m_trustlisttrustcomment));
\r
124 //tr->LinkEndChild(XMLCreateCDATAElement("TrustListTrustComment",(*i).m_trustlisttrustcomment));
\r
129 //return std::string(tp.CStr());
\r
130 return GenerateXML(doc);
\r
133 void TrustListXML::Initialize()
\r
138 const bool TrustListXML::ParseXML(const std::string &xml)
\r
142 Poco::XML::DOMParser dp;
\r
148 Poco::AutoPtr<Poco::XML::Document> doc=dp.parseString(FixCDATA(xml));
\r
149 Poco::XML::Element *root=XMLGetFirstChild(doc,"TrustList");
\r
150 Poco::XML::Element *trustel=NULL;
\r
151 Poco::XML::Element *txt=NULL;
\r
153 std::vector<std::string> foundkeys;
\r
155 trustel=XMLGetFirstChild(root,"Trust");
\r
158 std::string identity="";
\r
159 int messagetrust=-1;
\r
160 int trustlisttrust=-1;
\r
161 std::string messagetrustcomment="";
\r
162 std::string trustlisttrustcomment="";
\r
164 txt=XMLGetFirstChild(trustel,"Identity");
\r
167 if(txt->firstChild())
\r
169 identity=SanitizeSingleString(txt->firstChild()->getNodeValue());
\r
172 txt=XMLGetFirstChild(trustel,"MessageTrustLevel");
\r
175 if(txt->firstChild())
\r
177 std::string mtl=txt->firstChild()->getNodeValue();
\r
178 StringFunctions::Convert(mtl,messagetrust);
\r
181 txt=XMLGetFirstChild(trustel,"TrustListTrustLevel");
\r
184 if(txt->firstChild())
\r
186 std::string tltl=txt->firstChild()->getNodeValue();
\r
187 StringFunctions::Convert(tltl,trustlisttrust);
\r
190 txt=XMLGetFirstChild(trustel,"MessageTrustComment");
\r
193 if(txt->firstChild())
\r
195 messagetrustcomment=SanitizeSingleString(txt->firstChild()->getNodeValue());
\r
198 txt=XMLGetFirstChild(trustel,"TrustListTrustComment");
\r
201 if(txt->firstChild())
\r
203 trustlisttrustcomment=SanitizeSingleString(txt->firstChild()->getNodeValue());
\r
207 if(identity!="" && messagetrust>=-1 && messagetrust<=100 && trustlisttrust>=-1 && trustlisttrust<=100)
\r
209 // check so we don't add the same identity multiple times from a trust list
\r
210 if(std::find(foundkeys.begin(),foundkeys.end(),identity)==foundkeys.end())
\r
212 foundkeys.push_back(identity);
\r
213 m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));
\r
218 m_log->error("TrustListXML::ParseXML malformed Trust in TrustList.xml");
\r
221 trustel=XMLGetNextSibling(trustel,"Trust");
\r
234 td.Parse(xml.c_str());
\r
238 std::string identity;
\r
239 std::string messagetruststr;
\r
240 std::string trustlisttruststr;
\r
241 std::string messagetrustcomment="";
\r
242 std::string trustlisttrustcomment="";
\r
244 long trustlisttrust;
\r
246 TiXmlHandle hnd(&td);
\r
248 std::vector<std::string> m_foundkeys;
\r
252 node=hnd.FirstChild("TrustList").FirstChild("Trust").ToElement();
\r
258 messagetrustcomment="";
\r
259 trustlisttrustcomment="";
\r
261 TiXmlHandle hnd2(node);
\r
262 txt=hnd2.FirstChild("Identity").FirstChild().ToText();
\r
265 identity=SanitizeSingleString(txt->ValueStr());
\r
267 txt=hnd2.FirstChild("MessageTrustLevel").FirstChild().ToText();
\r
270 messagetruststr=SanitizeSingleString(txt->ValueStr());
\r
271 if(messagetruststr!="")
\r
273 StringFunctions::Convert(messagetruststr,messagetrust);
\r
276 txt=hnd2.FirstChild("TrustListTrustLevel").FirstChild().ToText();
\r
279 trustlisttruststr=SanitizeSingleString(txt->ValueStr());
\r
280 if(trustlisttruststr!="")
\r
282 StringFunctions::Convert(trustlisttruststr,trustlisttrust);
\r
285 txt=hnd2.FirstChild("MessageTrustComment").FirstChild().ToText();
\r
288 messagetrustcomment=txt->ValueStr();
\r
290 txt=hnd2.FirstChild("TrustListTrustComment").FirstChild().ToText();
\r
293 trustlisttrustcomment=txt->ValueStr();
\r
296 if(identity!="" && messagetrust>=-1 && messagetrust<=100 && trustlisttrust>=-1 && trustlisttrust<=100)
\r
298 // check so we don't add the same identity multiple times from a trust list
\r
299 if(std::find(m_foundkeys.begin(),m_foundkeys.end(),identity)==m_foundkeys.end())
\r
301 m_foundkeys.push_back(identity);
\r
302 m_trust.push_back(trust(identity,messagetrust,trustlisttrust,messagetrustcomment,trustlisttrustcomment));
\r
307 m_log->WriteLog(LogFile::LOGLEVEL_ERROR,"TrustListXML::ParseXML malformed Trust in TrustList.xml");
\r
310 node=node->NextSibling("Trust");
\r