version 0.3.2
[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         Poco::AutoPtr<Poco::XML::Document> doc=new Poco::XML::Document;\r
104         Poco::AutoPtr<Poco::XML::Element> root=doc->createElement("IdentityExport");\r
105         Poco::AutoPtr<Poco::XML::Element> el=NULL;\r
106 \r
107         doc->appendChild(root);\r
108 \r
109         for(std::vector<identity>::iterator i=m_identities.begin(); i!=m_identities.end(); i++)\r
110         {\r
111                 el=doc->createElement("Identity");\r
112                 root->appendChild(el);\r
113 \r
114                 el->appendChild(XMLCreateCDATAElement(doc,"Name",(*i).m_name));\r
115                 el->appendChild(XMLCreateTextElement(doc,"PublicKey",(*i).m_publickey));\r
116                 el->appendChild(XMLCreateTextElement(doc,"PrivateKey",(*i).m_privatekey));\r
117                 el->appendChild(XMLCreateBooleanElement(doc,"SingleUse",(*i).m_singleuse));\r
118                 el->appendChild(XMLCreateBooleanElement(doc,"PublishTrustList",(*i).m_publishtrustlist));\r
119                 el->appendChild(XMLCreateBooleanElement(doc,"PublishBoardList",(*i).m_publishboardlist));\r
120                 el->appendChild(XMLCreateBooleanElement(doc,"PublishFreesite",(*i).m_publishfreesite));\r
121         }\r
122 \r
123         return GenerateXML(doc);\r
124 }\r
125 \r
126 void IdentityExportXML::Initialize()\r
127 {\r
128         m_identities.clear();\r
129 }\r
130 \r
131 const bool IdentityExportXML::ParseXML(const std::string &xml)\r
132 {\r
133         bool parsed=false;\r
134         Poco::XML::DOMParser dp;\r
135 \r
136         Initialize();\r
137 \r
138         try\r
139         {\r
140                 Poco::AutoPtr<Poco::XML::Document> doc=dp.parseString(FixCDATA(xml));\r
141                 Poco::XML::Element *root=XMLGetFirstChild(doc,"IdentityExport");\r
142                 Poco::XML::Element *node=XMLGetFirstChild(root,"Identity");\r
143 \r
144                 while(node)\r
145                 {\r
146                         std::string name="";\r
147                         std::string publickey="";\r
148                         std::string privatekey="";\r
149                         bool singleuse=false;\r
150                         bool publishtrustlist=false;\r
151                         bool publishboardlist=false;\r
152                         bool publishfreesite=false;     \r
153 \r
154                         Poco::XML::Element *text=XMLGetFirstChild(node,"Name");\r
155                         if(text)\r
156                         {\r
157                                 if(text->firstChild())\r
158                                 {\r
159                                         std::string asdf=text->innerText();\r
160                                         asdf=text->firstChild()->innerText();\r
161                                         name=text->firstChild()->getNodeValue();\r
162                                 }\r
163                         }\r
164                         text=XMLGetFirstChild(node,"PublicKey");\r
165                         if(text)\r
166                         {\r
167                                 if(text->firstChild())\r
168                                 {\r
169                                         publickey=text->firstChild()->getNodeValue();\r
170                                 }\r
171                         }\r
172                         text=XMLGetFirstChild(node,"PrivateKey");\r
173                         if(text)\r
174                         {\r
175                                 if(text->firstChild())\r
176                                 {\r
177                                         privatekey=text->firstChild()->getNodeValue();\r
178                                 }\r
179                         }\r
180 \r
181                         singleuse=XMLGetBooleanElement(node,"SingleUse");\r
182                         publishtrustlist=XMLGetBooleanElement(node,"PublishTrustList");\r
183                         publishboardlist=XMLGetBooleanElement(node,"PublishBoardList");\r
184                         publishfreesite=XMLGetBooleanElement(node,"PublishFreesite");\r
185 \r
186                         if(name!="" && publickey!="" && privatekey!="")\r
187                         {\r
188                                 m_identities.push_back(identity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist,publishfreesite));\r
189                         }\r
190 \r
191                         node=XMLGetNextSibling(node,"Identity");\r
192                 }\r
193 \r
194                 parsed=true;\r
195         }\r
196         catch(...)\r
197         {\r
198         }\r
199 \r
200         return parsed;\r
201 }\r