wfut 0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
ChannelParser.cpp
1// This file may be redistributed and modified only under the terms of
2// the GNU Lesser General Public License (See COPYING for details).
3// Copyright (C) 2005 - 2007 Simon Goodall
4
5
6#include "tinyxml/tinyxml.h"
7
8#include "libwfut/types.h"
9#include "libwfut/ChannelIO.h"
10
11namespace WFUT {
12
13static int parseChannel(TiXmlElement *element, ChannelObject &channel) {
14 assert(element);
15 TiXmlNode * node;
16 const char *name_val = element->Attribute(TAG_name.c_str());
17 if (name_val != NULL) {
18 channel.name = name_val;
19 }
20
21 node = element->FirstChildElement(TAG_description);
22 if (node) node = node->FirstChild();
23 if (node) channel.description = node->Value();
24
25 node = element->FirstChildElement(TAG_url);
26 if (node) node = node->FirstChild();
27 if (node) channel.url = node->Value();
28
29 node = element->FirstChildElement(TAG_email);
30 if (node) node = node->FirstChild();
31 if (node) channel.email = node->Value();
32
33 node = element->FirstChildElement(TAG_logo);
34 if (node) node = node->FirstChild();
35 if (node) channel.logo = node->Value();
36
37 return 0;
38}
39
40static int parseChannels(TiXmlNode *element, ChannelList &channels) {
41 assert(element);
42
43 TiXmlElement *e = element->FirstChildElement(TAG_channel);
44 while (e) {
45 ChannelObject channel;
46 parseChannel(e, channel);
47 channels.push_back(channel);
48 e = e->NextSiblingElement();
49 }
50 return 0;
51}
52int parseChannelList(const std::string &filename, ChannelList &channels) {
53
54 TiXmlDocument doc(filename);
55
56 if (!doc.LoadFile()) {
57 return 1;
58 }
59
60 TiXmlNode *node = doc.FirstChild(TAG_channellist);
61
62 if (!node) {
63 // missing root node
64 return 1;
65 }
66
67 return parseChannels(node, channels);
68
69}
70
71int parseChannelListXML(const std::string &xml, ChannelList &channels) {
72
73 TiXmlDocument doc;
74
75 doc.Parse(xml.c_str());
76
77 if (doc.Error()) {
78
79 // printf("TixError %s\n", doc.ErrorDesc());
80
81 return 1;
82 }
83
84 TiXmlNode *node = doc.FirstChild(TAG_channellist);
85
86 if (!node) {
87 // missing root node
88 return 1;
89 }
90
91 return parseChannels(node, channels);
92
93}
94
95} /* namespace WFUT */