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;
26 // Does A begin with B?
27 bool begins_with(const string &a, const string &b)
29 return (a.compare(0, b.size(), b) == 0);
34 Directive::~Directive() {}
36 string Directive::get_contents() { return ""; }
38 Replace::Replace(const string &str)
41 void Replace::process(xmlNode *node, bool clean) {
42 xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
48 string Replace::get_contents() { return str; }
50 Clone::Clone(const vector<Directive *> &subdirectives)
51 : subdirectives(subdirectives) {}
53 Clone::Clone(initializer_list<Directive *> subdirectives)
54 : subdirectives(subdirectives) {}
58 for (auto it : subdirectives) {
63 void Clone::process(xmlNode *node, bool clean)
65 // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
66 // how to clone elements.
67 vector<xmlNode *> new_nodes;
69 for (auto it : subdirectives) {
71 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
72 it->process(new_node, clean);
73 while (new_node->children != NULL) {
74 xmlNode *child = new_node->children;
76 new_nodes.push_back(child);
78 xmlFreeNode(new_node);
81 xmlFreeNodeList(node->children);
83 node->children = node->last = NULL;
85 for (auto child : new_nodes) {
86 xmlAddChild(node, child);
93 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
94 : substitution_map(substitution_map) {}
96 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
97 : substitution_map(substitution_map) {}
99 Substitute::~Substitute()
101 for (auto it : substitution_map) {
106 void Substitute::process(xmlNode *node, bool clean)
109 for (xmlNode *child = node->children; child != NULL; child = next_child) {
110 next_child = child->next;
111 bool processed = false;
113 if (child->type == XML_ELEMENT_NODE) {
114 // Find the ID, if any.
116 xmlAttr *id_attr = NULL;
117 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
118 if (attr->ns != NULL &&
119 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
120 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
121 xmlChar *id_buf = xmlNodeGetContent(attr->children);
122 id = reinterpret_cast<const char *>(id_buf);
127 if (clean && id_attr != NULL) {
128 xmlRemoveProp(id_attr);
131 // Check all substitutions to see if we found anything appropriate.
132 for (auto it : substitution_map) {
133 string tag = reinterpret_cast<const char *>(child->name);
135 // Attribute substitution.
136 if (begins_with(it.first, tag + "/")) {
137 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
138 it.first.substr(tag.size() + 1).c_str());
139 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
140 it.second->get_contents().c_str());
141 xmlSetProp(child, attr_key, attr_value);
142 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
143 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
144 it.first.substr(tag.size() + 2).c_str());
145 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
146 it.second->get_contents().c_str());
147 xmlSetProp(child, attr_key, attr_value);
154 // Regular substitution.
155 if (it.first == tag ||
156 (!id.empty() && it.first == ("#" + id))) {
157 it.second->process(child, clean);
164 process(child, clean);
172 void process_file(const string &input_filename,
173 const string &output_filename,
174 Directive *root_directive)
178 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
179 root_directive->process(xmlDocGetRootElement(doc), true);
180 xmlSaveFile(output_filename.c_str(), doc);