summaryrefslogtreecommitdiffstats
path: root/src/Common/XmlData.cpp
blob: 956117f673589517c8384ae0d35356b27eb697d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * XmlData.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "XmlData.h"
#include "Base64Encoder.h"

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>

namespace Mad {
namespace Common {

void XmlData::Element::updateStr() {
  Core::String typeStr;

  if(type == NONE) {
    str = "";
  }
  else if(type == BINARY) {
    str = Base64Encoder::encode(boost::get<std::vector<boost::uint8_t> >(value)).c_str();
    typeStr = "binary";
  }
  else if(type == TIME) {
    str = Core::String::fromString(boost::posix_time::to_iso_string(boost::get<boost::posix_time::ptime>(value)));
    typeStr = "time";
  }
  else if(type != STRING) {
    std::ostringstream buf;

    switch(type) {
      case INT:
        buf << boost::get<long>(value);
        typeStr = "int";
        break;
      case UINT:
        buf << boost::get<unsigned long>(value);
        typeStr = "uint";
        break;
      case INT64:
        buf << boost::get<long long>(value);
        typeStr = "int64";
        break;
      case UINT64:
        buf << boost::get<unsigned long long>(value);
        typeStr = "uint64";
        break;
      case FLOAT:
        buf << boost::get<float>(value);
        typeStr = "float";
        break;
      case DOUBLE:
        buf << boost::get<double>(value);
        typeStr = "double";
        break;
      case LONGDOUBLE:
        buf << boost::get<long double>(value);
        typeStr = "longdouble";
      case BINARY:
      case STRING:
      case TIME:
      case NONE:
        break;
    }

    str = buf.str().c_str();
  }
  else
    typeStr = "string";

  xmlNodePtr newNode = xmlNewText((xmlChar*)str.toUTF8().c_str());
  xmlNodePtr oldNode = elementNode->children;

  xmlReplaceNode(oldNode, newNode);
  xmlFreeNode(oldNode);

  xmlSetProp(elementNode, (xmlChar*)"type", (xmlChar*)typeStr.toUTF8().c_str());
}

XmlData::Element::Element(xmlNodePtr node) : elementNode(node), type(NONE) {
  if(!node)
    return;

  xmlChar *typestr = xmlGetProp(node, (xmlChar*)"type");
  if(!typestr)
    return;

  xmlChar *content = xmlNodeGetContent(node->children);
  if(!content) {
    xmlFree(typestr);
    return;
  }

  str = Core::String::fromUTF8((char*)content);
  std::istringstream buf(str.toString());

  if(!xmlStrcmp(typestr, (xmlChar*)"binary")) {
    type = BINARY;

    value = Base64Encoder::decode(str.toString());
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"time")) {
    type = TIME;

    try {
      value = boost::posix_time::from_iso_string(str.toString());
    }
    catch(...) {
      value = boost::posix_time::ptime(boost::posix_time::not_a_date_time);
    }
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"int")) {
    type = INT;

    long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"uint")) {
    type = UINT;

    unsigned long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"int64")) {
    type = INT64;
    
    long long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"uint64")) {
    type = UINT64;

    unsigned long long tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"float")) {
    type = FLOAT;

    float tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"double")) {
    type = DOUBLE;
    
    double tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"longdouble")) {
    type = LONGDOUBLE;
    
    long double tmp;
    buf >> tmp;
    value = tmp;
  }
  else if(!xmlStrcmp(typestr, (xmlChar*)"string")) {
    type = STRING;
  }

  xmlFree(typestr);
  xmlFree(content);

  if(type != NONE)
    updateStr();
}

XmlData::Entry::Entry(xmlNodePtr node) : entryNode(node) {
  if(!node)
    return;

  for(xmlNodePtr element = node->children; element != 0; element = element->next) {
    if(element->type != XML_ELEMENT_NODE)
      continue;

    xmlChar *name = xmlGetProp(element, (xmlChar*)"name");

    if(!xmlStrcmp(element->name, (xmlChar*)"list"))
      lists.insert(std::make_pair(Core::String::fromUTF8((char*)name), new List(element)));
    else if(!xmlStrcmp(element->name, (xmlChar*)"value"))
      elements.insert(std::make_pair(Core::String::fromUTF8((char*)name), new Element(element)));

    xmlFree(name);
  }
}



