version 0.1.10
[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)\r
13 {\r
14         m_identities.push_back(identity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist));\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::GetPublishTrustList(const long index)\r
66 {\r
67         if(index>=0 && index<GetCount())\r
68         {\r
69                 return m_identities[index].m_publishtrustlist;\r
70         }\r
71         else\r
72         {\r
73                 return false;\r
74         }\r
75 }\r
76 \r
77 const bool IdentityExportXML::GetSingleUse(const long index)\r
78 {\r
79         if(index>=0 && index<GetCount())\r
80         {\r
81                 return m_identities[index].m_singleuse;\r
82         }\r
83         else\r
84         {\r
85                 return false;\r
86         }\r
87 }\r
88 \r
89 std::string IdentityExportXML::GetXML()\r
90 {\r
91         TiXmlDocument td;\r
92         TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
93         TiXmlElement *tid;\r
94         TiXmlPrinter tp;\r
95 \r
96         td.LinkEndChild(tdec);\r
97         tid=new TiXmlElement("IdentityExport");\r
98         td.LinkEndChild(tid);\r
99 \r
100         for(std::vector<identity>::iterator i=m_identities.begin(); i!=m_identities.end(); i++)\r
101         {\r
102                 TiXmlElement *tr=new TiXmlElement("Identity");\r
103                 tid->LinkEndChild(tr);\r
104                 tr->LinkEndChild(XMLCreateCDATAElement("Name",(*i).m_name));\r
105                 tr->LinkEndChild(XMLCreateTextElement("PublicKey",(*i).m_publickey));\r
106                 tr->LinkEndChild(XMLCreateTextElement("PrivateKey",(*i).m_privatekey));\r
107                 tr->LinkEndChild(XMLCreateBooleanElement("SingleUse",(*i).m_singleuse));\r
108                 tr->LinkEndChild(XMLCreateBooleanElement("PublishTrustList",(*i).m_publishtrustlist));\r
109                 tr->LinkEndChild(XMLCreateBooleanElement("PublishBoardList",(*i).m_publishboardlist));\r
110         }\r
111 \r
112         td.Accept(&tp);\r
113         return std::string(tp.CStr());\r
114 }\r
115 \r
116 void IdentityExportXML::Initialize()\r
117 {\r
118         m_identities.clear();\r
119 }\r
120 \r
121 const bool IdentityExportXML::ParseXML(const std::string &xml)\r
122 {\r
123         TiXmlDocument td;\r
124         td.Parse(xml.c_str());\r
125 \r
126         if(!td.Error())\r
127         {\r
128                 std::string name;\r
129                 std::string publickey;\r
130                 std::string privatekey;\r
131                 bool singleuse=false;\r
132                 bool publishtrustlist=false;\r
133                 bool publishboardlist=false;\r
134                 TiXmlText *txt;\r
135                 TiXmlHandle hnd(&td);\r
136                 TiXmlNode *node;\r
137 \r
138                 Initialize();\r
139 \r
140                 node=hnd.FirstChild("IdentityExport").FirstChild("Identity").ToElement();\r
141                 while(node)\r
142                 {\r
143                         name="";\r
144                         publickey="";\r
145                         privatekey="";\r
146                         singleuse=false;\r
147                         publishtrustlist=false;\r
148                         publishboardlist=false;\r
149 \r
150                         TiXmlHandle hnd2(node);\r
151                         txt=hnd2.FirstChild("Name").FirstChild().ToText();\r
152                         if(txt)\r
153                         {\r
154                                 name=txt->ValueStr();\r
155                         }\r
156                         txt=hnd2.FirstChild("PublicKey").FirstChild().ToText();\r
157                         if(txt)\r
158                         {\r
159                                 publickey=txt->ValueStr();\r
160                         }\r
161                         txt=hnd2.FirstChild("PrivateKey").FirstChild().ToText();\r
162                         if(txt)\r
163                         {\r
164                                 privatekey=txt->ValueStr();\r
165                         }\r
166 \r
167                         singleuse=XMLGetBooleanElement(node->ToElement(),"SingleUse");\r
168                         publishtrustlist=XMLGetBooleanElement(node->ToElement(),"PublishTrustList");\r
169                         publishboardlist=XMLGetBooleanElement(node->ToElement(),"PublishBoardList");\r
170 \r
171                         if(name!="" && publickey!="" && privatekey!="")\r
172                         {\r
173                                 m_identities.push_back(identity(name,publickey,privatekey,singleuse,publishtrustlist,publishboardlist));\r
174                         }\r
175                         \r
176                         node=node->NextSibling("Identity");\r
177                 }\r
178                 return true;\r
179 \r
180         }\r
181         else\r
182         {\r
183                 return false;\r
184         }\r
185 }