1 #include "xml-template.h"
4 #include <libxml/parser.h>
10 void clean_node(xmlNode *node)
12 if (node->type != XML_ELEMENT_NODE) {
15 if (strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
16 xmlNode *frag = xmlNewDocFragment(node->doc);
17 xmlReplaceNode(node, frag);
18 frag->children = node->children;
19 frag->last = node->last;
21 node->children = node->last = NULL;
28 Directive::~Directive() {}
30 Replace::Replace(const string &str)
33 void Replace::process(xmlNode *node, bool clean) {
34 xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
40 Clone::Clone(const vector<Directive *> &subdirectives)
41 : subdirectives(subdirectives) {}
43 Clone::Clone(initializer_list<Directive *> subdirectives)
44 : subdirectives(subdirectives) {}
48 for (auto it : subdirectives) {
53 void Clone::process(xmlNode *node, bool clean)
55 // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
56 // how to clone elements.
57 vector<xmlNode *> new_nodes;
59 for (auto it : subdirectives) {
61 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
62 it->process(new_node, clean);
63 while (new_node->children != NULL) {
64 xmlNode *child = new_node->children;
66 new_nodes.push_back(child);
68 xmlFreeNode(new_node);
71 xmlFreeNodeList(node->children);
73 node->children = node->last = NULL;
75 for (auto child : new_nodes) {
76 xmlAddChild(node, child);
83 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
84 : substitution_map(substitution_map) {}
86 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
87 : substitution_map(substitution_map) {}
89 Substitute::~Substitute()
91 for (auto it : substitution_map) {
96 void Substitute::process(xmlNode *node, bool clean)
99 for (xmlNode *child = node->children; child != NULL; child = next_child) {
100 next_child = child->next;
101 bool processed = false;
103 if (child->type == XML_ELEMENT_NODE) {
104 // Find the ID, if any.
106 xmlAttr *id_attr = NULL;
107 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
108 if (strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
109 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
110 xmlChar *id_buf = xmlNodeGetContent(attr->children);
111 id = reinterpret_cast<const char *>(id_buf);
116 if (clean && id_attr != NULL) {
117 xmlRemoveProp(id_attr);
120 // Check all substitutions to see if we found anything appropriate.
121 for (auto it : substitution_map) {
122 if (it.first == reinterpret_cast<const char *>(child->name) ||
123 (!id.empty() && it.first == ("#" + id))) {
124 it.second->process(child, clean);
132 process(child, clean);
140 void process_file(const string &input_filename,
141 const string &output_filename,
142 Directive *root_directive)
146 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
147 root_directive->process(xmlDocGetRootElement(doc), true);
148 xmlSaveFile(output_filename.c_str(), doc);