template <>
Core::String XmlData::Entry::get<Core::String>(const Core::String &name) const {
  Element *element = getElement(name);
  if(!element)
    return Core::String();

  return element->str;
}

template <>
const Core::String& XmlData::Entry::get<const Core::String&>(const Core::String &name) const {
  static const Core::String empty;

  Element *element = getElement(name);
  if(!element)
    return empty;

  return element->str;
}

template <>
std::vector<boost::uint8_t> XmlData::Entry::get<std::vector<unsigned char> >(const Core::String &name) const {
  Element *element = getElement(name);
  if(!element)
    return std::vector<boost::uint8_t>();

    switch(element->type) {
      case Element::BINARY:
        return boost::get<std::vector<boost::uint8_t> >(element->value);
      default:
        return std::vector<boost::uint8_t>();
    }
}

template <>
const std::vector<boost::uint8_t>& XmlData::Entry::get<const std::vector<unsigned char>&>(const Core::String &name) const {
  static const std::vector<boost::uint8_t> empty;

  Element *element = getElement(name);
  if(!element)
    return empty;

    switch(element->type) {
      case Element::BINARY:
        return boost::get<std::vector<boost::uint8_t> >(element->value);
      default:
        return empty;
    }
}

template <>
boost::posix_time::ptime XmlData::Entry::get<boost::posix_time::ptime>(const Core::String &name) const {
  Element *element = getElement(name);
  if(!element)
    return boost::posix_time::ptime(boost::posix_time::not_a_date_time);

  switch(element->type) {
    case Element::TIME:
      return boost::get<boost::posix_time::ptime>(element->value);
    default:
      return boost::posix_time::ptime(boost::posix_time::not_a_date_time);
  }
}

XmlData::List::List(xmlNodePtr node) : elementNode(node) {
  for(xmlNodePtr entry = node->children; entry != 0; entry = entry->next) {
    if(entry->type != XML_ELEMENT_NODE || xmlStrcmp(entry->name, (xmlChar*)"entry"))
      continue;

    entries.push_back(new Entry(entry));
  }
}

XmlData::XmlData() {
  doc = xmlNewDoc((xmlChar*)"1.0");

  rootNode = xmlNewNode(0, (xmlChar*)"mad");
  xmlDocSetRootElement(doc, rootNode);

  entry = new Entry(rootNode);
}

XmlData::XmlData(const XmlData &o) {
  doc = xmlCopyDoc(o.doc, 1);
  rootNode = xmlDocGetRootElement(doc);
  entry = new Entry(rootNode);
}

XmlData::XmlData(const Net::Packet &packet) {
  doc = xmlParseMemory((const char*)packet.getData(), packet.getLength());
  rootNode = xmlDocGetRootElement(doc);
  entry = new Entry(rootNode);
}

XmlData::XmlData(const std::string &filename) throw (Core::Exception) {
  doc = xmlParseFile(filename.c_str());
  if(!doc)
    throw Core::Exception(Core::Exception::NOT_AVAILABLE);

  rootNode = xmlDocGetRootElement(doc);
  entry = new Entry(rootNode);
}

XmlData& XmlData::operator=(const XmlData &o) {
  delete entry;
  xmlFreeDoc(doc);

  doc = xmlCopyDoc(o.doc, 1);
  rootNode = xmlDocGetRootElement(doc);
  entry = new Entry(rootNode);

  return *this;
}

Core::String XmlData::getType() const {
  xmlChar *type = xmlGetProp(rootNode, (xmlChar*)"type");

  Core::String ret(type ? Core::String::fromUTF8((char*)type) : "");

  xmlFree(type);

  return ret;
}

void XmlData::setType(const Core::String &type) {
  xmlSetProp(rootNode, (xmlChar*)"type", (xmlChar*)type.toUTF8().c_str());
}

Net::Packet XmlData::toPacket(boost::uint16_t requestId) const {
  xmlChar *buf;
  int length;

  xmlDocDumpMemory(doc, &buf, &length);

  Net::Packet packet(requestId, buf, length);

  xmlFree(buf);

  return packet;
}

void XmlData::toFile(const std::string &filename) const throw (Core::Exception) {
  std::FILE *file = std::fopen(filename.c_str(), "w");
  if(!file)
    throw Core::Exception(Core::Exception::INTERNAL_ERRNO, errno);

  if(!xmlDocDump(file, doc))
    throw Core::Exception(Core::Exception::NOT_AVAILABLE);

  std::fclose(file);
}

}
}