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
|
/**
* Inspired by: http://dbus-cplusplus.sourceforge.net/
*/
#include "xml.h"
#include <expat.h>
using namespace sdbuscpp::xml;
std::istream &operator >> (std::istream& in, Document& doc)
{
std::stringbuf xmlbuf;
in.get(xmlbuf, '\0');
doc.from_xml(xmlbuf.str());
return in;
}
std::ostream &operator << (std::ostream& out, const Document& doc)
{
return out << doc.to_xml();
}
Error::Error(const char *error, int line, int column)
{
std::ostringstream estream;
estream << "line " << line << ", column " << column << ": " << error;
m_error = estream.str();
}
Node::Node(const char *n, const char **a)
: name(n)
{
if (a)
{
for (int i = 0; a[i]; i += 2)
{
m_attrs[a[i]] = a[i + 1];
}
}
}
Nodes Nodes::operator[](const std::string& key) const
{
Nodes result;
for (auto it = begin(), endIt = end(); it != endIt; ++it)
{
Nodes part = (**it)[key];
result.insert(result.end(), part.begin(), part.end());
}
return result;
}
Nodes Nodes::select(const std::string& attr, const std::string& value) const
{
Nodes result;
for (auto it = begin(), itEnd = end(); it != itEnd; ++it)
{
if ((*it)->get(attr) == value)
{
result.insert(result.end(), *it);
}
}
return result;
}
Nodes Node::operator[](const std::string& key)
{
Nodes result;
if (key.length() == 0)
{
return result;
}
for (auto it = children.begin(), itEnd = children.end(); it != itEnd; ++it)
{
if (it->name == key)
{
result.push_back(&(*it));
}
}
return result;
}
std::string Node::get(const std::string& attribute) const
{
const auto it = m_attrs.find(attribute);
if (it != m_attrs.end())
{
return it->second;
}
return "";
}
void Node::set(const std::string& attribute, std::string value)
{
if (value.length())
{
m_attrs[attribute] = value;
}
else
{
m_attrs.erase(value);
}
}
std::string Node::to_xml() const
{
std::string xml;
int depth = 0;
_raw_xml(xml, depth);
return xml;
}
void Node::_raw_xml(std::string& xml, int& depth) const
{
xml.append(depth * 2, ' ');
xml.append("<" + name);
for (auto it = m_attrs.begin(), itEnd = m_attrs.end(); it != itEnd; ++it)
{
xml.append(" " + it->first + "=\"" + it->second + "\"");
}
if (cdata.length() == 0 && children.size() == 0)
{
xml.append("/>\n");
}
else
{
xml.append(">");
if (cdata.length())
{
xml.append(cdata);
}
if (children.size())
{
xml.append("\n");
++depth;
for (auto it = children.begin(), itEnd = children.end(); it != itEnd; ++it)
{
it->_raw_xml(xml, depth);
}
--depth;
xml.append(depth * 2, ' ');
}
xml.append("</" + name + ">\n");
}
}
Document::Document() :
root(nullptr),
m_depth(0)
{
}
Document::Document(const std::string &xml) :
root(nullptr),
m_depth(0)
{
from_xml(xml);
}
Document::~Document()
{
delete root;
}
struct Document::Expat
{
static void start_doctype_decl_handler(
void *data, const XML_Char *name, const XML_Char *sysid, const XML_Char *pubid, int has_internal_subset
);
static void end_doctype_decl_handler(void *data);
static void start_element_handler(void *data, const XML_Char *name, const XML_Char **atts);
static void character_data_handler(void *data, const XML_Char *chars, int len);
static void end_element_handler(void *data, const XML_Char *name);
};
void Document::from_xml(const std::string& xml)
{
m_depth = 0;
if (root)
{
delete root;
}
root = nullptr;
XML_Parser parser = XML_ParserCreate("UTF-8");
XML_SetUserData(parser, this);
XML_SetDoctypeDeclHandler(
parser,
Document::Expat::start_doctype_decl_handler,
Document::Expat::end_doctype_decl_handler
);
XML_SetElementHandler(
parser,
Document::Expat::start_element_handler,
Document::Expat::end_element_handler
);
XML_SetCharacterDataHandler(
parser,
Document::Expat::character_data_handler
);
XML_Status status = XML_Parse(parser, xml.c_str(), xml.length(), true);
if (status == XML_STATUS_ERROR)
{
const char* error = XML_ErrorString(XML_GetErrorCode(parser));
int line = XML_GetCurrentLineNumber(parser);
int column = XML_GetCurrentColumnNumber(parser);
XML_ParserFree(parser);
throw Error(error, line, column);
}
else
{
XML_ParserFree(parser);
}
}
std::string Document::to_xml() const
{
return root->to_xml();
}
void Document::Expat::start_doctype_decl_handler(
void* /*data*/,
const XML_Char* /*name*/,
const XML_Char* /*sysid*/,
const XML_Char */*pubid*/,
int /*has_internal_subset*/)
{
}
void Document::Expat::end_doctype_decl_handler(void* /*data*/)
{
}
void Document::Expat::start_element_handler(void* data, const XML_Char* name, const XML_Char** atts)
{
Document* doc = static_cast<Document*>(data);
if (!doc->root)
{
doc->root = new Node(name, atts);
}
else
{
Node::Children* cld = &(doc->root->children);
for (int i = 1; i < doc->m_depth; ++i)
{
cld = &(cld->back().children);
}
cld->push_back(Node(name, atts));
}
doc->m_depth++;
}
void Document::Expat::character_data_handler(void* data, const XML_Char* chars, int len)
{
Document* doc = static_cast<Document*>(data);
Node* nod = doc->root;
for (int i = 1; i < doc->m_depth; ++i)
{
nod = &(nod->children.back());
}
int x = 0, y = len - 1;
while (isspace(chars[y]) && y > 0) --y;
while (isspace(chars[x]) && x < y) ++x;
nod->cdata = std::string(chars, x, y + 1);
}
void Document::Expat::end_element_handler(void* data, const XML_Char* /*name*/)
{
Document* doc = static_cast<Document*>(data);
doc->m_depth--;
}
|