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 (node->ns != NULL &&
16 strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
17 xmlNode *frag = xmlNewDocFragment(node->doc);
18 xmlReplaceNode(node, frag);
19 frag->children = node->children;
20 frag->last = node->last;
22 node->children = node->last = NULL;
27 // Does A begin with B?
28 bool begins_with(const string &a, const string &b)
30 return (a.compare(0, b.size(), b) == 0);
35 Directive::~Directive() {}
37 string Directive::get_contents() { return ""; }
39 Replace::Replace(const string &str)
42 void Replace::process(xmlNode *node, bool clean) {
43 xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(str.data()), str.size());
49 string Replace::get_contents() { return str; }
51 ReplaceInclude::ReplaceInclude(xmlNodePtr included_node)
52 : included_node(included_node),
55 ReplaceInclude::ReplaceInclude(xmlDocPtr included_doc)
56 : included_node(xmlDocGetRootElement(included_doc)),
57 included_doc(included_doc) {}
59 ReplaceInclude::~ReplaceInclude()
61 if (included_doc != NULL) {
62 xmlFreeDoc(included_doc);
64 xmlFreeNode(included_node);
68 void ReplaceInclude::process(xmlNode *node, bool clean) {
69 xmlFreeNodeList(node->children);
71 node->children = node->last = NULL;
74 xmlDOMWrapCloneNode(NULL, NULL, included_node, &new_node, node->doc, node, 1, 0);
75 xmlAddChild(node, new_node);
78 Substitute dummy_subs {};
79 dummy_subs.process(new_node, clean);
84 Clone::Clone(const vector<Directive *> &subdirectives)
85 : subdirectives(subdirectives) {}
87 Clone::Clone(const vector<Substitute *> &subdirectives_subs)
89 for (auto it : subdirectives_subs) {
90 subdirectives.push_back(static_cast<Directive *>(it));
94 Clone::Clone(initializer_list<Directive *> subdirectives)
95 : subdirectives(subdirectives) {}
99 for (auto it : subdirectives) {
104 void Clone::process(xmlNode *node, bool clean)
106 // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
107 // how to clone elements.
108 vector<xmlNode *> new_nodes;
110 for (auto it : subdirectives) {
115 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
116 it->process(new_node, clean);
117 while (new_node->children != NULL) {
118 xmlNode *child = new_node->children;
119 xmlUnlinkNode(child);
120 new_nodes.push_back(child);
122 xmlFreeNode(new_node);
125 xmlFreeNodeList(node->children);
126 node->content = NULL;
127 node->children = node->last = NULL;
129 for (auto child : new_nodes) {
130 xmlAddChild(node, child);
137 Alternate::Alternate(const string &attribute,
138 const vector<Substitute *> &subdirectives_subs,
139 const vector<string> &alternatives)
140 : Clone(subdirectives_subs)
143 for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
144 if (subdirectives_subs[ix] == NULL) {
147 string value = alternatives[jx++ % alternatives.size()];
148 subdirectives_subs[ix]->substitution_map.insert(make_pair(
150 new Replace { value }));
154 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
155 : substitution_map(substitution_map) {}
157 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
158 : substitution_map(substitution_map) {}
160 Substitute::~Substitute()
162 for (auto it : substitution_map) {
167 void Substitute::process(xmlNode *node, bool clean)
170 for (xmlNode *child = node->children; child != NULL; child = next_child) {
171 next_child = child->next;
172 Directive *next_processor = this;
174 if (child->type == XML_ELEMENT_NODE) {
175 // Find the ID, if any.
177 xmlAttr *id_attr = NULL;
178 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
179 if (attr->ns != NULL &&
180 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
181 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
182 xmlChar *id_buf = xmlNodeGetContent(attr->children);
183 id = reinterpret_cast<const char *>(id_buf);
188 if (clean && id_attr != NULL) {
189 xmlRemoveProp(id_attr);
192 // Check all substitutions to see if we found anything appropriate.
193 for (auto it : substitution_map) {
194 string tag = reinterpret_cast<const char *>(child->name);
196 // Attribute substitution.
197 if (begins_with(it.first, tag + "/")) {
198 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
199 it.first.c_str() + tag.size() + 1);
200 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
201 it.second->get_contents().c_str());
202 xmlSetProp(child, attr_key, attr_value);
203 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
204 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
205 it.first.c_str() + tag.size() + 2);
206 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
207 it.second->get_contents().c_str());
208 xmlSetProp(child, attr_key, attr_value);
211 // Regular substitution. (Don't call process() immediately, because
212 // that might delete the element, which would cause problems.)
213 if (it.first == tag ||
214 (!id.empty() && it.first == ("#" + id))) {
215 next_processor = it.second;
220 next_processor->process(child, clean);
227 xmlDocPtr process_file(const string &input_filename,
228 Directive *root_directive,
233 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
234 root_directive->process(xmlDocGetRootElement(doc), clean);
240 void output_to_fd_and_free(xmlDocPtr doc, int fd)
242 xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
243 xmlSaveFileTo(buf, doc, NULL);