version 0.3.0
[fms.git] / src / freenet / boardlistxml.cpp
1 #include "../../include/freenet/boardlistxml.h"\r
2 \r
3 #ifdef XMEM\r
4         #include <xmem.h>\r
5 #endif\r
6 \r
7 BoardListXML::BoardListXML()\r
8 {\r
9         Initialize();\r
10 }\r
11 \r
12 void BoardListXML::AddBoard(const std::string &name, const std::string &description)\r
13 {\r
14         if(name!="" && description!="")\r
15         {\r
16                 std::string lowername=name;\r
17                 StringFunctions::LowerCase(lowername,lowername);\r
18                 if(lowername.size()>40)\r
19                 {\r
20                         lowername.erase(40);\r
21                 }\r
22                 m_boards.push_back(board(lowername,description));\r
23         }\r
24 }\r
25 \r
26 const std::string BoardListXML::GetDescription(const long index)\r
27 {\r
28         if(index>=0 && index<GetCount())\r
29         {\r
30                 return m_boards[index].m_description;\r
31         }\r
32         else\r
33         {\r
34                 return "";\r
35         }\r
36 }\r
37 \r
38 const std::string BoardListXML::GetName(const long index)\r
39 {\r
40         if(index>=0 && index<GetCount())\r
41         {\r
42                 return m_boards[index].m_name;\r
43         }\r
44         else\r
45         {\r
46                 return "";\r
47         }\r
48 }\r
49 \r
50 std::string BoardListXML::GetXML()\r
51 {\r
52         Poco::AutoPtr<Poco::XML::Document> doc=new Poco::XML::Document;\r
53         //TiXmlDocument td;\r
54         //TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
55         Poco::XML::Element *root=doc->createElement("BoardList");\r
56         //TiXmlElement *tid;\r
57         //TiXmlPrinter tp;\r
58 \r
59         doc->appendChild(root);\r
60         //td.LinkEndChild(tdec);\r
61         //tid=new TiXmlElement("BoardList");\r
62         //td.LinkEndChild(tid);\r
63 \r
64         for(std::vector<board>::iterator i=m_boards.begin(); i!=m_boards.end(); i++)\r
65         {\r
66                 std::string boardname=(*i).m_name;\r
67                 StringFunctions::LowerCase(boardname,boardname);\r
68                 Poco::XML::Element *tr=doc->createElement("Board");\r
69                 //TiXmlElement *tr=new TiXmlElement("Board");\r
70                 root->appendChild(tr);\r
71                 //tid->LinkEndChild(tr);\r
72                 tr->appendChild(XMLCreateCDATAElement(doc,"Name",boardname));\r
73                 //tr->LinkEndChild(XMLCreateCDATAElement("Name",boardname));\r
74                 tr->appendChild(XMLCreateCDATAElement(doc,"Description",(*i).m_description));\r
75                 //tr->LinkEndChild(XMLCreateCDATAElement("Description",(*i).m_description));\r
76         }\r
77 \r
78         //td.Accept(&tp);\r
79         //return std::string(tp.CStr());\r
80         return GenerateXML(doc);\r
81 }\r
82 \r
83 void BoardListXML::Initialize()\r
84 {\r
85         m_boards.clear();\r
86 }\r
87 \r
88 const bool BoardListXML::ParseXML(const std::string &xml)\r
89 {\r
90         bool parsed=false;\r
91         Poco::XML::DOMParser dp;\r
92 \r
93         Initialize();\r
94 \r
95         try\r
96         {\r
97                 Poco::AutoPtr<Poco::XML::Document> doc=dp.parseString(FixCDATA(xml));\r
98                 Poco::XML::Element *root=XMLGetFirstChild(doc,"BoardList");\r
99                 Poco::XML::Element *boardel=NULL;\r
100 \r
101                 boardel=XMLGetFirstChild(root,"Board");\r
102                 while(boardel)\r
103                 {\r
104                         std::string name="";\r
105                         std::string description="";\r
106 \r
107                         Poco::XML::Element *txt=XMLGetFirstChild(boardel,"Name");\r
108                         if(txt && txt->firstChild())\r
109                         {\r
110                                 name=SanitizeSingleString(txt->firstChild()->getNodeValue());\r
111                                 StringFunctions::LowerCase(name,name);\r
112                                 if(name.size()>40)\r
113                                 {\r
114                                         name.erase(40);\r
115                                 }\r
116                         }\r
117                         txt=XMLGetFirstChild(boardel,"Description");\r
118                         if(txt && txt->firstChild())\r
119                         {\r
120                                 description=SanitizeSingleString(txt->firstChild()->getNodeValue());\r
121                         }\r
122 \r
123                         if(name!="" && description!="")\r
124                         {\r
125                                 m_boards.push_back(board(name,description));\r
126                         }\r
127 \r
128                         boardel=XMLGetNextSibling(boardel,"Board");\r
129                 }\r
130 \r
131                 parsed=true;\r
132 \r
133         }\r
134         catch(...)\r
135         {\r
136         }\r
137 \r
138         return parsed;\r
139 \r
140         /*\r
141         std::string name;\r
142         std::string description;\r
143         TiXmlDocument td;\r
144         td.Parse(xml.c_str());\r
145 \r
146         if(!td.Error())\r
147         {\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("BoardList").FirstChild("Board").ToElement();\r
155                 while(node)\r
156                 {\r
157                         name="";\r
158                         description="";\r
159 \r
160                         TiXmlHandle hnd2(node);\r
161                         txt=hnd2.FirstChild("Name").FirstChild().ToText();\r
162                         if(txt)\r
163                         {\r
164                                 name=txt->ValueStr();\r
165                                 StringFunctions::LowerCase(name,name);\r
166                                 if(name.size()>40)\r
167                                 {\r
168                                         name.erase(40);\r
169                                 }\r
170                         }\r
171                         txt=hnd2.FirstChild("Description").FirstChild().ToText();\r
172                         if(txt)\r
173                         {\r
174                                 description=txt->ValueStr();\r
175                         }\r
176 \r
177                         if(name!="" && description!="")\r
178                         {\r
179                                 m_boards.push_back(board(name,description));\r
180                         }\r
181                         \r
182                         node=node->NextSibling("Board");\r
183                 }\r
184                 return true;\r
185 \r
186         }\r
187         else\r
188         {\r
189                 return false;\r
190         }\r
191         */\r
192 }\r