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(const vector<Substitute *> &subdirectives_subs)
55 for (auto it : subdirectives_subs) {
56 subdirectives.push_back(static_cast<Directive *>(it));
60 Clone::Clone(initializer_list<Directive *> subdirectives)
61 : subdirectives(subdirectives) {}
65 for (auto it : subdirectives) {
70 void Clone::process(xmlNode *node, bool clean)
72 // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows
73 // how to clone elements.
74 vector<xmlNode *> new_nodes;
76 for (auto it : subdirectives) {
81 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
82 it->process(new_node, clean);
83 while (new_node->children != NULL) {
84 xmlNode *child = new_node->children;
86 new_nodes.push_back(child);
88 xmlFreeNode(new_node);
91 xmlFreeNodeList(node->children);
93 node->children = node->last = NULL;
95 for (auto child : new_nodes) {
96 xmlAddChild(node, child);
103 Alternate::Alternate(const string &attribute,
104 const vector<Substitute *> &subdirectives_subs,
105 const vector<string> &alternatives)
106 : Clone(subdirectives_subs)
109 for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
110 if (subdirectives_subs[ix] == NULL) {
113 string value = alternatives[jx++ % alternatives.size()];
114 subdirectives_subs[ix]->substitution_map.insert(make_pair(
116 new Replace { value }));
120 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
121 : substitution_map(substitution_map) {}
123 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
124 : substitution_map(substitution_map) {}
126 Substitute::~Substitute()
128 for (auto it : substitution_map) {
133 void Substitute::process(xmlNode *node, bool clean)
136 for (xmlNode *child = node->children; child != NULL; child = next_child) {
137 next_child = child->next;
138 bool processed = false;
140 if (child->type == XML_ELEMENT_NODE) {
141 // Find the ID, if any.
143 xmlAttr *id_attr = NULL;
144 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
145 if (attr->ns != NULL &&
146 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
147 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
148 xmlChar *id_buf = xmlNodeGetContent(attr->children);
149 id = reinterpret_cast<const char *>(id_buf);
154 if (clean && id_attr != NULL) {
155 xmlRemoveProp(id_attr);
158 // Check all substitutions to see if we found anything appropriate.
159 for (auto it : substitution_map) {
160 string tag = reinterpret_cast<const char *>(child->name);
162 // Attribute substitution.
163 if (begins_with(it.first, tag + "/")) {
164 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
165 it.first.substr(tag.size() + 1).c_str());
166 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
167 it.second->get_contents().c_str());
168 xmlSetProp(child, attr_key, attr_value);
169 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
170 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
171 it.first.substr(tag.size() + 2).c_str());
172 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
173 it.second->get_contents().c_str());
174 xmlSetProp(child, attr_key, attr_value);
181 // Regular substitution.
182 if (it.first == tag ||
183 (!id.empty() && it.first == ("#" + id))) {
184 it.second->process(child, clean);
191 process(child, clean);
199 xmlDocPtr process_file(const string &input_filename,
200 Directive *root_directive)
204 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
205 root_directive->process(xmlDocGetRootElement(doc), true);
211 void output_to_fd_and_free(xmlDocPtr doc, int fd)
213 xmlOutputBufferPtr buf = xmlOutputBufferCreateFd(fd, NULL);
214 xmlSaveFileTo(buf, doc, NULL);