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 while (node->children != NULL) {
18 xmlAddPrevSibling(node, node->children);
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 ReplaceInclude::ReplaceInclude(xmlNodePtr included_node)
51 : included_node(included_node),
54 ReplaceInclude::ReplaceInclude(xmlDocPtr included_doc)
55 : included_node(xmlDocGetRootElement(included_doc)),
56 included_doc(included_doc) {}
58 ReplaceInclude::~ReplaceInclude()
60 if (included_doc != NULL) {
61 xmlFreeDoc(included_doc);
63 xmlFreeNode(included_node);
67 void ReplaceInclude::process(xmlNode *node, bool clean) {
68 xmlFreeNodeList(node->children);
70 node->children = node->last = NULL;
73 xmlDOMWrapCloneNode(NULL, NULL, included_node, &new_node, node->doc, node, 1, 0);
74 xmlAddChild(node, new_node);
77 Substitute dummy_subs {};
78 dummy_subs.process(new_node, clean);
83 Clone::Clone(const vector<Directive *> &subdirectives)
84 : subdirectives(subdirectives) {}
86 Clone::Clone(const vector<Substitute *> &subdirectives_subs)
88 for (auto it : subdirectives_subs) {
89 subdirectives.push_back(static_cast<Directive *>(it));
93 Clone::Clone(initializer_list<Directive *> subdirectives)
94 : subdirectives(subdirectives) {}
98 for (auto it : subdirectives) {
103 void Clone::process(xmlNode *node, bool clean)
105 // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
106 // how to clone elements.
107 vector<xmlNode *> new_nodes;
109 for (auto it : subdirectives) {
114 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
115 it->process(new_node, clean);
116 while (new_node->children != NULL) {
117 xmlNode *child = new_node->children;
118 xmlUnlinkNode(child);
119 new_nodes.push_back(child);
121 xmlFreeNode(new_node);
124 xmlFreeNodeList(node->children);
125 node->content = NULL;
126 node->children = node->last = NULL;
128 for (auto child : new_nodes) {
129 xmlAddChild(node, child);
136 Alternate::Alternate(const string &attribute,
137 const vector<Substitute *> &subdirectives_subs,
138 const vector<string> &alternatives)
139 : Clone(subdirectives_subs)
142 for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
143 if (subdirectives_subs[ix] == NULL) {
146 string value = alternatives[jx++ % alternatives.size()];
147 subdirectives_subs[ix]->substitution_map.insert(make_pair(
149 new Replace { value }));
153 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
154 : substitution_map(substitution_map) {}
156 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
157 : substitution_map(substitution_map) {}
159 Substitute::~Substitute()
161 for (auto it : substitution_map) {
166 void Substitute::process(xmlNode *node, bool clean)
169 for (xmlNode *child = node->children; child != NULL; child = next_child) {
170 next_child = child->next;
171 Directive *next_processor = this;
173 if (child->type == XML_ELEMENT_NODE) {
174 // Find the ID, if any.
176 xmlAttr *id_attr = NULL;
177 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
178 if (attr->ns != NULL &&
179 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
180 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
181 xmlChar *id_buf = xmlNodeGetContent(attr->children);
182 id = reinterpret_cast<const char *>(id_buf);
187 if (clean && id_attr != NULL) {
188 xmlRemoveProp(id_attr);
191 // Check all substitutions to see if we found anything appropriate.
192 string tag = reinterpret_cast<const char *>(child->name);
193 for (auto it : substitution_map) {
194 // Attribute substitution.
195 if (begins_with(it.first, tag + "/")) {
196 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
197 it.first.c_str() + tag.size() + 1);
198 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
199 it.second->get_contents().c_str());
200 xmlSetProp(child, attr_key, attr_value);
201 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
202 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
203 it.first.c_str() + id.size() + 2);
204 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
205 it.second->get_contents().c_str());
206 xmlSetProp(child, attr_key, attr_value);
209 // Regular substitution. (Don't call process() immediately, because
210 // that might delete the element, which would cause problems.)
211 if (it.first == tag ||
212 (!id.empty() && it.first == ("#" + id))) {
213 next_processor = it.second;
218 next_processor->process(child, clean);
225 xmlDocPtr process_file(const string &input_filename,
226 Directive *root_directive,
231 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
232 root_directive->process(xmlDocGetRootElement(doc), clean);
238 void output_to_fd_and_free(xmlDocPtr doc, int fd)
240 xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
241 xmlSaveFileTo(buf, doc, NULL);