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) {
78 xmlDOMWrapCloneNode(NULL, node->doc, node, &new_node, node->doc, NULL, 1, 0);
79 it->process(new_node, clean);
80 while (new_node->children != NULL) {
81 xmlNode *child = new_node->children;
83 new_nodes.push_back(child);
85 xmlFreeNode(new_node);
88 xmlFreeNodeList(node->children);
90 node->children = node->last = NULL;
92 for (auto child : new_nodes) {
93 xmlAddChild(node, child);
100 Alternate::Alternate(const string &attribute,
101 const vector<Substitute *> &subdirectives_subs,
102 const vector<string> &alternatives)
103 : Clone(subdirectives_subs)
105 for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
106 string value = alternatives[ix % alternatives.size()];
107 subdirectives_subs[ix]->substitution_map.insert(make_pair(
109 new Replace { value }));
113 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
114 : substitution_map(substitution_map) {}
116 Substitute::Substitute(initializer_list<pair<const string, Directive*>> substitution_map)
117 : substitution_map(substitution_map) {}
119 Substitute::~Substitute()
121 for (auto it : substitution_map) {
126 void Substitute::process(xmlNode *node, bool clean)
129 for (xmlNode *child = node->children; child != NULL; child = next_child) {
130 next_child = child->next;
131 bool processed = false;
133 if (child->type == XML_ELEMENT_NODE) {
134 // Find the ID, if any.
136 xmlAttr *id_attr = NULL;
137 for (xmlAttr *attr = child->properties; attr != NULL; attr = attr->next) {
138 if (attr->ns != NULL &&
139 strcmp(reinterpret_cast<const char *>(attr->ns->href), "http://template.sesse.net/") == 0 &&
140 strcmp(reinterpret_cast<const char *>(attr->name), "id") == 0) {
141 xmlChar *id_buf = xmlNodeGetContent(attr->children);
142 id = reinterpret_cast<const char *>(id_buf);
147 if (clean && id_attr != NULL) {
148 xmlRemoveProp(id_attr);
151 // Check all substitutions to see if we found anything appropriate.
152 for (auto it : substitution_map) {
153 string tag = reinterpret_cast<const char *>(child->name);
155 // Attribute substitution.
156 if (begins_with(it.first, tag + "/")) {
157 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
158 it.first.substr(tag.size() + 1).c_str());
159 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
160 it.second->get_contents().c_str());
161 xmlSetProp(child, attr_key, attr_value);
162 } else if ((!id.empty() && begins_with(it.first, "#" + id + "/"))) {
163 const xmlChar *attr_key = reinterpret_cast<const xmlChar *>(
164 it.first.substr(tag.size() + 2).c_str());
165 const xmlChar *attr_value = reinterpret_cast<const xmlChar *>(
166 it.second->get_contents().c_str());
167 xmlSetProp(child, attr_key, attr_value);
174 // Regular substitution.
175 if (it.first == tag ||
176 (!id.empty() && it.first == ("#" + id))) {
177 it.second->process(child, clean);
184 process(child, clean);
192 void process_file(const string &input_filename,
193 const string &output_filename,
194 Directive *root_directive)
198 xmlDocPtr doc = xmlParseFile(input_filename.c_str());
199 root_directive->process(xmlDocGetRootElement(doc), true);
200 xmlSaveFile(output_filename.c_str(), doc);