version 0.2.4
[fms.git] / src / http / identityexportxml.cpp
1 #include "../../include/http/identityexportxml.h"\r
2 \r
3 #ifdef XMEM\r
4         #include <xmem.h>\r
5 #endif\r
6 \r
7 IdentityExportXML::IdentityExportXML()\r
8 {\r
9         Initialize();\r
10 }\r
11 \r
12 void IdentityExportXML::AddIdentity(const std::string &name, const std::string &publickey, const std::string &privatekey, const bool singleuse, const bool publishtrustlist, const bool publishboardlist, const bool publishfreesite)\r
13 {\r
14         m_identities.push_back(identity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist,publishfreesite));\r
15 }\r
16 \r
17 const std::string IdentityExportXML::GetName(const long index)\r
18 {\r
19         if(index>=0 && index<GetCount())\r
20         {\r
21                 return m_identities[index].m_name;\r
22         }\r
23         else\r
24         {\r
25                 return "";\r
26         }\r
27 }\r
28 \r
29 const std::string IdentityExportXML::GetPrivateKey(const long index)\r
30 {\r
31         if(index>=0 && index<GetCount())\r
32         {\r
33                 return m_identities[index].m_privatekey;\r
34         }\r
35         else\r
36         {\r
37                 return "";\r
38         }\r
39 }\r
40 \r
41 const std::string IdentityExportXML::GetPublicKey(const long index)\r
42 {\r
43         if(index>=0 && index<GetCount())\r
44         {\r
45                 return m_identities[index].m_publickey;\r
46         }\r
47         else\r
48         {\r
49                 return "";\r
50         }\r
51 }\r
52 \r
53 const bool IdentityExportXML::GetPublishBoardList(const long index)\r
54 {\r
55         if(index>=0 && index<GetCount())\r
56         {\r
57                 return m_identities[index].m_publishboardlist;\r
58         }\r
59         else\r
60         {\r
61                 return false;\r
62         }\r
63 }\r
64 \r
65 const bool IdentityExportXML::GetPublishFreesite(const long index)\r
66 {\r
67         if(index>=0 && index<GetCount())\r
68         {\r
69                 return m_identities[index].m_publishfreesite;\r
70         }\r
71         else\r
72         {\r
73                 return false;\r
74         }\r
75 }\r
76 \r
77 const bool IdentityExportXML::GetPublishTrustList(const long index)\r
78 {\r
79         if(index>=0 && index<GetCount())\r
80         {\r
81                 return m_identities[index].m_publishtrustlist;\r
82         }\r
83         else\r
84         {\r
85                 return false;\r
86         }\r
87 }\r
88 \r
89 const bool IdentityExportXML::GetSingleUse(const long index)\r
90 {\r
91         if(index>=0 && index<GetCount())\r
92         {\r
93                 return m_identities[index].m_singleuse;\r
94         }\r
95         else\r
96         {\r
97                 return false;\r
98         }\r
99 }\r
100 \r
101 std::string IdentityExportXML::GetXML()\r
102 {\r
103         TiXmlDocument td;\r
104         TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
105         TiXmlElement *tid;\r
106         TiXmlPrinter tp;\r
107 \r
108         td.LinkEndChild(tdec);\r
109         tid=new TiXmlElement("IdentityExport");\r
110         td.LinkEndChild(tid);\r
111 \r
112         for(std::vector<identity>::iterator i=m_identities.begin(); i!=m_identities.end(); i++)\r
113         {\r
114                 TiXmlElement *tr=new TiXmlElement("Identity");\r
115                 tid->LinkEndChild(tr);\r
116                 tr->LinkEndChild(XMLCreateCDATAElement("Name",(*i).m_name));\r
117                 tr->LinkEndChild(XMLCreateTextElement("PublicKey",(*i).m_publickey));\r
118                 tr->LinkEndChild(XMLCreateTextElement("PrivateKey",(*i).m_privatekey));\r
119                 tr->LinkEndChild(XMLCreateBooleanElement("SingleUse",(*i).m_singleuse));\r
120                 tr->LinkEndChild(XMLCreateBooleanElement("PublishTrustList",(*i).m_publishtrustlist));\r
121                 tr->LinkEndChild(XMLCreateBooleanElement("PublishBoardList",(*i).m_publishboardlist));\r
122                 tr->LinkEndChild(XMLCreateBooleanElement("PublishFreesite",(*i).m_publishfreesite));\r
123         }\r
124 \r
125         td.Accept(&tp);\r
126         return std::string(tp.CStr());\r
127 }\r
128 \r
129 void IdentityExportXML::Initialize()\r
130 {\r
131         m_identities.clear();\r
132 }\r
133 \r
134 const bool IdentityExportXML::ParseXML(const std::string &xml)\r
135 {\r
136         TiXmlDocument td;\r
137         td.Parse(xml.c_str());\r
138 \r
139         if(!td.Error())\r
140         {\r
141                 std::string name;\r
142                 std::string publickey;\r
143                 std::string privatekey;\r
144                 bool singleuse=false;\r
145                 bool publishtrustlist=false;\r
146                 bool publishboardlist=false;\r
147                 bool publishfreesite=false;\r
148                 TiXmlText *txt;\r
149                 TiXmlHandle hnd(&td);\r
150                 TiXmlNode *node;\r
151 \r
152                 Initialize();\r
153 \r
154                 node=hnd.FirstChild("IdentityExport").FirstChild("Identity").ToElement();\r
155                 while(node)\r
156                 {\r
157                         name="";\r
158                         publickey="";\r
159                         privatekey="";\r
160                         singleuse=false;\r
161                         publishtrustlist=false;\r
162                         publishboardlist=false;\r
163                         publishfreesite=false;\r
164 \r
165                         TiXmlHandle hnd2(node);\r
166                         txt=hnd2.FirstChild("Name").FirstChild().ToText();\r
167                         if(txt)\r
168                         {\r
169                                 name=txt->ValueStr();\r
170                         }\r
171                         txt=hnd2.FirstChild("PublicKey").FirstChild().ToText();\r
172                         if(txt)\r
173                         {\r
174                                 publickey=txt->ValueStr();\r
175                         }\r
176                         txt=hnd2.FirstChild("PrivateKey").FirstChild().ToText();\r
177                         if(txt)\r
178                         {\r
179                                 privatekey=txt->ValueStr();\r
180                         }\r
181 \r
182                         singleuse=XMLGetBooleanElement(node->ToElement(),"SingleUse");\r
183                         publishtrustlist=XMLGetBooleanElement(node->ToElement(),"PublishTrustList");\r
184                         publishboardlist=XMLGetBooleanElement(node->ToElement(),"PublishBoardList");\r
185                         publishfreesite=XMLGetBooleanElement(node->ToElement(),"PublishFreesite");\r
186 \r
187                         if(name!="" && publickey!="" && privatekey!="")\r
188                         {\r
189                                 m_identities.push_back(identity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist,publishfreesite));\r
190                         }\r
191                         \r
192                         node=node->NextSibling("Identity");\r
193                 }\r
194                 return true;\r
195 \r
196         }\r
197         else\r
198         {\r
199                 return false;\r
200         }\r
201 }\r