1 #include "xml-template.h"
4 #include <libxml/parser.h>
8 Replace::Replace(const string &str)
11 void Replace::process(xmlNode *node, bool clean) {
12 node->children = xmlNewTextLen(reinterpret_cast<const xmlChar *>(str.data()), str.size());
15 Clone::Clone(const std::vector<Directive *> &subdirectives)
16 : subdirectives(subdirectives) {}
18 void Clone::process(xmlNode *node, bool clean) {
21 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
22 : substitution_map(substitution_map) {}
24 void Substitute::process(xmlNode *node, bool clean) {
25 for (xmlNode *child = node->children; child != NULL; child = child->next) {
26 bool processed = false;
28 if (child->type == XML_ELEMENT_NODE) {
29 // Find the ID, if any.
31 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
32 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
33 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
34 id = reinterpret_cast<const char *>(xmlNodeGetContent(attr->children));
37 if (attr->prev == NULL) {
38 child->properties = attr->next;
40 attr->prev->next = attr->next;
46 // Check all substitutions to see if we found anything appropriate.
47 for (auto it : substitution_map) {
48 if (it.first == reinterpret_cast<const char *>(child->name) ||
49 (!id.empty() && it.first == ("#" + id))) {
50 it.second->process(child, clean);
58 process(child, clean);
63 void process_file(const string &input_filename,
64 const string &output_filename,
65 Directive *root_directive)
69 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
70 root_directive->process(xmlDocGetRootElement(doc), true);
71 xmlSaveFile(output_filename.c_str(), doc);