1 #include "xml-template.h"
5 #include <libxml/parser.h>
11 void clean_node(xmlNode *node)
13 if (node->type != XML_ELEMENT_NODE) {
16 if (node->ns != NULL &&
17 strcmp(reinterpret_cast<const char *>(node->ns->href), "http://template.sesse.net/") == 0) {
18 while (node->children != NULL) {
19 xmlAddPrevSibling(node, node->children);
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 Replace *r = new Replace { value };
149 subdirectives_subs[ix]->substitution_map.insert(make_pair(
151 new Replace { value }));
155 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
156 : substitution_map(substitution_map) {}
158 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
159 : substitution_map(substitution_map) {}
161 Substitute::~Substitute()
163 for (auto it : substitution_map) {
168 void Substitute::process(xmlNode *node, bool clean)
171 for (xmlNode *child = node->children; child != NULL; child = next_child) {
172 next_child = child->next;
173 Directive *next_processor = this;
175 if (child->type == XML_ELEMENT_NODE) {
176 // Find the ID, if any.
178 xmlAttr *id_attr = NULL;
179 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
180 if (attr->ns != NULL &&
181 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
182 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
183 xmlChar *id_buf = xmlNodeGetContent(attr->children);
184 id = reinterpret_cast<const char *>(id_buf);
189 if (clean && id_attr != NULL) {
190 xmlRemoveProp(id_attr);
193 // Check all substitutions to see if we found anything appropriate.
194 string tag = reinterpret_cast<const char *>(child->name);
195 for (auto it : substitution_map) {
196 assert(it.second != nullptr);
197 // Attribute substitution.
198 if (begins_with(it.first, tag + "/")) {
199 string contents = it.second->get_contents();
200 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
201 it.first.c_str() + tag.size() + 1);
202 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
204 xmlSetProp(child, attr_key, attr_value);
205 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
206 string contents = it.second->get_contents();
207 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
208 it.first.c_str() + id.size() + 2);
209 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
211 xmlSetProp(child, attr_key, attr_value);
214 // Regular substitution. (Don't call process() immediately, because
215 // that might delete the element, which would cause problems.)
216 if (it.first == tag ||
217 (!id.empty() && it.first == ("#" + id))) {
218 next_processor = it.second;
223 next_processor->process(child, clean);
230 xmlDocPtr process_file(const string &input_filename,
231 Directive *root_directive,
236 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
237 root_directive->process(xmlDocGetRootElement(doc), clean);
243 void output_to_fd_and_free(xmlDocPtr doc, int fd)
245 xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
246 xmlSaveFileTo(buf, doc, NULL);